mirror of
https://github.com/boostorg/variant.git
synced 2026-01-19 04:42:16 +00:00
Add std::hash specialization for variant (refs #49)
This commit is contained in:
@@ -140,6 +140,13 @@
|
||||
</description>
|
||||
</macro>
|
||||
|
||||
<macro name="BOOST_VARIANT_DO_NOT_SPECIALIZE_STD_HASH">
|
||||
<purpose>
|
||||
<simpara>Define this macro if you do not with to have a <code><classname>std::hash</classname></code> specialization for
|
||||
<code><classname>boost::variant</classname></code>.</simpara>
|
||||
</purpose>
|
||||
</macro>
|
||||
|
||||
<macro name="BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT">
|
||||
<purpose>
|
||||
<simpara>Indicates
|
||||
|
||||
46
include/boost/variant/detail/std_hash.hpp
Normal file
46
include/boost/variant/detail/std_hash.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// boost variant/detail/std_hash.hpp header file
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2018-2019 Antony Polukhin
|
||||
//
|
||||
// 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)
|
||||
|
||||
|
||||
#ifndef BOOST_VARIANT_DETAIL_STD_HASH_HPP
|
||||
#define BOOST_VARIANT_DETAIL_STD_HASH_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#ifdef BOOST_HAS_PRAGMA_ONCE
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/variant/variant_fwd.hpp>
|
||||
#include <boost/variant/detail/hash_variant.hpp>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// macro BOOST_VARIANT_DO_NOT_SPECIALIZE_STD_HASH
|
||||
//
|
||||
// Define this macro if you do not with to have a std::hash specialization for
|
||||
// boost::variant.
|
||||
|
||||
#if !defined(BOOST_VARIANT_DO_NOT_SPECIALIZE_STD_HASH) && !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
|
||||
|
||||
#include <functional> // for std::hash
|
||||
|
||||
namespace std {
|
||||
template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
|
||||
struct hash<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) > > {
|
||||
std::size_t operator()(const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& val) const {
|
||||
return ::boost::hash_value(val);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // #if !defined(BOOST_VARIANT_DO_NOT_SPECIALIZE_STD_HASH) && !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
|
||||
|
||||
#endif // BOOST_VARIANT_DETAIL_STD_HASH_HPP
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <boost/variant/detail/over_sequence.hpp>
|
||||
#include <boost/variant/detail/visitation_impl.hpp>
|
||||
#include <boost/variant/detail/hash_variant.hpp>
|
||||
#include <boost/variant/detail/std_hash.hpp>
|
||||
|
||||
#include <boost/variant/detail/move.hpp>
|
||||
|
||||
|
||||
@@ -1,16 +1,32 @@
|
||||
// Copyright (c) 2011-2018
|
||||
// Antony Polukhin
|
||||
// Copyright (c) 2011-2019 Antony Polukhin
|
||||
//
|
||||
// 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 "boost/core/lightweight_test.hpp"
|
||||
|
||||
#include "boost/config.hpp"
|
||||
#include "boost/variant.hpp"
|
||||
#include "boost/functional/hash/hash.hpp"
|
||||
|
||||
void run()
|
||||
{
|
||||
#if !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET) && !defined(BOOST_VARIANT_DO_NOT_SPECIALIZE_STD_HASH)
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
void test_std_hash() {
|
||||
std::unordered_set<boost::variant<int, bool> > us;
|
||||
us.insert(1);
|
||||
us.insert(true);
|
||||
BOOST_TEST(us.size() == 2);
|
||||
}
|
||||
|
||||
#else
|
||||
void test_std_hash() {}
|
||||
#endif
|
||||
|
||||
|
||||
void run() {
|
||||
typedef boost::variant<bool, int, unsigned int, char> variant_type;
|
||||
boost::hash<variant_type> hasher;
|
||||
|
||||
@@ -30,9 +46,9 @@ void run()
|
||||
BOOST_TEST(hasher(char_variant2) == hasher(char_variant2));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
run();
|
||||
test_std_hash();
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user