Std :: unordered_set <Foo> as a member of the Foo class
I am writing a class that has a unordered_set of its type as a member. Therefore, I need to write a specialization for hash<Foo>. This specialization should be determined after the declaration of Foo. But it seems to me that I already need a specialization for hash<Foo>, before defining a member unordered_set<Foo>. At least it does not compile and does not work. I tried declaring a hash template ahead, but couldn't get it to work.
Corresponding code snippet:
class Foo {
public:
int i;
std::unordered_set<Foo> dummy;
Peer(std::unordered_set<Foo>);
};
namespace std {
template<> struct hash<Foo>
{
size_t operator()(const Foo& f) const
{
return hash<int>()(f.i);
}
};
}
Thanks in advance
+5
2 answers
, :
class Foo;
namespace std {
template<> struct hash<Foo> {
size_t operator()(const Foo& f) const;
};
}
class Foo {
public:
int i;
std::unordered_set<Foo> dummy;
Foo(std::unordered_set<Foo>);
};
namespace std {
size_t hash<Foo>::operator()(const Foo& f) const {
return hash<int>()(f.i);
}
}
, dummy undefined.
; operator== Foo.
, Foo const-reference.
+1