hash_variant.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //-----------------------------------------------------------------------------
  2. // boost variant/detail/hash_variant.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2011-2021 Antony Polukhin
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_HASH_VARIANT_FUNCTION_HPP
  12. #define BOOST_HASH_VARIANT_FUNCTION_HPP
  13. #if defined(_MSC_VER)
  14. # pragma once
  15. #endif
  16. #include <boost/variant/variant_fwd.hpp>
  17. #include <boost/variant/static_visitor.hpp>
  18. #include <boost/variant/apply_visitor.hpp>
  19. #include <boost/functional/hash_fwd.hpp>
  20. namespace boost {
  21. namespace detail { namespace variant {
  22. struct variant_hasher: public boost::static_visitor<std::size_t> {
  23. template <class T>
  24. std::size_t operator()(T const& val) const {
  25. boost::hash<T> hasher;
  26. return hasher(val);
  27. }
  28. };
  29. }}
  30. template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
  31. std::size_t hash_value(variant< BOOST_VARIANT_ENUM_PARAMS(T) > const& val) {
  32. std::size_t seed = boost::apply_visitor(detail::variant::variant_hasher(), val);
  33. hash_combine(seed, val.which());
  34. return seed;
  35. }
  36. }
  37. #endif