From 2553a5fbdc2d842f54b9becd607dcd6c6dff96ba Mon Sep 17 00:00:00 2001 From: Daniel James Date: Tue, 6 Jul 2010 23:32:37 +0000 Subject: [PATCH 01/22] Try preventing static casts when calling `hash_value`. [SVN r63716] --- include/boost/functional/hash/hash.hpp | 9 +++++++++ test/Jamfile.v2 | 2 ++ test/implicit_fail_test.cpp | 16 ++++++++++++++++ test/shared_ptr_fail_test.cpp | 11 +++++++++++ 4 files changed, 38 insertions(+) create mode 100644 test/implicit_fail_test.cpp create mode 100644 test/shared_ptr_fail_test.cpp 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); diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 4435dd2..c2d8084 100644 --- a/test/Jamfile.v2 +++ b/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/test/implicit_fail_test.cpp b/test/implicit_fail_test.cpp new file mode 100644 index 0000000..1dd0555 --- /dev/null +++ b/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/test/shared_ptr_fail_test.cpp b/test/shared_ptr_fail_test.cpp new file mode 100644 index 0000000..584739f --- /dev/null +++ b/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 From 38d131c158c4c680b4a89b6eee7ef375bfb0bf90 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 8 Jul 2010 20:48:30 +0000 Subject: [PATCH 02/22] Fix inspect issues. [SVN r63762] --- test/implicit_fail_test.cpp | 7 ++++++- test/shared_ptr_fail_test.cpp | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/test/implicit_fail_test.cpp b/test/implicit_fail_test.cpp index 1dd0555..e48bbcd 100644 --- a/test/implicit_fail_test.cpp +++ b/test/implicit_fail_test.cpp @@ -1,3 +1,8 @@ + +// 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 @@ -13,4 +18,4 @@ int main() { test::converts x; hash(x); -} \ No newline at end of file +} diff --git a/test/shared_ptr_fail_test.cpp b/test/shared_ptr_fail_test.cpp index 584739f..b0e045f 100644 --- a/test/shared_ptr_fail_test.cpp +++ b/test/shared_ptr_fail_test.cpp @@ -1,3 +1,8 @@ + +// 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 @@ -8,4 +13,4 @@ int main() { boost::shared_ptr x(new int(10)); hash(x); -} \ No newline at end of file +} From bbfb6fd32c405b18527f615be6c7d6c455ce5780 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sat, 10 Jul 2010 13:47:47 +0000 Subject: [PATCH 03/22] Release notes for hash. [SVN r63810] --- doc/changes.qbk | 8 ++++++++ include/boost/functional/hash/hash.hpp | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/doc/changes.qbk b/doc/changes.qbk index 4e0d0a9..bc6f4ca 100644 --- a/doc/changes.qbk +++ b/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/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index bd8d37f..54407d6 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -30,6 +30,8 @@ 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 @@ -38,6 +40,8 @@ namespace boost return 0; } +#endif + std::size_t hash_value(bool); std::size_t hash_value(char); std::size_t hash_value(unsigned char); From 906f6327069ec8aff234f26e3540ee133877a81c Mon Sep 17 00:00:00 2001 From: Daniel James Date: Wed, 14 Jul 2010 08:17:48 +0000 Subject: [PATCH 04/22] Actually, make the change to hash opt-in, rather than opt-out. It's a bit late to introduce a breaking change. [SVN r64007] --- doc/changes.qbk | 15 ++++++++++----- include/boost/functional/hash/hash.hpp | 2 +- test/Jamfile.v2 | 1 + 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/doc/changes.qbk b/doc/changes.qbk index bc6f4ca..eec4876 100644 --- a/doc/changes.qbk +++ b/doc/changes.qbk @@ -108,10 +108,15 @@ [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. +* Add option to prevent implicit conversions when calling `hash_value` by + defining `BOOST_HASH_NO_IMPLICIT_CASTS`. When using `boost::hash` + for a type that does not have `hash_value` declared but does have + an implicit conversion to a type that does, it would use that + implicit conversion to hash it. Which can sometimes go very wrong, + e.g. using a conversion to bool and only hashing to 2 possible + values. Since fixing this is a breaking change and was only + approached quite late in the release cycle with little discussion + it's opt-in for now. This, or something like it, will become the + default in a future version. [endsect] diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index 54407d6..24a78f3 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -30,7 +30,7 @@ namespace boost { -#if !defined(BOOST_HASH_ALLOW_IMPLICIT_CASTS) +#if defined(BOOST_HASH_NO_IMPLICIT_CASTS) // If you get a static assertion here, it's because hash_value // isn't declared for your type. diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index c2d8084..1b35eed 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -7,6 +7,7 @@ import testing ; project hash-tests : requirements + BOOST_HASH_NO_IMPLICIT_CASTS all intel:on intel:-strict-ansi From fc3b3863b4da1128a537ce7dbf5a659257c604b7 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Wed, 14 Jul 2010 08:28:04 +0000 Subject: [PATCH 05/22] Only include static_assert when necessary. [SVN r64009] --- include/boost/functional/hash/hash.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index 24a78f3..108d3ed 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -15,7 +15,10 @@ #include #include #include + +#if defined(BOOST_HASH_NO_IMPLICIT_CASTS) #include +#endif #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) #include From 577054de93c45090dc6faf3ff6d87efde3a25e7b Mon Sep 17 00:00:00 2001 From: Daniel James Date: Tue, 27 Jul 2010 19:18:53 +0000 Subject: [PATCH 06/22] Fix hashing pointers on 64-bit OpenVMS. Patch by Artyom. Refs #4477 [SVN r64397] --- include/boost/functional/hash/hash.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index 108d3ed..0eedf7f 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -209,9 +209,15 @@ namespace boost template std::size_t hash_value(T* v) #endif { +#if defined(__VMS) && __INITIAL_POINTER_SIZE == 64 + // for some reason ptrdiff_t on OpenVMS compiler with + // 64 bit is not 64 bit !!! + std::size_t x = static_cast( + reinterpret_cast(v)); +#else std::size_t x = static_cast( reinterpret_cast(v)); - +#endif return x + (x >> 3); } From 982b350d71698ac31a1b5a7ef642bad002387f85 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 14 Nov 2010 11:42:58 +0000 Subject: [PATCH 07/22] Remove some 'always_show_run_output' flags. [SVN r66566] --- test/Jamfile.v2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 1b35eed..de35142 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -25,8 +25,8 @@ test-suite functional/hash [ run hash_number_test.cpp ] [ run hash_pointer_test.cpp ] [ run hash_function_pointer_test.cpp ] - [ run hash_float_test.cpp : : : always_show_run_output ] - [ run hash_long_double_test.cpp : : : always_show_run_output ] + [ run hash_float_test.cpp ] + [ run hash_long_double_test.cpp ] [ run hash_string_test.cpp ] [ run hash_range_test.cpp ] [ run hash_custom_test.cpp ] From a2e947588dddaa6a741d7685632f5a72a49db859 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Tue, 7 Dec 2010 20:45:08 +0000 Subject: [PATCH 08/22] Import boostbook/quickbook in unordered and hash docs. [SVN r67091] --- doc/Jamfile.v2 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/Jamfile.v2 b/doc/Jamfile.v2 index 1aab1e9..044ee70 100644 --- a/doc/Jamfile.v2 +++ b/doc/Jamfile.v2 @@ -3,6 +3,9 @@ # 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) +using boostbook ; +using quickbook ; + xml hash : hash.qbk ; boostbook standalone : hash : boost.root=../../../.. From 482f038837bf213ca0823a069e6d1057e9925ed8 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sat, 11 Dec 2010 14:43:00 +0000 Subject: [PATCH 09/22] Avoid `-Wconversion` warnings in unordered & hash. [SVN r67170] --- doc/changes.qbk | 4 ++++ include/boost/functional/hash/detail/hash_float_generic.hpp | 4 ++-- test/Jamfile.v2 | 4 ++-- test/hash_complex_test.cpp | 2 +- test/hash_number_test.cpp | 3 ++- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/changes.qbk b/doc/changes.qbk index eec4876..eb6572f 100644 --- a/doc/changes.qbk +++ b/doc/changes.qbk @@ -119,4 +119,8 @@ it's opt-in for now. This, or something like it, will become the default in a future version. +[h2 Boost 1.46.0] + +* Avoid warning due with gcc's `-Wconversion` flag. + [endsect] diff --git a/include/boost/functional/hash/detail/hash_float_generic.hpp b/include/boost/functional/hash/detail/hash_float_generic.hpp index fdbf53f..1278c2f 100644 --- a/include/boost/functional/hash/detail/hash_float_generic.hpp +++ b/include/boost/functional/hash/detail/hash_float_generic.hpp @@ -53,7 +53,7 @@ namespace boost v = ldexp(v, limits::digits); std::size_t seed = static_cast(v); - v -= seed; + v -= static_cast(seed); // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1; std::size_t const length @@ -66,7 +66,7 @@ namespace boost { v = ldexp(v, limits::digits); std::size_t part = static_cast(v); - v -= part; + v -= static_cast(part); hash_float_combine(seed, part); } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index de35142..8985875 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -11,8 +11,8 @@ project hash-tests all intel:on intel:-strict-ansi - gcc:"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter" - darwin:"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter" + gcc:"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter -Wconversion" + darwin:"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter -Wconversion" msvc:on #gcc:on #darwin:on diff --git a/test/hash_complex_test.cpp b/test/hash_complex_test.cpp index a2a2dc2..67e2aff 100644 --- a/test/hash_complex_test.cpp +++ b/test/hash_complex_test.cpp @@ -69,7 +69,7 @@ void complex_float_tests(Float*) generic_complex_tests(complex(0.5,0)); generic_complex_tests(complex(25,0)); generic_complex_tests(complex(25,0)); - generic_complex_tests(complex(-67.5324535,56.23578678)); + generic_complex_tests(complex(static_cast(-67.5324535),static_cast(56.23578678))); } template diff --git a/test/hash_number_test.cpp b/test/hash_number_test.cpp index 91b9b92..1080860 100644 --- a/test/hash_number_test.cpp +++ b/test/hash_number_test.cpp @@ -113,7 +113,8 @@ void poor_quality_tests(T*) if(T(1) != T(2)) BOOST_TEST(x1(T(1)) != x2(T(2))); if((limits::max)() != (limits::max)() - 1) - BOOST_TEST(x1((limits::max)()) != x2((limits::max)() - 1)); + BOOST_TEST(x1(static_cast((limits::max)())) + != x2(static_cast((limits::max)() - 1))); } void bool_test() From ed598f865e181cd4fc6b3da7fb4d2d911a628c15 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Mon, 3 Jan 2011 12:43:34 +0000 Subject: [PATCH 10/22] Fix tabs and files without copyright. [SVN r67612] --- include/boost/functional/hash/hash.hpp | 4 ++-- test/hash_number_test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index 0eedf7f..389804c 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -210,8 +210,8 @@ namespace boost #endif { #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64 - // for some reason ptrdiff_t on OpenVMS compiler with - // 64 bit is not 64 bit !!! + // for some reason ptrdiff_t on OpenVMS compiler with + // 64 bit is not 64 bit !!! std::size_t x = static_cast( reinterpret_cast(v)); #else diff --git a/test/hash_number_test.cpp b/test/hash_number_test.cpp index 1080860..b989d22 100644 --- a/test/hash_number_test.cpp +++ b/test/hash_number_test.cpp @@ -114,7 +114,7 @@ void poor_quality_tests(T*) BOOST_TEST(x1(T(1)) != x2(T(2))); if((limits::max)() != (limits::max)() - 1) BOOST_TEST(x1(static_cast((limits::max)())) - != x2(static_cast((limits::max)() - 1))); + != x2(static_cast((limits::max)() - 1))); } void bool_test() From ad614b3d5f1c3e7eb31a08e0624e04f65af3e650 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Tue, 4 Jan 2011 23:30:22 +0000 Subject: [PATCH 11/22] Move tests for container_fwd.hpp into detail. [SVN r67667] --- test/Jamfile.v2 | 6 -- test/container_fwd_test.cpp | 114 --------------------------------- test/container_no_fwd_test.cpp | 14 ---- 3 files changed, 134 deletions(-) delete mode 100644 test/container_fwd_test.cpp delete mode 100644 test/container_no_fwd_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 8985875..f7fe7d9 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -43,12 +43,6 @@ test-suite functional/hash [ run link_test.cpp link_test_2.cpp ] [ run link_ext_test.cpp link_no_ext_test.cpp ] [ run extensions_hpp_test.cpp ] - [ run container_fwd_test.cpp ] - [ run container_fwd_test.cpp : : - : gcc:_GLIBCXX_DEBUG - darwin:_GLIBCXX_DEBUG - : container_fwd_gcc_debug ] - [ 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 ] diff --git a/test/container_fwd_test.cpp b/test/container_fwd_test.cpp deleted file mode 100644 index ec8cc54..0000000 --- a/test/container_fwd_test.cpp +++ /dev/null @@ -1,114 +0,0 @@ - -// Copyright 2005-2009 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 "./config.hpp" - -#include - -#if BOOST_WORKAROUND(__GNUC__, < 3) && \ - !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION) -template -static void test( - std::basic_string, Allocator> const&) -{ -} -#else -template -static void test( - std::basic_string, Allocator> const&) -{ -} -#endif - -template -static void test(std::deque const&) -{ -} - -template -static void test(std::list const&) -{ -} - -template -static void test(std::vector const&) -{ -} - -template -static void test(std::map const&) -{ -} - -template -static void test(std::multimap const&) -{ -} - -template -static void test(std::set const&) -{ -} - -template -static void test(std::multiset const&) -{ -} - -template -static void test(std::bitset const&) -{ -} - -template -static void test(std::complex const&) -{ -} - -template -static void test(std::pair const&) -{ -} - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int main() -{ - std::deque x1; - std::list x2; - std::vector x3; - std::vector x4; - std::map x5; - std::multimap x6; - std::set x7; - std::multiset > x8; - std::bitset<10> x9; - std::string x10; - std::complex x11; - std::pair, char***> x12; - - test(x1); - test(x2); - test(x3); - test(x4); - test(x5); - test(x6); - test(x7); - test(x8); - test(x9); - test(x10); - test(x11); - test(x12); - - return 0; -} diff --git a/test/container_no_fwd_test.cpp b/test/container_no_fwd_test.cpp deleted file mode 100644 index 9da09da..0000000 --- a/test/container_no_fwd_test.cpp +++ /dev/null @@ -1,14 +0,0 @@ - -// 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) - -#define BOOST_DETAIL_NO_CONTAINER_FWD - -#include - -int main() -{ - std::set x; - std::vector y; -} From 7dc95d044d2cba7ce5cdd5f902281ba11c049bd5 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Fri, 14 Jan 2011 03:13:39 +0000 Subject: [PATCH 12/22] Support typeindex in hash. Refs #4756. [SVN r68145] --- doc/ref.xml | 34 ++++++++++++++++++ include/boost/functional/hash/hash.hpp | 20 +++++++++++ test/Jamfile.v2 | 1 + test/hash_type_index_test.cpp | 48 ++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 test/hash_type_index_test.cpp diff --git a/doc/ref.xml b/doc/ref.xml index 47a09d5..318bf85 100644 --- a/doc/ref.xml +++ b/doc/ref.xml @@ -406,6 +406,29 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + + + std::type_index + + + std::size_t + + std::type_index + + + val.hash_code() + + Doesn't throw + + + + Only available if it's in your standard library and Boost.Config + is aware of it. + + + +