Boost logo

Boost Users :

Subject: Re: [Boost-users] [hash] hashing of smart pointer types - prevent conversion to bool?
From: Daniel James (dnljms_at_[hidden])
Date: 2010-07-06 19:02:34


On 6 July 2010 13:52, Igor R <boost.lists_at_[hidden]> wrote:
>> One idea is to patch the boost headers to add a hash function for each of
>> the smart pointer types (as per the example above) but there must be a better answer than this?
>
> Technically, you can define hash functions in your own headers, in
> boost namespace (that's what I do). I'm not sure about legal aspects
> of this though.

It's open source, so really it's up to you. Although I'd suggest that
if you have adapted a boost class, it'd be a good idea to submit a
patch to the appropriate maintainer.

boost::shared_ptr should be pretty simple, something like (untested):

namespace boost
{
    template <typename T>
    std::size_t hash_value(boost::shared_ptr<T> const& x)
    {
        boost::hash<T*> hash_ptr;
        return hash_ptr(x.get());
    }
}

Daniel


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