unordered_mapクラスのシグネチャは次のとおりです。
template<class Key,
class Ty,
class Hash = std::hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, Ty> > >
class unordered_map;
デフォルトのPred、std :: equal_to <>は、デフォルトでoperator ==を使用して等しいかどうかをチェックするため、この例は機能します。コンパイラはfoo::operator ==メンバー関数を見つけて、それを使用します。
std :: hashには、クラスのメンバー関数を呼び出す特殊化がないため、カスタムハッシュを使用してfooにメンバーを追加することはできません。代わりに、std::hashを特殊化する必要があります。fooのメンバー関数を呼び出す場合は、先に進んでください。最終的には次のようになります。
struct foo
{
size_t hash() const
{
// hashing method here, return a size_t
}
};
namespace std
{
// Specialise std::hash for foo.
template<>
class hash< foo >
: public unary_function< foo, size_t >
{
public:
size_t operator()( const foo& f )
{
return f.hash();
}
};
}