Boost logo

Boost :

Subject: [boost] [hash] Turn on BOOST_HASH_NO_IMPLICIT_CASTS by default
From: Daniel James (dnljms_at_[hidden])
Date: 2012-04-15 05:10:53


Hello everyone,

There have been occasional issues where Boost.Hash will incorrectly
hash an object that doesn't have a hash function but is convertible to
bool. I added an extra templated overload of hash_value which contains
a static assert. The idea is that it's a better match than the
implicit cast, so it will cause a compile error in this case. This is
currently only activated when a macro is defined, as it could break
otherwise valid code. I was a bit worried about making any interface
changes because at this stage, Boost.Hash should be very stable from
release to release. The problem turned up again yesterday, so I'm
starting to think it's enough of a problem that it's worth activating
this be default.

A different solution might be to change the implement of Boost.Hash
for bools, so that the bool overload isn't required. But that would be
treating the sympton, as other implicit casts could cause problems.

An example of valid code that will now break is something like:

    struct base;
    std::size_t hash_value(base const&);

    struct derived : base
    {
        void some_method_but_no_extra_state();
    };

    boost::hash<derived> derived_hash;

Currently that will automatically use base's hash_value, with this
change it will be a compile error. Although that might be a good
thing, as sometime it is an error to use the base's hash function.

Of course, there will be a macro to keep the old behaviour. Any
objections to this change?


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