pair.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*!
  2. @file
  3. Adapts `std::pair` for use with Hana.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_EXT_STD_PAIR_HPP
  9. #define BOOST_HANA_EXT_STD_PAIR_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/fwd/core/make.hpp>
  12. #include <boost/hana/fwd/core/tag_of.hpp>
  13. #include <boost/hana/fwd/first.hpp>
  14. #include <boost/hana/fwd/second.hpp>
  15. #include <utility>
  16. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  17. namespace std {
  18. //! @ingroup group-ext-std
  19. //! Adaptation of `std::pair` for Hana.
  20. //!
  21. //!
  22. //! Modeled concepts
  23. //! ----------------
  24. //! A `std::pair` models exactly the same concepts as a `hana::pair`.
  25. //! Please refer to the documentation of `hana::pair` for details.
  26. //!
  27. //! @include example/ext/std/pair.cpp
  28. template <typename First, typename Second>
  29. struct pair { };
  30. }
  31. #endif
  32. BOOST_HANA_NAMESPACE_BEGIN
  33. namespace ext { namespace std { struct pair_tag; }}
  34. template <typename First, typename Second>
  35. struct tag_of<std::pair<First, Second>> {
  36. using type = ext::std::pair_tag;
  37. };
  38. //////////////////////////////////////////////////////////////////////////
  39. // Product
  40. //////////////////////////////////////////////////////////////////////////
  41. template <>
  42. struct make_impl<ext::std::pair_tag> {
  43. template <typename X, typename Y>
  44. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  45. return std::make_pair(static_cast<X&&>(x),
  46. static_cast<Y&&>(y));
  47. }
  48. };
  49. template <>
  50. struct first_impl<ext::std::pair_tag> {
  51. template <typename T, typename U>
  52. static constexpr T const& apply(std::pair<T, U> const& p)
  53. { return p.first; }
  54. template <typename T, typename U>
  55. static constexpr T& apply(std::pair<T, U>& p)
  56. { return p.first; }
  57. template <typename T, typename U>
  58. static constexpr T&& apply(std::pair<T, U>&& p)
  59. { return static_cast<T&&>(p.first); }
  60. };
  61. template <>
  62. struct second_impl<ext::std::pair_tag> {
  63. template <typename T, typename U>
  64. static constexpr U const& apply(std::pair<T, U> const& p)
  65. { return p.second; }
  66. template <typename T, typename U>
  67. static constexpr U& apply(std::pair<T, U>& p)
  68. { return p.second; }
  69. template <typename T, typename U>
  70. static constexpr U&& apply(std::pair<T, U>&& p)
  71. { return static_cast<U&&>(p.second); }
  72. };
  73. BOOST_HANA_NAMESPACE_END
  74. #endif // !BOOST_HANA_EXT_STD_PAIR_HPP