Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2021-06-05 17:07:57


John Maddock wrote:
> What's the best way to combine a hash value with a bool?
>
> Specifically, if you're generating a hash value for a class which has a one
> member a bool. Normally I would just boost::hash_combine all the class
> members together, but doing that with a bool member leads to:
>
> 1>d:\data\boost\boost\boost\multiprecision\detail\hash.hpp(20,88):
> warning C4805: '^': unsafe mix of type 'T' and type 'size_t' in operation
> 1> with
> 1> [
> 1> T=bool
> 1> ]
>
> Followed by a long instantiation backtrace (this is from Boost.Multiprecision).
>
> Since there's only one bool in this class, I could just apply a unary ~ to the
> value when the bool is true, but obviously that wouldn't work for classes with
> multiple bool's. Should we modify hash_combine for this use case?

You're using your own (wrong) hash_combine, so "we" can't modify anything. :-)

You need to apply hash_value to the T argument to produce a size_t first,
instead of using it directly. This will also get rid of the casts.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk