Is this valid C++ (considering the latest standard)? I’m getting compilation errors with near-top-of-tree clang/libc++ on Ubuntu 12.04. If it should be valid, I’ll mail the clang-dev list with error messages and such.
#include <functional>
#include <unordered_set>
struct X
{
int i;
};
void f ()
{
std::unordered_set<std::reference_wrapper<X>> setOfReferencesToX;
// Do stuff with setOfReferencesToX
}
** As an aside, I’m tired of qualifying that the question/answer is specific to the latest standard. Could the C++ community as a whole, please start qualifying things that are specific to the old standard instead? The newer standard has been out for about a year now.
The problem is not specific to
std::reference_wrapper<T>, but rather to the typeXitself.The issue is that std::unordered_set requires that you define hashing and equality functors for
std::reference_wrapper<X>. You can pass the hash functor as second template parameter.For example, this would work:
and then
Another option is to make a specialization for
std::hash<X>:This allows you to avoid explicitly specifying the 2nd template argument:
Concerning the equality comparison, you can fix this by providing an equality operator for class
X:Otherwise, you can define your own functor and pass it as third template argument.