Opened 12 years ago

Closed 12 years ago

#5216 closed Bugs (fixed)

boost::hash<boost::shared_ptr<T>> Returns Only Two Values

Reported by: Rob Stewart <robert.stewart@…> Owned by: Peter Dimov
Milestone: To Be Determined Component: smart_ptr
Version: Boost 1.44.0 Severity: Problem
Keywords: shared_ptr hash Cc:

Description

The attached program 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.

Attachments (1)

bad_hash.cpp (708 bytes ) - added by Rob Stewart <robert.stewart@…> 12 years ago.
Bug demonstration program

Download all attachments as: .zip

Change History (5)

by Rob Stewart <robert.stewart@…>, 12 years ago

Attachment: bad_hash.cpp added

Bug demonstration program

comment:1 by Rob Stewart <robert.stewart@…>, 12 years ago

Defining BOOST_HASH_NO_IMPLICIT_CASTS produces a static assertion failure which is better than the silent misbehavior.

The following makes things work, regardless of whether BOOST_HASH_NO_IMPLICIT_CASTS is defined:

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

comment:2 by Peter Dimov, 12 years ago

(In [69260]) Add hash_value for shared_ptr; prevents hash_value( bool ) from being used. Refs #5216.

comment:3 by Peter Dimov, 12 years ago

Status: newassigned

comment:4 by Peter Dimov, 12 years ago

Resolution: fixed
Status: assignedclosed

(In [70440]) Merge [69260] to release. Fixes #5216.

Note: See TracTickets for help on using tickets.