pair.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*!
  2. @file
  3. Forward declares `boost::hana::pair`.
  4. @copyright Louis Dionne 2013-2016
  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_FWD_PAIR_HPP
  9. #define BOOST_HANA_FWD_PAIR_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/fwd/core/make.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! @ingroup group-datatypes
  14. //! Generic container for two elements.
  15. //!
  16. //! `hana::pair` is conceptually the same as `std::pair`. However,
  17. //! `hana::pair` automatically compresses the storage of empty types,
  18. //! and as a result it does not have the `.first` and `.second` members.
  19. //! Instead, one must use the `hana::first` and `hana::second` free
  20. //! functions to access the elements of a pair.
  21. //!
  22. //!
  23. //! Modeled concepts
  24. //! ----------------
  25. //! 1. `Comparable`\n
  26. //! Two pairs `(x, y)` and `(x', y')` are equal if and only if both
  27. //! `x == x'` and `y == y'`.
  28. //! @include example/pair/comparable.cpp
  29. //!
  30. //! 2. `Orderable`\n
  31. //! Pairs are ordered as-if they were 2-element tuples, using a
  32. //! lexicographical ordering.
  33. //! @include example/pair/orderable.cpp
  34. //!
  35. //! 3. `Foldable`\n
  36. //! Folding a pair is equivalent to folding a 2-element tuple. In other
  37. //! words:
  38. //! @code
  39. //! fold_left(make_pair(x, y), s, f) == f(f(s, x), y)
  40. //! fold_right(make_pair(x, y), s, f) == f(x, f(y, s))
  41. //! @endcode
  42. //! Example:
  43. //! @include example/pair/foldable.cpp
  44. //!
  45. //! 4. `Product`\n
  46. //! The model of `Product` is the simplest one possible; the first element
  47. //! of a pair `(x, y)` is `x`, and its second element is `y`.
  48. //! @include example/pair/product.cpp
  49. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  50. template <typename First, typename Second>
  51. struct pair {
  52. //! Default constructs the `pair`. Only exists when both elements
  53. //! of the pair are default constructible.
  54. constexpr pair();
  55. //! Initialize each element of the pair with the corresponding element.
  56. //! Only exists when both elements of the pair are copy-constructible.
  57. constexpr pair(First const& first, Second const& second);
  58. //! Initialize both elements of the pair by perfect-forwarding the
  59. //! corresponding argument. Only exists when both arguments are
  60. //! implicitly-convertible to the corresponding element of the pair.
  61. template <typename T, typename U>
  62. constexpr pair(T&& t, U&& u);
  63. //! Copy-initialize a pair from another pair. Only exists when both
  64. //! elements of the source pair are implicitly convertible to the
  65. //! corresponding element of the constructed pair.
  66. template <typename T, typename U>
  67. constexpr pair(pair<T, U> const& other);
  68. //! Move-initialize a pair from another pair. Only exists when both
  69. //! elements of the source pair are implicitly convertible to the
  70. //! corresponding element of the constructed pair.
  71. template <typename T, typename U>
  72. constexpr pair(pair<T, U>&& other);
  73. //! Assign a pair to another pair. Only exists when both elements
  74. //! of the destination pair are assignable from the corresponding
  75. //! element in the source pair.
  76. template <typename T, typename U>
  77. constexpr pair& operator=(pair<T, U> const& other);
  78. //! Move-assign a pair to another pair. Only exists when both elements
  79. //! of the destination pair are move-assignable from the corresponding
  80. //! element in the source pair.
  81. template <typename T, typename U>
  82. constexpr pair& operator=(pair<T, U>&& other);
  83. //! Equivalent to `hana::equal`
  84. template <typename X, typename Y>
  85. friend constexpr auto operator==(X&& x, Y&& y);
  86. //! Equivalent to `hana::not_equal`
  87. template <typename X, typename Y>
  88. friend constexpr auto operator!=(X&& x, Y&& y);
  89. //! Equivalent to `hana::less`
  90. template <typename X, typename Y>
  91. friend constexpr auto operator<(X&& x, Y&& y);
  92. //! Equivalent to `hana::greater`
  93. template <typename X, typename Y>
  94. friend constexpr auto operator>(X&& x, Y&& y);
  95. //! Equivalent to `hana::less_equal`
  96. template <typename X, typename Y>
  97. friend constexpr auto operator<=(X&& x, Y&& y);
  98. //! Equivalent to `hana::greater_equal`
  99. template <typename X, typename Y>
  100. friend constexpr auto operator>=(X&& x, Y&& y);
  101. };
  102. #else
  103. template <typename First, typename Second>
  104. struct pair;
  105. #endif
  106. //! Tag representing `hana::pair`.
  107. //! @relates hana::pair
  108. struct pair_tag { };
  109. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  110. //! Creates a `hana::pair` with the given elements.
  111. //! @relates hana::pair
  112. //!
  113. //!
  114. //! Example
  115. //! -------
  116. //! @include example/pair/make.cpp
  117. template <>
  118. constexpr auto make<pair_tag> = [](auto&& first, auto&& second)
  119. -> hana::pair<std::decay_t<decltype(first)>, std::decay_t<decltype(second)>>
  120. {
  121. return {forwarded(first), forwarded(second)};
  122. };
  123. #endif
  124. //! Alias to `make<pair_tag>`; provided for convenience.
  125. //! @relates hana::pair
  126. //!
  127. //! Example
  128. //! -------
  129. //! @include example/pair/make.cpp
  130. constexpr auto make_pair = make<pair_tag>;
  131. BOOST_HANA_NAMESPACE_END
  132. #endif // !BOOST_HANA_FWD_PAIR_HPP