Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63716 - in trunk: boost/functional/hash libs/functional/hash/test
From: daniel_james_at_[hidden]
Date: 2010-07-06 19:32:39


Author: danieljames
Date: 2010-07-06 19:32:37 EDT (Tue, 06 Jul 2010)
New Revision: 63716
URL: http://svn.boost.org/trac/boost/changeset/63716

Log:
Try preventing static casts when calling `hash_value`.
Added:
   trunk/libs/functional/hash/test/implicit_fail_test.cpp (contents, props changed)
   trunk/libs/functional/hash/test/shared_ptr_fail_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/functional/hash/hash.hpp | 9 +++++++++
   trunk/libs/functional/hash/test/Jamfile.v2 | 2 ++
   2 files changed, 11 insertions(+), 0 deletions(-)

Modified: trunk/boost/functional/hash/hash.hpp
==============================================================================
--- trunk/boost/functional/hash/hash.hpp (original)
+++ trunk/boost/functional/hash/hash.hpp 2010-07-06 19:32:37 EDT (Tue, 06 Jul 2010)
@@ -15,6 +15,7 @@
 #include <boost/functional/hash/detail/hash_float.hpp>
 #include <string>
 #include <boost/limits.hpp>
+#include <boost/static_assert.hpp>
 
 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
 #include <boost/type_traits/is_pointer.hpp>
@@ -29,6 +30,14 @@
 
 namespace boost
 {
+ // If you get a static assertion here, it's because hash_value
+ // isn't declared for your type.
+ template <typename T>
+ std::size_t hash_value(T const&) {
+ BOOST_STATIC_ASSERT((T*) 0 && false);
+ return 0;
+ }
+
     std::size_t hash_value(bool);
     std::size_t hash_value(char);
     std::size_t hash_value(unsigned char);

Modified: trunk/libs/functional/hash/test/Jamfile.v2
==============================================================================
--- trunk/libs/functional/hash/test/Jamfile.v2 (original)
+++ trunk/libs/functional/hash/test/Jamfile.v2 2010-07-06 19:32:37 EDT (Tue, 06 Jul 2010)
@@ -50,6 +50,8 @@
         [ run container_no_fwd_test.cpp ]
         [ compile-fail hash_no_ext_fail_test.cpp ]
         [ compile-fail namespace_fail_test.cpp ]
+ [ compile-fail implicit_fail_test.cpp ]
+ [ compile-fail shared_ptr_fail_test.cpp ]
         [ run hash_no_ext_macro_1.cpp ]
         [ run hash_no_ext_macro_2.cpp ]
     ;

Added: trunk/libs/functional/hash/test/implicit_fail_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/functional/hash/test/implicit_fail_test.cpp 2010-07-06 19:32:37 EDT (Tue, 06 Jul 2010)
@@ -0,0 +1,16 @@
+#include <boost/functional/hash.hpp>
+
+namespace test
+{
+ struct base {};
+ std::size_t hash_value(base const&) { return 0; }
+
+ struct converts { operator base() const { return base(); } };
+}
+
+int main() {
+ boost::hash<test::converts> hash;
+ test::converts x;
+
+ hash(x);
+}
\ No newline at end of file

Added: trunk/libs/functional/hash/test/shared_ptr_fail_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/functional/hash/test/shared_ptr_fail_test.cpp 2010-07-06 19:32:37 EDT (Tue, 06 Jul 2010)
@@ -0,0 +1,11 @@
+#include <boost/functional/hash.hpp>
+#include <boost/shared_ptr.hpp>
+
+// This should obviously pass if shared_ptr ever supports Boost.Hash.
+
+int main() {
+ boost::hash<boost::shared_ptr<int> > hash;
+ boost::shared_ptr<int> x(new int(10));
+
+ hash(x);
+}
\ No newline at end of file


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk