From 67c3951e3f233f08a389b3a89584317397a3338e Mon Sep 17 00:00:00 2001 From: Daniel James Date: Tue, 6 Jul 2010 23:32:37 +0000 Subject: [PATCH] Try preventing static casts when calling `hash_value`. [SVN r63716] --- hash/test/Jamfile.v2 | 2 ++ hash/test/implicit_fail_test.cpp | 16 ++++++++++++++++ hash/test/shared_ptr_fail_test.cpp | 11 +++++++++++ include/boost/functional/hash/hash.hpp | 9 +++++++++ 4 files changed, 38 insertions(+) create mode 100644 hash/test/implicit_fail_test.cpp create mode 100644 hash/test/shared_ptr_fail_test.cpp diff --git a/hash/test/Jamfile.v2 b/hash/test/Jamfile.v2 index 4435dd2..c2d8084 100644 --- a/hash/test/Jamfile.v2 +++ b/hash/test/Jamfile.v2 @@ -50,6 +50,8 @@ test-suite functional/hash [ 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 ] ; diff --git a/hash/test/implicit_fail_test.cpp b/hash/test/implicit_fail_test.cpp new file mode 100644 index 0000000..1dd0555 --- /dev/null +++ b/hash/test/implicit_fail_test.cpp @@ -0,0 +1,16 @@ +#include + +namespace test +{ + struct base {}; + std::size_t hash_value(base const&) { return 0; } + + struct converts { operator base() const { return base(); } }; +} + +int main() { + boost::hash hash; + test::converts x; + + hash(x); +} \ No newline at end of file diff --git a/hash/test/shared_ptr_fail_test.cpp b/hash/test/shared_ptr_fail_test.cpp new file mode 100644 index 0000000..584739f --- /dev/null +++ b/hash/test/shared_ptr_fail_test.cpp @@ -0,0 +1,11 @@ +#include +#include + +// This should obviously pass if shared_ptr ever supports Boost.Hash. + +int main() { + boost::hash > hash; + boost::shared_ptr x(new int(10)); + + hash(x); +} \ No newline at end of file diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index e85ca5a..bd8d37f 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) #include @@ -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 + 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);