From fe28cdbd1f31c35f68f95d8a1a38edb30960eb43 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 27 Nov 2022 19:50:49 +0200 Subject: [PATCH] Add boost::hash_value for std::nullptr_t. Refs #29. --- include/boost/container_hash/hash.hpp | 14 +++++++++ test/Jamfile.v2 | 2 ++ test/hash_nullptr_test.cpp | 41 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 test/hash_nullptr_test.cpp diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index 306c279..10eb18a 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -494,6 +495,19 @@ namespace boost return seed; } +#endif + + // std::nullptr_t + +#if !defined(BOOST_NO_CXX11_NULLPTR) + + template + typename boost::enable_if_::value, std::size_t>::type + hash_value( T const& v ) + { + return boost::hash_value( static_cast( v ) ); + } + #endif // std::optional diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index b5985cc..7b2eccc 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -115,3 +115,5 @@ run described_class_test.cpp run hash_is_avalanching_test.cpp ; run hash_is_avalanching_test2.cpp ; + +run hash_nullptr_test.cpp ; diff --git a/test/hash_nullptr_test.cpp b/test/hash_nullptr_test.cpp new file mode 100644 index 0000000..05a921c --- /dev/null +++ b/test/hash_nullptr_test.cpp @@ -0,0 +1,41 @@ +// Copyright 2022 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX11_NULLPTR) + +BOOST_PRAGMA_MESSAGE( "Test skipped, BOOST_NO_CXX11_NULLPTR is defined" ) +int main() {} + +#else + +template std::size_t hv( T const& x ) +{ + return boost::hash()( x ); +} + +int main() +{ + { + BOOST_TEST_EQ( hv((void*)0), hv(nullptr) ); + } + + { + int x = 0; + + BOOST_TEST_EQ( hv((int*)0), hv(nullptr) ); + BOOST_TEST_NE( hv(&x), hv(nullptr) ); + + (void)x; + } + + return boost::report_errors(); +} + +#endif