I'm trying to use QString as the key in a std::unordered_map, however I get the error:
error C2280: 'std::hash<_Kty>::hash(const std::hash<_Kty> &)': attempting to reference a deleted function
I can't switch to QHash because the value-type of the map is non-copyable. Is there any way to make this work?
Put the
hashimplementation in a header, and make sure that you include that header everywhere the map is used.A trivial implementation that forwards to
qHashshould be sufficient:Even though
std::size_tis larger thanunsigned inton common 64 bit platforms, and thus the hash doesn't change across its full length - this isn't a problem. The standard places no such requirement on anstd::hashimplementation.Let's not forget, though, that modifying anything in the
stdnamespace is generally undefined behavior and unnecessary.TL;DR:
You're allowed to specialize certain type and variable templates, and only if the specialization depends on at least one user-defined type. You can't fully specialize for built-in nor C++ library types.
See Extending the namespace std for reference. There, we read:
Mainly, it is allowed to specialize certain types in the
stdnamespace:Note that what's legal is specializing types, i.e. classes. Functions and member functions? Not ever:
Another limited exception is for variable templates:
Emphasis mine in all cases. As always, there are further details that one should get acquainted with.