Boost logo

Boost Users :

From: Marc Mutz (marc.mutz_at_[hidden])
Date: 2020-12-21 10:55:21


Hi,

I'm trying to move away from Qt containers in my source code, and Qt
5.15 makes that a bit easier by providing specialisations for some its
types.

Now I have a struct S { QLatin1String a, b; } that I'd like to hash.

I can use boost::hash_combine to implement std::hash<S>, but S is
actually a nested type, and with qHash(), I could just write an overload
as a hidden friend, and I figured I could do the same with hash_value()
(at the cost of having to specify boost::hash<S> as a unordered_map
template argument).

But hash_combine seems to insist on hash_value which in turn doesn't
look at std::hash at all:

       size_t seed = 0;
       boost::hash_combine(seed, key.a); // ERROR: no hash_value overload
for QLatin1String
       boost::hash_combine(seed, key.b); // Ditto
       return seed;

expected: calls std::hash<QLatin1String>{}::op() (if it exists).

It's trivial to write hash_value(QLatin1String), of course, but what's
the general strategy here? Shouldn't hash_combine() look for
std::hash<T> first? And, speaking as a Qt developer: do I really need to
provide qHash(), std::hash<T> _and_ hash_value(T) for every hashable T?!

Thanks,
Marc


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net