From 69aa01ec001ac2a737274fb7738d2c370fac3d19 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 24 Feb 2011 23:24:54 +0000 Subject: [PATCH] Add hash_value for shared_ptr; prevents hash_value( bool ) from being used. Refs #5216. [SVN r69260] --- include/boost/smart_ptr/shared_ptr.hpp | 12 ++++++++- test/Jamfile.v2 | 1 + test/sp_hash_test.cpp | 34 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/sp_hash_test.cpp diff --git a/include/boost/smart_ptr/shared_ptr.hpp b/include/boost/smart_ptr/shared_ptr.hpp index 66ab48a..16a1f92 100644 --- a/include/boost/smart_ptr/shared_ptr.hpp +++ b/include/boost/smart_ptr/shared_ptr.hpp @@ -41,6 +41,7 @@ #include // for std::swap #include // for std::less #include // for std::bad_cast +#include // for std::size_t #if !defined(BOOST_NO_IOSTREAM) #if !defined(BOOST_NO_IOSFWD) @@ -722,7 +723,16 @@ template inline bool atomic_compare_exchange_explicit( shared_ptr * return atomic_compare_exchange( p, v, w ); // std::move( w ) } -#endif +#endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS) + +// hash_value + +template< class T > struct hash; + +template< class T > std::size_t hash_value( boost::shared_ptr const & p ) +{ + return boost::hash< T* >()( p.get() ); +} } // namespace boost diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 95e0654..632ef08 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -65,5 +65,6 @@ import testing ; [ run atomic_count_test2.cpp ] [ run sp_typeinfo_test.cpp ] [ compile make_shared_fp_test.cpp ] + [ run sp_hash_test.cpp ] ; } diff --git a/test/sp_hash_test.cpp b/test/sp_hash_test.cpp new file mode 100644 index 0000000..b67f547 --- /dev/null +++ b/test/sp_hash_test.cpp @@ -0,0 +1,34 @@ +// +// sp_hash_test.cpp +// +// Copyright 2011 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include +#include + +int main() +{ + boost::hash< boost::shared_ptr > hasher; + + boost::shared_ptr< int > p1, p2( p1 ), p3( new int ), p4( p3 ), p5( new int ); + + BOOST_TEST_EQ( p1, p2 ); + BOOST_TEST_EQ( hasher( p1 ), hasher( p2 ) ); + + BOOST_TEST_NE( p1, p3 ); + BOOST_TEST_NE( hasher( p1 ), hasher( p3 ) ); + + BOOST_TEST_EQ( p3, p4 ); + BOOST_TEST_EQ( hasher( p3 ), hasher( p4 ) ); + + BOOST_TEST_NE( p3, p5 ); + BOOST_TEST_NE( hasher( p3 ), hasher( p5 ) ); + + return boost::report_errors(); +}