Boost logo

Boost :

Subject: Re: [boost] [functional] [shared_ptr] boost::hash<shared_ptr<T>> Returns Only Two Values
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-02-18 16:13:38

Daniel James wrote:
> On 18 February 2011 19:40, Stewart, Robert
> <Robert.Stewart_at_[hidden]> wrote:
> > The following is a program that demonstrates that a null
> > shared_ptr<int> produces a hash value of zero and that
> > (ostensibly) all non-null instances produce a hash value
> > of one.
> This is known, it happens because shared_ptr can be implicitly cast to
> bool, so it uses that. If you set BOOST_HASH_NO_IMPLICIT_CASTS it
> won't happen. This macro isn't set by default because I'm not sure if
> there will be any other consequences from preventing implicit casts,
> but it might be in a future version.

Thanks for the information.

We knew it was due to the Safe Bool Idiom, but it was surprising that there was no documentation on the issue or a suitable solution available.

Defining BOOST_HASH_NO_IMPLICIT_CASTS produces a static assertion failure which is certainly better than silent misbehavior. It would be nice to ascertain whether allowing implicit casts is really worthwhile.

> > I expected that there'd be a shared_ptr specialization of
> > hash_value to make it work right, but I don't find one.
> > Shouldn't there be one?
> That's an issue for the shared_ptr implementation.

The following worked even without defining BOOST_HASH_NO_IMPLICIT_CASTS:

namespace boost
   template <class T>
   hash_value(boost::shared_ptr<T> const & _ptr)
      return boost::hash_value(_ptr.get());

Peter, should this be added to boost/smart_ptr/shared_ptr_hash_value.hpp or some such?

Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Boost list run by bdawes at, gregod at, cpdaniel at, john at