optional_hash.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2022 Andrzej Krzemienski.
  2. //
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/optional for documentation.
  8. //
  9. // You are welcome to contact the author at:
  10. // akrzemi1@gmail.com
  11. #ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_HASH_AJK_20MAY2022_HPP
  12. #define BOOST_OPTIONAL_DETAIL_OPTIONAL_HASH_AJK_20MAY2022_HPP
  13. #include <boost/optional/optional_fwd.hpp>
  14. #include <boost/config.hpp>
  15. #if !defined(BOOST_OPTIONAL_CONFIG_DO_NOT_SPECIALIZE_STD_HASH) && !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  16. #include <functional>
  17. namespace std
  18. {
  19. template <typename T>
  20. struct hash<boost::optional<T> >
  21. {
  22. typedef std::size_t result_type;
  23. typedef boost::optional<T> argument_type;
  24. BOOST_CONSTEXPR result_type operator()(const argument_type& arg) const {
  25. return arg ? std::hash<T>()(*arg) : result_type();
  26. }
  27. };
  28. template <typename T>
  29. struct hash<boost::optional<T&> >
  30. {
  31. typedef std::size_t result_type;
  32. typedef boost::optional<T&> argument_type;
  33. BOOST_CONSTEXPR result_type operator()(const argument_type& arg) const {
  34. return arg ? std::hash<T>()(*arg) : result_type();
  35. }
  36. };
  37. }
  38. #endif // !defined(BOOST_OPTIONAL_CONFIG_DO_NOT_SPECIALIZE_STD_HASH) && !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  39. #endif // header guard