pair.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___UTILITY_PAIR_H
  9. #define _LIBCPP___UTILITY_PAIR_H
  10. #include <__compare/common_comparison_category.h>
  11. #include <__compare/synth_three_way.h>
  12. #include <__concepts/different_from.h>
  13. #include <__config>
  14. #include <__fwd/array.h>
  15. #include <__fwd/pair.h>
  16. #include <__fwd/tuple.h>
  17. #include <__tuple/sfinae_helpers.h>
  18. #include <__tuple/tuple_element.h>
  19. #include <__tuple/tuple_indices.h>
  20. #include <__tuple/tuple_like_no_subrange.h>
  21. #include <__tuple/tuple_size.h>
  22. #include <__type_traits/common_reference.h>
  23. #include <__type_traits/common_type.h>
  24. #include <__type_traits/conditional.h>
  25. #include <__type_traits/decay.h>
  26. #include <__type_traits/integral_constant.h>
  27. #include <__type_traits/is_assignable.h>
  28. #include <__type_traits/is_constructible.h>
  29. #include <__type_traits/is_convertible.h>
  30. #include <__type_traits/is_implicitly_default_constructible.h>
  31. #include <__type_traits/is_nothrow_assignable.h>
  32. #include <__type_traits/is_nothrow_constructible.h>
  33. #include <__type_traits/is_same.h>
  34. #include <__type_traits/is_swappable.h>
  35. #include <__type_traits/nat.h>
  36. #include <__type_traits/remove_cvref.h>
  37. #include <__type_traits/unwrap_ref.h>
  38. #include <__utility/declval.h>
  39. #include <__utility/forward.h>
  40. #include <__utility/move.h>
  41. #include <__utility/piecewise_construct.h>
  42. #include <cstddef>
  43. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  44. # pragma GCC system_header
  45. #endif
  46. _LIBCPP_PUSH_MACROS
  47. #include <__undef_macros>
  48. _LIBCPP_BEGIN_NAMESPACE_STD
  49. template <class, class>
  50. struct __non_trivially_copyable_base {
  51. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __non_trivially_copyable_base() _NOEXCEPT {}
  52. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
  53. __non_trivially_copyable_base(__non_trivially_copyable_base const&) _NOEXCEPT {}
  54. };
  55. template <class _T1, class _T2>
  56. struct _LIBCPP_TEMPLATE_VIS pair
  57. #if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
  58. : private __non_trivially_copyable_base<_T1, _T2>
  59. #endif
  60. {
  61. using first_type = _T1;
  62. using second_type = _T2;
  63. _T1 first;
  64. _T2 second;
  65. _LIBCPP_HIDE_FROM_ABI pair(pair const&) = default;
  66. _LIBCPP_HIDE_FROM_ABI pair(pair&&) = default;
  67. #ifdef _LIBCPP_CXX03_LANG
  68. _LIBCPP_HIDE_FROM_ABI pair() : first(), second() {}
  69. _LIBCPP_HIDE_FROM_ABI pair(_T1 const& __t1, _T2 const& __t2) : first(__t1), second(__t2) {}
  70. template <class _U1, class _U2>
  71. _LIBCPP_HIDE_FROM_ABI pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
  72. _LIBCPP_HIDE_FROM_ABI pair& operator=(pair const& __p) {
  73. first = __p.first;
  74. second = __p.second;
  75. return *this;
  76. }
  77. // Extension: This is provided in C++03 because it allows properly handling the
  78. // assignment to a pair containing references, which would be a hard
  79. // error otherwise.
  80. template <
  81. class _U1,
  82. class _U2,
  83. __enable_if_t<is_assignable<first_type&, _U1 const&>::value && is_assignable<second_type&, _U2 const&>::value,
  84. int> = 0>
  85. _LIBCPP_HIDE_FROM_ABI pair& operator=(pair<_U1, _U2> const& __p) {
  86. first = __p.first;
  87. second = __p.second;
  88. return *this;
  89. }
  90. #else
  91. struct _CheckArgs {
  92. template <int&...>
  93. static _LIBCPP_HIDE_FROM_ABI constexpr bool __enable_implicit_default() {
  94. return __is_implicitly_default_constructible<_T1>::value && __is_implicitly_default_constructible<_T2>::value;
  95. }
  96. template <int&...>
  97. static _LIBCPP_HIDE_FROM_ABI constexpr bool __enable_default() {
  98. return is_default_constructible<_T1>::value && is_default_constructible<_T2>::value;
  99. }
  100. template <class _U1, class _U2>
  101. static _LIBCPP_HIDE_FROM_ABI constexpr bool __is_pair_constructible() {
  102. return is_constructible<first_type, _U1>::value && is_constructible<second_type, _U2>::value;
  103. }
  104. template <class _U1, class _U2>
  105. static _LIBCPP_HIDE_FROM_ABI constexpr bool __is_implicit() {
  106. return is_convertible<_U1, first_type>::value && is_convertible<_U2, second_type>::value;
  107. }
  108. };
  109. template <bool _MaybeEnable>
  110. using _CheckArgsDep _LIBCPP_NODEBUG =
  111. typename conditional< _MaybeEnable, _CheckArgs, __check_tuple_constructor_fail>::type;
  112. template <bool _Dummy = true, __enable_if_t<_CheckArgsDep<_Dummy>::__enable_default(), int> = 0>
  113. explicit(!_CheckArgsDep<_Dummy>::__enable_implicit_default()) _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR pair()
  114. _NOEXCEPT_(
  115. is_nothrow_default_constructible<first_type>::value&& is_nothrow_default_constructible<second_type>::value)
  116. : first(), second() {}
  117. template <bool _Dummy = true,
  118. __enable_if_t<_CheckArgsDep<_Dummy>::template __is_pair_constructible<_T1 const&, _T2 const&>(), int> = 0>
  119. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(
  120. !_CheckArgsDep<_Dummy>::template __is_implicit<_T1 const&, _T2 const&>()) pair(_T1 const& __t1, _T2 const& __t2)
  121. _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value&& is_nothrow_copy_constructible<second_type>::value)
  122. : first(__t1), second(__t2) {}
  123. template <
  124. # if _LIBCPP_STD_VER >= 23 // http://wg21.link/P1951
  125. class _U1 = _T1,
  126. class _U2 = _T2,
  127. # else
  128. class _U1,
  129. class _U2,
  130. # endif
  131. __enable_if_t<_CheckArgs::template __is_pair_constructible<_U1, _U2>(), int> = 0 >
  132. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(!_CheckArgs::template __is_implicit<_U1, _U2>())
  133. pair(_U1&& __u1, _U2&& __u2)
  134. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
  135. is_nothrow_constructible<second_type, _U2>::value))
  136. : first(std::forward<_U1>(__u1)), second(std::forward<_U2>(__u2)) {
  137. }
  138. # if _LIBCPP_STD_VER >= 23
  139. template <class _U1, class _U2, __enable_if_t<_CheckArgs::template __is_pair_constructible<_U1&, _U2&>(), int> = 0>
  140. _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_CheckArgs::template __is_implicit<_U1&, _U2&>())
  141. pair(pair<_U1, _U2>& __p) noexcept((is_nothrow_constructible<first_type, _U1&>::value &&
  142. is_nothrow_constructible<second_type, _U2&>::value))
  143. : first(__p.first), second(__p.second) {}
  144. # endif
  145. template <class _U1,
  146. class _U2,
  147. __enable_if_t<_CheckArgs::template __is_pair_constructible<_U1 const&, _U2 const&>(), int> = 0>
  148. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(
  149. !_CheckArgs::template __is_implicit<_U1 const&, _U2 const&>()) pair(pair<_U1, _U2> const& __p)
  150. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
  151. is_nothrow_constructible<second_type, _U2 const&>::value))
  152. : first(__p.first), second(__p.second) {}
  153. template <class _U1, class _U2, __enable_if_t<_CheckArgs::template __is_pair_constructible<_U1, _U2>(), int> = 0>
  154. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(!_CheckArgs::template __is_implicit<_U1, _U2>())
  155. pair(pair<_U1, _U2>&& __p)
  156. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
  157. is_nothrow_constructible<second_type, _U2&&>::value))
  158. : first(std::forward<_U1>(__p.first)), second(std::forward<_U2>(__p.second)) {}
  159. # if _LIBCPP_STD_VER >= 23
  160. template <class _U1,
  161. class _U2,
  162. __enable_if_t<_CheckArgs::template __is_pair_constructible<const _U1&&, const _U2&&>(), int> = 0>
  163. _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_CheckArgs::template __is_implicit<const _U1&&, const _U2&&>())
  164. pair(const pair<_U1, _U2>&& __p) noexcept(is_nothrow_constructible<first_type, const _U1&&>::value &&
  165. is_nothrow_constructible<second_type, const _U2&&>::value)
  166. : first(std::move(__p.first)), second(std::move(__p.second)) {}
  167. # endif
  168. # if _LIBCPP_STD_VER >= 23
  169. // TODO: Remove this workaround in LLVM 20. The bug got fixed in Clang 18.
  170. // This is a workaround for http://llvm.org/PR60710. We should be able to remove it once Clang is fixed.
  171. template <class _PairLike>
  172. _LIBCPP_HIDE_FROM_ABI static constexpr bool __pair_like_explicit_wknd() {
  173. if constexpr (__pair_like_no_subrange<_PairLike>) {
  174. return !is_convertible_v<decltype(std::get<0>(std::declval<_PairLike&&>())), first_type> ||
  175. !is_convertible_v<decltype(std::get<1>(std::declval<_PairLike&&>())), second_type>;
  176. }
  177. return false;
  178. }
  179. template <__pair_like_no_subrange _PairLike>
  180. requires(is_constructible_v<first_type, decltype(std::get<0>(std::declval<_PairLike &&>()))> &&
  181. is_constructible_v<second_type, decltype(std::get<1>(std::declval<_PairLike &&>()))>)
  182. _LIBCPP_HIDE_FROM_ABI constexpr explicit(__pair_like_explicit_wknd<_PairLike>()) pair(_PairLike&& __p)
  183. : first(std::get<0>(std::forward<_PairLike>(__p))), second(std::get<1>(std::forward<_PairLike>(__p))) {}
  184. # endif
  185. template <class... _Args1, class... _Args2>
  186. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  187. pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args, tuple<_Args2...> __second_args) _NOEXCEPT_(
  188. is_nothrow_constructible<first_type, _Args1...>::value&& is_nothrow_constructible<second_type, _Args2...>::value)
  189. : pair(__pc,
  190. __first_args,
  191. __second_args,
  192. typename __make_tuple_indices<sizeof...(_Args1)>::type(),
  193. typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
  194. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair&
  195. operator=(__conditional_t< is_copy_assignable<first_type>::value && is_copy_assignable<second_type>::value,
  196. pair,
  197. __nat> const& __p)
  198. _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value&& is_nothrow_copy_assignable<second_type>::value) {
  199. first = __p.first;
  200. second = __p.second;
  201. return *this;
  202. }
  203. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair& operator=(
  204. __conditional_t< is_move_assignable<first_type>::value && is_move_assignable<second_type>::value, pair, __nat>&&
  205. __p)
  206. _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value&& is_nothrow_move_assignable<second_type>::value) {
  207. first = std::forward<first_type>(__p.first);
  208. second = std::forward<second_type>(__p.second);
  209. return *this;
  210. }
  211. template <
  212. class _U1,
  213. class _U2,
  214. __enable_if_t<is_assignable<first_type&, _U1 const&>::value && is_assignable<second_type&, _U2 const&>::value,
  215. int> = 0>
  216. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair& operator=(pair<_U1, _U2> const& __p) {
  217. first = __p.first;
  218. second = __p.second;
  219. return *this;
  220. }
  221. template <class _U1,
  222. class _U2,
  223. __enable_if_t<is_assignable<first_type&, _U1>::value && is_assignable<second_type&, _U2>::value, int> = 0>
  224. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair& operator=(pair<_U1, _U2>&& __p) {
  225. first = std::forward<_U1>(__p.first);
  226. second = std::forward<_U2>(__p.second);
  227. return *this;
  228. }
  229. # if _LIBCPP_STD_VER >= 23
  230. _LIBCPP_HIDE_FROM_ABI constexpr const pair& operator=(pair const& __p) const
  231. noexcept(is_nothrow_copy_assignable_v<const first_type> && is_nothrow_copy_assignable_v<const second_type>)
  232. requires(is_copy_assignable_v<const first_type> && is_copy_assignable_v<const second_type>)
  233. {
  234. first = __p.first;
  235. second = __p.second;
  236. return *this;
  237. }
  238. _LIBCPP_HIDE_FROM_ABI constexpr const pair& operator=(pair&& __p) const
  239. noexcept(is_nothrow_assignable_v<const first_type&, first_type> &&
  240. is_nothrow_assignable_v<const second_type&, second_type>)
  241. requires(is_assignable_v<const first_type&, first_type> && is_assignable_v<const second_type&, second_type>)
  242. {
  243. first = std::forward<first_type>(__p.first);
  244. second = std::forward<second_type>(__p.second);
  245. return *this;
  246. }
  247. template <class _U1, class _U2>
  248. _LIBCPP_HIDE_FROM_ABI constexpr const pair& operator=(const pair<_U1, _U2>& __p) const
  249. requires(is_assignable_v<const first_type&, const _U1&> && is_assignable_v<const second_type&, const _U2&>)
  250. {
  251. first = __p.first;
  252. second = __p.second;
  253. return *this;
  254. }
  255. template <class _U1, class _U2>
  256. _LIBCPP_HIDE_FROM_ABI constexpr const pair& operator=(pair<_U1, _U2>&& __p) const
  257. requires(is_assignable_v<const first_type&, _U1> && is_assignable_v<const second_type&, _U2>)
  258. {
  259. first = std::forward<_U1>(__p.first);
  260. second = std::forward<_U2>(__p.second);
  261. return *this;
  262. }
  263. template <__pair_like_no_subrange _PairLike>
  264. requires(__different_from<_PairLike, pair> &&
  265. is_assignable_v<first_type&, decltype(std::get<0>(std::declval<_PairLike>()))> &&
  266. is_assignable_v<second_type&, decltype(std::get<1>(std::declval<_PairLike>()))>)
  267. _LIBCPP_HIDE_FROM_ABI constexpr pair& operator=(_PairLike&& __p) {
  268. first = std::get<0>(std::forward<_PairLike>(__p));
  269. second = std::get<1>(std::forward<_PairLike>(__p));
  270. return *this;
  271. }
  272. template <__pair_like_no_subrange _PairLike>
  273. requires(__different_from<_PairLike, pair> &&
  274. is_assignable_v<first_type const&, decltype(std::get<0>(std::declval<_PairLike>()))> &&
  275. is_assignable_v<second_type const&, decltype(std::get<1>(std::declval<_PairLike>()))>)
  276. _LIBCPP_HIDE_FROM_ABI constexpr pair const& operator=(_PairLike&& __p) const {
  277. first = std::get<0>(std::forward<_PairLike>(__p));
  278. second = std::get<1>(std::forward<_PairLike>(__p));
  279. return *this;
  280. }
  281. # endif // _LIBCPP_STD_VER >= 23
  282. // Prior to C++23, we provide an approximation of constructors and assignment operators from
  283. // pair-like types. This was historically provided as an extension.
  284. # if _LIBCPP_STD_VER < 23
  285. // from std::tuple
  286. template <class _U1,
  287. class _U2,
  288. __enable_if_t<is_convertible<_U1 const&, _T1>::value && is_convertible<_U2 const&, _T2>::value, int> = 0>
  289. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair(tuple<_U1, _U2> const& __p)
  290. : first(std::get<0>(__p)), second(std::get<1>(__p)) {}
  291. template < class _U1,
  292. class _U2,
  293. __enable_if_t<is_constructible<_T1, _U1 const&>::value && is_constructible<_T2, _U2 const&>::value &&
  294. !(is_convertible<_U1 const&, _T1>::value && is_convertible<_U2 const&, _T2>::value),
  295. int> = 0>
  296. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit pair(tuple<_U1, _U2> const& __p)
  297. : first(std::get<0>(__p)), second(std::get<1>(__p)) {}
  298. template <class _U1,
  299. class _U2,
  300. __enable_if_t<is_convertible<_U1, _T1>::value && is_convertible<_U2, _T2>::value, int> = 0>
  301. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair(tuple<_U1, _U2>&& __p)
  302. : first(std::get<0>(std::move(__p))), second(std::get<1>(std::move(__p))) {}
  303. template <class _U1,
  304. class _U2,
  305. __enable_if_t<is_constructible<_T1, _U1>::value && is_constructible<_T2, _U2>::value &&
  306. !(is_convertible<_U1, _T1>::value && is_convertible<_U2, _T2>::value) > = 0>
  307. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit pair(tuple<_U1, _U2>&& __p)
  308. : first(std::get<0>(std::move(__p))), second(std::get<1>(std::move(__p))) {}
  309. template <class _U1,
  310. class _U2,
  311. __enable_if_t<is_assignable<_T1&, _U1 const&>::value && is_assignable<_T2&, _U2 const&>::value, int> = 0>
  312. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair& operator=(tuple<_U1, _U2> const& __p) {
  313. first = std::get<0>(__p);
  314. second = std::get<1>(__p);
  315. return *this;
  316. }
  317. template <class _U1,
  318. class _U2,
  319. __enable_if_t<is_assignable<_T1&, _U1&&>::value && is_assignable<_T2&, _U2&&>::value, int> = 0>
  320. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair& operator=(tuple<_U1, _U2>&& __p) {
  321. first = std::get<0>(std::move(__p));
  322. second = std::get<1>(std::move(__p));
  323. return *this;
  324. }
  325. // from std::array
  326. template <class _Up,
  327. __enable_if_t<is_convertible<_Up const&, _T1>::value && is_convertible<_Up const&, _T2>::value, int> = 0>
  328. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair(array<_Up, 2> const& __p) : first(__p[0]), second(__p[1]) {}
  329. template <class _Up,
  330. __enable_if_t<is_constructible<_T1, _Up const&>::value && is_constructible<_T2, _Up const&>::value &&
  331. !(is_convertible<_Up const&, _T1>::value && is_convertible<_Up const&, _T2>::value),
  332. int> = 0>
  333. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit pair(array<_Up, 2> const& __p)
  334. : first(__p[0]), second(__p[1]) {}
  335. template <class _Up, __enable_if_t< is_convertible<_Up, _T1>::value && is_convertible<_Up, _T2>::value, int> = 0>
  336. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair(array<_Up, 2>&& __p)
  337. : first(std::move(__p)[0]), second(std::move(__p)[1]) {}
  338. template <class _Up,
  339. __enable_if_t<is_constructible<_T1, _Up>::value && is_constructible<_T2, _Up>::value &&
  340. !(is_convertible<_Up, _T1>::value && is_convertible<_Up, _T2>::value),
  341. int> = 0>
  342. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit pair(array<_Up, 2>&& __p)
  343. : first(std::move(__p)[0]), second(std::move(__p)[1]) {}
  344. template <class _Up,
  345. __enable_if_t<is_assignable<_T1&, _Up const&>::value && is_assignable<_T2&, _Up const&>::value, int> = 0>
  346. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair& operator=(array<_Up, 2> const& __p) {
  347. first = std::get<0>(__p);
  348. second = std::get<1>(__p);
  349. return *this;
  350. }
  351. template <class _Up, __enable_if_t<is_assignable<_T1&, _Up>::value && is_assignable<_T2&, _Up>::value, int> = 0>
  352. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair& operator=(array<_Up, 2>&& __p) {
  353. first = std::get<0>(std::move(__p));
  354. second = std::get<1>(std::move(__p));
  355. return *this;
  356. }
  357. # endif // _LIBCPP_STD_VER < 23
  358. #endif // _LIBCPP_CXX03_LANG
  359. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(pair& __p)
  360. _NOEXCEPT_(__is_nothrow_swappable<first_type>::value&& __is_nothrow_swappable<second_type>::value) {
  361. using std::swap;
  362. swap(first, __p.first);
  363. swap(second, __p.second);
  364. }
  365. #if _LIBCPP_STD_VER >= 23
  366. _LIBCPP_HIDE_FROM_ABI constexpr void swap(const pair& __p) const
  367. noexcept(__is_nothrow_swappable<const first_type>::value && __is_nothrow_swappable<const second_type>::value) {
  368. using std::swap;
  369. swap(first, __p.first);
  370. swap(second, __p.second);
  371. }
  372. #endif
  373. private:
  374. #ifndef _LIBCPP_CXX03_LANG
  375. template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
  376. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  377. pair(piecewise_construct_t,
  378. tuple<_Args1...>& __first_args,
  379. tuple<_Args2...>& __second_args,
  380. __tuple_indices<_I1...>,
  381. __tuple_indices<_I2...>)
  382. : first(std::forward<_Args1>(std::get<_I1>(__first_args))...),
  383. second(std::forward<_Args2>(std::get<_I2>(__second_args))...) {}
  384. #endif
  385. };
  386. #if _LIBCPP_STD_VER >= 17
  387. template <class _T1, class _T2>
  388. pair(_T1, _T2) -> pair<_T1, _T2>;
  389. #endif
  390. // [pairs.spec], specialized algorithms
  391. template <class _T1, class _T2, class _U1, class _U2>
  392. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  393. operator==(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
  394. return __x.first == __y.first && __x.second == __y.second;
  395. }
  396. #if _LIBCPP_STD_VER >= 20
  397. template <class _T1, class _T2, class _U1, class _U2>
  398. _LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t< __synth_three_way_result<_T1, _U1>,
  399. __synth_three_way_result<_T2, _U2> >
  400. operator<=>(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
  401. if (auto __c = std::__synth_three_way(__x.first, __y.first); __c != 0) {
  402. return __c;
  403. }
  404. return std::__synth_three_way(__x.second, __y.second);
  405. }
  406. #else // _LIBCPP_STD_VER >= 20
  407. template <class _T1, class _T2, class _U1, class _U2>
  408. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  409. operator!=(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
  410. return !(__x == __y);
  411. }
  412. template <class _T1, class _T2, class _U1, class _U2>
  413. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  414. operator<(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
  415. return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
  416. }
  417. template <class _T1, class _T2, class _U1, class _U2>
  418. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  419. operator>(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
  420. return __y < __x;
  421. }
  422. template <class _T1, class _T2, class _U1, class _U2>
  423. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  424. operator>=(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
  425. return !(__x < __y);
  426. }
  427. template <class _T1, class _T2, class _U1, class _U2>
  428. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  429. operator<=(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
  430. return !(__y < __x);
  431. }
  432. #endif // _LIBCPP_STD_VER >= 20
  433. #if _LIBCPP_STD_VER >= 23
  434. template <class _T1, class _T2, class _U1, class _U2, template <class> class _TQual, template <class> class _UQual>
  435. requires requires {
  436. typename pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>, common_reference_t<_TQual<_T2>, _UQual<_U2>>>;
  437. }
  438. struct basic_common_reference<pair<_T1, _T2>, pair<_U1, _U2>, _TQual, _UQual> {
  439. using type = pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>, common_reference_t<_TQual<_T2>, _UQual<_U2>>>;
  440. };
  441. template <class _T1, class _T2, class _U1, class _U2>
  442. requires requires { typename pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>; }
  443. struct common_type<pair<_T1, _T2>, pair<_U1, _U2>> {
  444. using type = pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>;
  445. };
  446. #endif // _LIBCPP_STD_VER >= 23
  447. template <class _T1, class _T2, __enable_if_t<__is_swappable<_T1>::value && __is_swappable<_T2>::value, int> = 0>
  448. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
  449. _NOEXCEPT_(__is_nothrow_swappable<_T1>::value&& __is_nothrow_swappable<_T2>::value) {
  450. __x.swap(__y);
  451. }
  452. #if _LIBCPP_STD_VER >= 23
  453. template <class _T1, class _T2>
  454. requires(__is_swappable<const _T1>::value && __is_swappable<const _T2>::value)
  455. _LIBCPP_HIDE_FROM_ABI constexpr void
  456. swap(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) noexcept(noexcept(__x.swap(__y))) {
  457. __x.swap(__y);
  458. }
  459. #endif
  460. template <class _T1, class _T2>
  461. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  462. pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
  463. make_pair(_T1&& __t1, _T2&& __t2) {
  464. return pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>(
  465. std::forward<_T1>(__t1), std::forward<_T2>(__t2));
  466. }
  467. template <class _T1, class _T2>
  468. struct _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> > : public integral_constant<size_t, 2> {};
  469. template <size_t _Ip, class _T1, class _T2>
  470. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> > {
  471. static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
  472. };
  473. template <class _T1, class _T2>
  474. struct _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> > {
  475. using type _LIBCPP_NODEBUG = _T1;
  476. };
  477. template <class _T1, class _T2>
  478. struct _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> > {
  479. using type _LIBCPP_NODEBUG = _T2;
  480. };
  481. template <size_t _Ip>
  482. struct __get_pair;
  483. template <>
  484. struct __get_pair<0> {
  485. template <class _T1, class _T2>
  486. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T1& get(pair<_T1, _T2>& __p) _NOEXCEPT {
  487. return __p.first;
  488. }
  489. template <class _T1, class _T2>
  490. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _T1& get(const pair<_T1, _T2>& __p) _NOEXCEPT {
  491. return __p.first;
  492. }
  493. template <class _T1, class _T2>
  494. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T1&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
  495. return std::forward<_T1>(__p.first);
  496. }
  497. template <class _T1, class _T2>
  498. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _T1&& get(const pair<_T1, _T2>&& __p) _NOEXCEPT {
  499. return std::forward<const _T1>(__p.first);
  500. }
  501. };
  502. template <>
  503. struct __get_pair<1> {
  504. template <class _T1, class _T2>
  505. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T2& get(pair<_T1, _T2>& __p) _NOEXCEPT {
  506. return __p.second;
  507. }
  508. template <class _T1, class _T2>
  509. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _T2& get(const pair<_T1, _T2>& __p) _NOEXCEPT {
  510. return __p.second;
  511. }
  512. template <class _T1, class _T2>
  513. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T2&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
  514. return std::forward<_T2>(__p.second);
  515. }
  516. template <class _T1, class _T2>
  517. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _T2&& get(const pair<_T1, _T2>&& __p) _NOEXCEPT {
  518. return std::forward<const _T2>(__p.second);
  519. }
  520. };
  521. template <size_t _Ip, class _T1, class _T2>
  522. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  523. get(pair<_T1, _T2>& __p) _NOEXCEPT {
  524. return __get_pair<_Ip>::get(__p);
  525. }
  526. template <size_t _Ip, class _T1, class _T2>
  527. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  528. get(const pair<_T1, _T2>& __p) _NOEXCEPT {
  529. return __get_pair<_Ip>::get(__p);
  530. }
  531. template <size_t _Ip, class _T1, class _T2>
  532. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  533. get(pair<_T1, _T2>&& __p) _NOEXCEPT {
  534. return __get_pair<_Ip>::get(std::move(__p));
  535. }
  536. template <size_t _Ip, class _T1, class _T2>
  537. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  538. get(const pair<_T1, _T2>&& __p) _NOEXCEPT {
  539. return __get_pair<_Ip>::get(std::move(__p));
  540. }
  541. #if _LIBCPP_STD_VER >= 14
  542. template <class _T1, class _T2>
  543. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1& get(pair<_T1, _T2>& __p) _NOEXCEPT {
  544. return __get_pair<0>::get(__p);
  545. }
  546. template <class _T1, class _T2>
  547. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const& get(pair<_T1, _T2> const& __p) _NOEXCEPT {
  548. return __get_pair<0>::get(__p);
  549. }
  550. template <class _T1, class _T2>
  551. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
  552. return __get_pair<0>::get(std::move(__p));
  553. }
  554. template <class _T1, class _T2>
  555. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const&& get(pair<_T1, _T2> const&& __p) _NOEXCEPT {
  556. return __get_pair<0>::get(std::move(__p));
  557. }
  558. template <class _T1, class _T2>
  559. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1& get(pair<_T2, _T1>& __p) _NOEXCEPT {
  560. return __get_pair<1>::get(__p);
  561. }
  562. template <class _T1, class _T2>
  563. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const& get(pair<_T2, _T1> const& __p) _NOEXCEPT {
  564. return __get_pair<1>::get(__p);
  565. }
  566. template <class _T1, class _T2>
  567. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1&& get(pair<_T2, _T1>&& __p) _NOEXCEPT {
  568. return __get_pair<1>::get(std::move(__p));
  569. }
  570. template <class _T1, class _T2>
  571. inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const&& get(pair<_T2, _T1> const&& __p) _NOEXCEPT {
  572. return __get_pair<1>::get(std::move(__p));
  573. }
  574. #endif // _LIBCPP_STD_VER >= 14
  575. _LIBCPP_END_NAMESPACE_STD
  576. _LIBCPP_POP_MACROS
  577. #endif // _LIBCPP___UTILITY_PAIR_H