Following code shows what I want to achieve, but I'm not sure whether it is possible in C++.
class Hashable {
public:
virtual std::size_t hash() const = 0;
};
class A : public Hashable {
std::size_t hash() override const {
...
return hash_value;
}
};
class B : public Hashable {
std::size_t hash() override const {
...
return hash_value;
}
};
namespace std {
template<>
struct hash<typename T /*where T matches all type that has hash member function*/> {
std::size_t operator()(const T& t) {
return t.hash();
}
};
}