Add boost::hash_value for std::nullptr_t. Refs #29.

This commit is contained in:
Peter Dimov
2022-11-27 19:50:49 +02:00
parent f98b943585
commit fe28cdbd1f
3 changed files with 57 additions and 0 deletions

View File

@@ -27,6 +27,7 @@
#include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/conjunction.hpp>
#include <boost/type_traits/is_union.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/describe/bases.hpp>
#include <boost/describe/members.hpp>
#include <boost/cstdint.hpp>
@@ -494,6 +495,19 @@ namespace boost
return seed;
}
#endif
// std::nullptr_t
#if !defined(BOOST_NO_CXX11_NULLPTR)
template <typename T>
typename boost::enable_if_<boost::is_same<T, std::nullptr_t>::value, std::size_t>::type
hash_value( T const& v )
{
return boost::hash_value( static_cast<void*>( v ) );
}
#endif
// std::optional

View File

@@ -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 ;

View File

@@ -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 <boost/container_hash/hash.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#include <cstddef>
#if defined(BOOST_NO_CXX11_NULLPTR)
BOOST_PRAGMA_MESSAGE( "Test skipped, BOOST_NO_CXX11_NULLPTR is defined" )
int main() {}
#else
template<class T> std::size_t hv( T const& x )
{
return boost::hash<T>()( 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