I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName;
It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that?
After looking through all the pages of error messages I found to lines that might help.
/usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type
/usr/include/c++/4.7/bits/stl_function.h: In instantiation of ‘bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::weak_ptr<Node>]’:
Please read Richard Hodges answer below as mine is incorrect, despite being the accepted solution.
Since
unordered_setsare hash-based you have to provide a hash function object for the std::weak_ptr data-type.If you take a look at the unordered_set template-parameters
you’ll notice that std::unordered_set provides you with a default std::hash<> template parameter. But since std::hash does only provide specializations for a specific set of data types, you might have to provide your own.
The error-message you quoted tells you, that no std::hash<> specialization for std::weak_ptr<> exists, so you have to provide your own hashing function for that:
Edit:
You also need to provide an equality function, since no std::equal_to for weak_ptr is provided.
Taking a possible way to do this from "Equality-compare std::weak_ptr" on Stackoverflow:
All combined this gives us the following: