From 6f135105802435f4f229027178fa204e9a961132 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sat, 10 Jul 2010 14:20:45 +0000 Subject: [PATCH] Merge iostreams, hash. Including disallowing implicit casts to `hash_value`. [SVN r63812] --- hash/doc/changes.qbk | 8 ++++++++ hash/test/Jamfile.v2 | 2 ++ hash/test/implicit_fail_test.cpp | 21 +++++++++++++++++++++ hash/test/shared_ptr_fail_test.cpp | 16 ++++++++++++++++ include/boost/functional/hash/hash.hpp | 13 +++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 hash/test/implicit_fail_test.cpp create mode 100644 hash/test/shared_ptr_fail_test.cpp diff --git a/hash/doc/changes.qbk b/hash/doc/changes.qbk index 4e0d0a9..bc6f4ca 100644 --- a/hash/doc/changes.qbk +++ b/hash/doc/changes.qbk @@ -106,4 +106,12 @@ Avoid hashing 0.5 and 0 to the same number. * Stop using deprecated `BOOST_HAS_*` macros. +[h2 Boost 1.44.0] + +* Prevent implicit conversions when calling `hash_value`. If you find + that the new version breaks your code, you can enable the old + behaviour by defining `BOOST_HASH_ALLOW_IMPLICIT_CASTS` - although + I would recommend that you update your code to work with the new + version. + [endsect] 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..e48bbcd --- /dev/null +++ b/hash/test/implicit_fail_test.cpp @@ -0,0 +1,21 @@ + +// Copyright 2010 Daniel James. +// 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 + +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); +} diff --git a/hash/test/shared_ptr_fail_test.cpp b/hash/test/shared_ptr_fail_test.cpp new file mode 100644 index 0000000..b0e045f --- /dev/null +++ b/hash/test/shared_ptr_fail_test.cpp @@ -0,0 +1,16 @@ + +// Copyright 2010 Daniel James. +// 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 + +// 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); +} diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index e85ca5a..54407d6 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,18 @@ namespace boost { +#if !defined(BOOST_HASH_ALLOW_IMPLICIT_CASTS) + + // 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; + } + +#endif + std::size_t hash_value(bool); std::size_t hash_value(char); std::size_t hash_value(unsigned char);