pair.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. @file
  3. Forward declares `boost::hana::pair`.
  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_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. //! @note
  23. //! When you use a container, remember not to make assumptions about its
  24. //! representation, unless the documentation gives you those guarantees.
  25. //! More details [in the tutorial](@ref tutorial-containers-types).
  26. //!
  27. //!
  28. //! Modeled concepts
  29. //! ----------------
  30. //! 1. `Comparable`\n
  31. //! Two pairs `(x, y)` and `(x', y')` are equal if and only if both
  32. //! `x == x'` and `y == y'`.
  33. //! @include example/pair/comparable.cpp
  34. //!
  35. //! 2. `Orderable`\n
  36. //! Pairs are ordered as-if they were 2-element tuples, using a
  37. //! lexicographical ordering.
  38. //! @include example/pair/orderable.cpp
  39. //!
  40. //! 3. `Foldable`\n
  41. //! Folding a pair is equivalent to folding a 2-element tuple. In other
  42. //! words:
  43. //! @code
  44. //! fold_left(make_pair(x, y), s, f) == f(f(s, x), y)
  45. //! fold_right(make_pair(x, y), s, f) == f(x, f(y, s))
  46. //! @endcode
  47. //! Example:
  48. //! @include example/pair/foldable.cpp
  49. //!
  50. //! 4. `Product`\n
  51. //! The model of `Product` is the simplest one possible; the first element
  52. //! of a pair `(x, y)` is `x`, and its second element is `y`.
  53. //! @include example/pair/product.cpp
  54. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  55. template <typename First, typename Second>
  56. struct pair {
  57. //! Default constructs the `pair`. Only exists when both elements
  58. //! of the pair are default constructible.
  59. constexpr pair();
  60. //! Initialize each element of the pair with the corresponding element.
  61. //! Only exists when both elements of the pair are copy-constructible.
  62. constexpr pair(First const& first, Second const& second);
  63. //! Initialize both elements of the pair by perfect-forwarding the
  64. //! corresponding argument. Only exists when both arguments are
  65. //! implicitly-convertible to the corresponding element of the pair.
  66. template <typename T, typename U>
  67. constexpr pair(T&& t, U&& u);
  68. //! Copy-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> const& other);
  73. //! Move-initialize a pair from another pair. Only exists when both
  74. //! elements of the source pair are implicitly convertible to the
  75. //! corresponding element of the constructed pair.
  76. template <typename T, typename U>
  77. constexpr pair(pair<T, U>&& other);
  78. //! Assign a pair to another pair. Only exists when both elements
  79. //! of the destination pair are assignable from the corresponding
  80. //! element in the source pair.
  81. template <typename T, typename U>
  82. constexpr pair& operator=(pair<T, U> const& other);
  83. //! Move-assign a pair to another pair. Only exists when both elements
  84. //! of the destination pair are move-assignable from the corresponding
  85. //! element in the source pair.
  86. template <typename T, typename U>
  87. constexpr pair& operator=(pair<T, U>&& other);
  88. //! Equivalent to `hana::equal`
  89. template <typename X, typename Y>
  90. friend constexpr auto operator==(X&& x, Y&& y);
  91. //! Equivalent to `hana::not_equal`
  92. template <typename X, typename Y>
  93. friend constexpr auto operator!=(X&& x, Y&& y);
  94. //! Equivalent to `hana::less`
  95. template <typename X, typename Y>
  96. friend constexpr auto operator<(X&& x, Y&& y);
  97. //! Equivalent to `hana::greater`
  98. template <typename X, typename Y>
  99. friend constexpr auto operator>(X&& x, Y&& y);
  100. //! Equivalent to `hana::less_equal`
  101. template <typename X, typename Y>
  102. friend constexpr auto operator<=(X&& x, Y&& y);
  103. //! Equivalent to `hana::greater_equal`
  104. template <typename X, typename Y>
  105. friend constexpr auto operator>=(X&& x, Y&& y);
  106. };
  107. #else
  108. template <typename First, typename Second>
  109. struct pair;
  110. #endif
  111. //! Tag representing `hana::pair`.
  112. //! @relates hana::pair
  113. struct pair_tag { };
  114. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  115. //! Creates a `hana::pair` with the given elements.
  116. //! @relates hana::pair
  117. //!
  118. //!
  119. //! Example
  120. //! -------
  121. //! @include example/pair/make.cpp
  122. template <>
  123. constexpr auto make<pair_tag> = [](auto&& first, auto&& second)
  124. -> hana::pair<std::decay_t<decltype(first)>, std::decay_t<decltype(second)>>
  125. {
  126. return {forwarded(first), forwarded(second)};
  127. };
  128. #endif
  129. //! Alias to `make<pair_tag>`; provided for convenience.
  130. //! @relates hana::pair
  131. //!
  132. //! Example
  133. //! -------
  134. //! @include example/pair/make.cpp
  135. constexpr auto make_pair = make<pair_tag>;
  136. BOOST_HANA_NAMESPACE_END
  137. #endif // !BOOST_HANA_FWD_PAIR_HPP