pair.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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/get.h>
  16. #include <__fwd/pair.h>
  17. #include <__fwd/subrange.h>
  18. #include <__fwd/tuple.h>
  19. #include <__tuple/pair_like.h>
  20. #include <__tuple/sfinae_helpers.h>
  21. #include <__tuple/tuple_element.h>
  22. #include <__tuple/tuple_indices.h>
  23. #include <__tuple/tuple_size.h>
  24. #include <__type_traits/common_reference.h>
  25. #include <__type_traits/common_type.h>
  26. #include <__type_traits/conditional.h>
  27. #include <__type_traits/decay.h>
  28. #include <__type_traits/integral_constant.h>
  29. #include <__type_traits/is_assignable.h>
  30. #include <__type_traits/is_constructible.h>
  31. #include <__type_traits/is_convertible.h>
  32. #include <__type_traits/is_copy_assignable.h>
  33. #include <__type_traits/is_default_constructible.h>
  34. #include <__type_traits/is_implicitly_default_constructible.h>
  35. #include <__type_traits/is_move_assignable.h>
  36. #include <__type_traits/is_nothrow_assignable.h>
  37. #include <__type_traits/is_nothrow_constructible.h>
  38. #include <__type_traits/is_nothrow_copy_assignable.h>
  39. #include <__type_traits/is_nothrow_copy_constructible.h>
  40. #include <__type_traits/is_nothrow_default_constructible.h>
  41. #include <__type_traits/is_nothrow_move_assignable.h>
  42. #include <__type_traits/is_same.h>
  43. #include <__type_traits/is_swappable.h>
  44. #include <__type_traits/nat.h>
  45. #include <__type_traits/remove_cvref.h>
  46. #include <__type_traits/unwrap_ref.h>
  47. #include <__utility/declval.h>
  48. #include <__utility/forward.h>
  49. #include <__utility/move.h>
  50. #include <__utility/piecewise_construct.h>
  51. #include <cstddef>
  52. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  53. # pragma GCC system_header
  54. #endif
  55. _LIBCPP_PUSH_MACROS
  56. #include <__undef_macros>
  57. _LIBCPP_BEGIN_NAMESPACE_STD
  58. template <class, class>
  59. struct __non_trivially_copyable_base {
  60. _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI
  61. __non_trivially_copyable_base() _NOEXCEPT {}
  62. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
  63. __non_trivially_copyable_base(__non_trivially_copyable_base const&) _NOEXCEPT {}
  64. };
  65. #if _LIBCPP_STD_VER >= 23
  66. template <class _Tp>
  67. struct __is_specialization_of_subrange : false_type {};
  68. template <class _Iter, class _Sent, ranges::subrange_kind _Kind>
  69. struct __is_specialization_of_subrange<ranges::subrange<_Iter, _Sent, _Kind>> : true_type {};
  70. #endif
  71. template <class _T1, class _T2>
  72. struct _LIBCPP_TEMPLATE_VIS pair
  73. #if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
  74. : private __non_trivially_copyable_base<_T1, _T2>
  75. #endif
  76. {
  77. using first_type = _T1;
  78. using second_type = _T2;
  79. _T1 first;
  80. _T2 second;
  81. _LIBCPP_HIDE_FROM_ABI pair(pair const&) = default;
  82. _LIBCPP_HIDE_FROM_ABI pair(pair&&) = default;
  83. #ifdef _LIBCPP_CXX03_LANG
  84. _LIBCPP_HIDE_FROM_ABI
  85. pair() : first(), second() {}
  86. _LIBCPP_HIDE_FROM_ABI
  87. pair(_T1 const& __t1, _T2 const& __t2) : first(__t1), second(__t2) {}
  88. template <class _U1, class _U2>
  89. _LIBCPP_HIDE_FROM_ABI
  90. pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
  91. _LIBCPP_HIDE_FROM_ABI
  92. pair& operator=(pair const& __p) {
  93. first = __p.first;
  94. second = __p.second;
  95. return *this;
  96. }
  97. // Extension: This is provided in C++03 because it allows properly handling the
  98. // assignment to a pair containing references, which would be a hard
  99. // error otherwise.
  100. template <class _U1, class _U2, class = __enable_if_t<
  101. is_assignable<first_type&, _U1 const&>::value &&
  102. is_assignable<second_type&, _U2 const&>::value
  103. > >
  104. _LIBCPP_HIDE_FROM_ABI
  105. pair& operator=(pair<_U1, _U2> const& __p) {
  106. first = __p.first;
  107. second = __p.second;
  108. return *this;
  109. }
  110. #else
  111. struct _CheckArgs {
  112. template <int&...>
  113. static _LIBCPP_HIDE_FROM_ABI constexpr bool __enable_explicit_default() {
  114. return is_default_constructible<_T1>::value
  115. && is_default_constructible<_T2>::value
  116. && !__enable_implicit_default<>();
  117. }
  118. template <int&...>
  119. static _LIBCPP_HIDE_FROM_ABI constexpr bool __enable_implicit_default() {
  120. return __is_implicitly_default_constructible<_T1>::value
  121. && __is_implicitly_default_constructible<_T2>::value;
  122. }
  123. template <class _U1, class _U2>
  124. static _LIBCPP_HIDE_FROM_ABI constexpr bool __is_pair_constructible() {
  125. return is_constructible<first_type, _U1>::value
  126. && is_constructible<second_type, _U2>::value;
  127. }
  128. template <class _U1, class _U2>
  129. static _LIBCPP_HIDE_FROM_ABI constexpr bool __is_implicit() {
  130. return is_convertible<_U1, first_type>::value
  131. && is_convertible<_U2, second_type>::value;
  132. }
  133. template <class _U1, class _U2>
  134. static _LIBCPP_HIDE_FROM_ABI constexpr bool __enable_explicit() {
  135. return __is_pair_constructible<_U1, _U2>() && !__is_implicit<_U1, _U2>();
  136. }
  137. template <class _U1, class _U2>
  138. static _LIBCPP_HIDE_FROM_ABI constexpr bool __enable_implicit() {
  139. return __is_pair_constructible<_U1, _U2>() && __is_implicit<_U1, _U2>();
  140. }
  141. };
  142. template <bool _MaybeEnable>
  143. using _CheckArgsDep _LIBCPP_NODEBUG = typename conditional<
  144. _MaybeEnable, _CheckArgs, __check_tuple_constructor_fail>::type;
  145. template<bool _Dummy = true, __enable_if_t<_CheckArgsDep<_Dummy>::__enable_explicit_default(), int> = 0>
  146. explicit _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  147. pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
  148. is_nothrow_default_constructible<second_type>::value)
  149. : first(), second() {}
  150. template<bool _Dummy = true, __enable_if_t<_CheckArgsDep<_Dummy>::__enable_implicit_default(), int> = 0>
  151. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  152. pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
  153. is_nothrow_default_constructible<second_type>::value)
  154. : first(), second() {}
  155. template <bool _Dummy = true,
  156. __enable_if_t<_CheckArgsDep<_Dummy>::template __enable_explicit<_T1 const&, _T2 const&>(), int> = 0>
  157. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  158. explicit pair(_T1 const& __t1, _T2 const& __t2)
  159. _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
  160. is_nothrow_copy_constructible<second_type>::value)
  161. : first(__t1), second(__t2) {}
  162. template<bool _Dummy = true,
  163. __enable_if_t<_CheckArgsDep<_Dummy>::template __enable_implicit<_T1 const&, _T2 const&>(), int> = 0>
  164. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  165. pair(_T1 const& __t1, _T2 const& __t2)
  166. _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
  167. is_nothrow_copy_constructible<second_type>::value)
  168. : first(__t1), second(__t2) {}
  169. template <
  170. #if _LIBCPP_STD_VER >= 23 // http://wg21.link/P1951
  171. class _U1 = _T1, class _U2 = _T2,
  172. #else
  173. class _U1, class _U2,
  174. #endif
  175. __enable_if_t<_CheckArgs::template __enable_explicit<_U1, _U2>(), int> = 0
  176. >
  177. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  178. explicit pair(_U1&& __u1, _U2&& __u2)
  179. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
  180. is_nothrow_constructible<second_type, _U2>::value))
  181. : first(std::forward<_U1>(__u1)), second(std::forward<_U2>(__u2)) {}
  182. template <
  183. #if _LIBCPP_STD_VER >= 23 // http://wg21.link/P1951
  184. class _U1 = _T1, class _U2 = _T2,
  185. #else
  186. class _U1, class _U2,
  187. #endif
  188. __enable_if_t<_CheckArgs::template __enable_implicit<_U1, _U2>(), int> = 0
  189. >
  190. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  191. pair(_U1&& __u1, _U2&& __u2)
  192. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
  193. is_nothrow_constructible<second_type, _U2>::value))
  194. : first(std::forward<_U1>(__u1)), second(std::forward<_U2>(__u2)) {}
  195. #if _LIBCPP_STD_VER >= 23
  196. template<class _U1, class _U2, __enable_if_t<
  197. _CheckArgs::template __is_pair_constructible<_U1&, _U2&>()
  198. >* = nullptr>
  199. _LIBCPP_HIDE_FROM_ABI constexpr
  200. explicit(!_CheckArgs::template __is_implicit<_U1&, _U2&>()) pair(pair<_U1, _U2>& __p)
  201. noexcept((is_nothrow_constructible<first_type, _U1&>::value &&
  202. is_nothrow_constructible<second_type, _U2&>::value))
  203. : first(__p.first), second(__p.second) {}
  204. #endif
  205. template<class _U1, class _U2, __enable_if_t<_CheckArgs::template __enable_explicit<_U1 const&, _U2 const&>(), int> = 0>
  206. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  207. explicit pair(pair<_U1, _U2> const& __p)
  208. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
  209. is_nothrow_constructible<second_type, _U2 const&>::value))
  210. : first(__p.first), second(__p.second) {}
  211. template<class _U1, class _U2, __enable_if_t<_CheckArgs::template __enable_implicit<_U1 const&, _U2 const&>(), int> = 0>
  212. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  213. pair(pair<_U1, _U2> const& __p)
  214. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
  215. is_nothrow_constructible<second_type, _U2 const&>::value))
  216. : first(__p.first), second(__p.second) {}
  217. template<class _U1, class _U2, __enable_if_t<_CheckArgs::template __enable_explicit<_U1, _U2>(), int> = 0>
  218. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  219. explicit pair(pair<_U1, _U2>&&__p)
  220. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
  221. is_nothrow_constructible<second_type, _U2&&>::value))
  222. : first(std::forward<_U1>(__p.first)), second(std::forward<_U2>(__p.second)) {}
  223. template<class _U1, class _U2, __enable_if_t<_CheckArgs::template __enable_implicit<_U1, _U2>(), int> = 0>
  224. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  225. pair(pair<_U1, _U2>&& __p)
  226. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
  227. is_nothrow_constructible<second_type, _U2&&>::value))
  228. : first(std::forward<_U1>(__p.first)), second(std::forward<_U2>(__p.second)) {}
  229. #if _LIBCPP_STD_VER >= 23
  230. template<class _U1, class _U2, __enable_if_t<
  231. _CheckArgs::template __is_pair_constructible<const _U1&&, const _U2&&>()
  232. >* = nullptr>
  233. _LIBCPP_HIDE_FROM_ABI constexpr
  234. explicit(!_CheckArgs::template __is_implicit<const _U1&&, const _U2&&>())
  235. pair(const pair<_U1, _U2>&& __p)
  236. noexcept(is_nothrow_constructible<first_type, const _U1&&>::value &&
  237. is_nothrow_constructible<second_type, const _U2&&>::value)
  238. : first(std::move(__p.first)), second(std::move(__p.second)) {}
  239. #endif
  240. # if _LIBCPP_STD_VER >= 23
  241. // This is a workaround for http://llvm.org/PR60710. We should be able to remove it once Clang is fixed.
  242. template <class _PairLike>
  243. _LIBCPP_HIDE_FROM_ABI static constexpr bool __pair_like_explicit_wknd() {
  244. if constexpr (__pair_like<_PairLike>) {
  245. return !is_convertible_v<decltype(std::get<0>(std::declval<_PairLike&&>())), first_type> ||
  246. !is_convertible_v<decltype(std::get<1>(std::declval<_PairLike&&>())), second_type>;
  247. }
  248. return false;
  249. }
  250. template <__pair_like _PairLike>
  251. requires(is_constructible_v<first_type, decltype(std::get<0>(std::declval<_PairLike&&>()))> &&
  252. is_constructible_v<second_type, decltype(std::get<1>(std::declval<_PairLike&&>()))>)
  253. _LIBCPP_HIDE_FROM_ABI constexpr explicit(__pair_like_explicit_wknd<_PairLike>())
  254. pair(_PairLike&& __p)
  255. : first(std::get<0>(std::forward<_PairLike>(__p))), second(std::get<1>(std::forward<_PairLike>(__p))) {}
  256. # endif
  257. template <class... _Args1, class... _Args2>
  258. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  259. pair(piecewise_construct_t __pc,
  260. tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
  261. _NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value &&
  262. is_nothrow_constructible<second_type, _Args2...>::value))
  263. : pair(__pc, __first_args, __second_args,
  264. typename __make_tuple_indices<sizeof...(_Args1)>::type(),
  265. typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
  266. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  267. pair& operator=(__conditional_t<
  268. is_copy_assignable<first_type>::value &&
  269. is_copy_assignable<second_type>::value,
  270. pair, __nat> const& __p)
  271. _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
  272. is_nothrow_copy_assignable<second_type>::value)
  273. {
  274. first = __p.first;
  275. second = __p.second;
  276. return *this;
  277. }
  278. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  279. pair& operator=(__conditional_t<
  280. is_move_assignable<first_type>::value &&
  281. is_move_assignable<second_type>::value,
  282. pair, __nat>&& __p)
  283. _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
  284. is_nothrow_move_assignable<second_type>::value)
  285. {
  286. first = std::forward<first_type>(__p.first);
  287. second = std::forward<second_type>(__p.second);
  288. return *this;
  289. }
  290. template <class _U1, class _U2, __enable_if_t<
  291. is_assignable<first_type&, _U1 const&>::value &&
  292. is_assignable<second_type&, _U2 const&>::value
  293. >* = nullptr>
  294. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  295. pair& operator=(pair<_U1, _U2> const& __p) {
  296. first = __p.first;
  297. second = __p.second;
  298. return *this;
  299. }
  300. template <class _U1, class _U2, __enable_if_t<
  301. is_assignable<first_type&, _U1>::value &&
  302. is_assignable<second_type&, _U2>::value
  303. >* = nullptr>
  304. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  305. pair& operator=(pair<_U1, _U2>&& __p) {
  306. first = std::forward<_U1>(__p.first);
  307. second = std::forward<_U2>(__p.second);
  308. return *this;
  309. }
  310. # if _LIBCPP_STD_VER >= 23
  311. _LIBCPP_HIDE_FROM_ABI constexpr
  312. const pair& operator=(pair const& __p) const
  313. noexcept(is_nothrow_copy_assignable_v<const first_type> &&
  314. is_nothrow_copy_assignable_v<const second_type>)
  315. requires(is_copy_assignable_v<const first_type> &&
  316. is_copy_assignable_v<const second_type>) {
  317. first = __p.first;
  318. second = __p.second;
  319. return *this;
  320. }
  321. _LIBCPP_HIDE_FROM_ABI constexpr
  322. const pair& operator=(pair&& __p) const
  323. noexcept(is_nothrow_assignable_v<const first_type&, first_type> &&
  324. is_nothrow_assignable_v<const second_type&, second_type>)
  325. requires(is_assignable_v<const first_type&, first_type> &&
  326. is_assignable_v<const second_type&, second_type>) {
  327. first = std::forward<first_type>(__p.first);
  328. second = std::forward<second_type>(__p.second);
  329. return *this;
  330. }
  331. template<class _U1, class _U2>
  332. _LIBCPP_HIDE_FROM_ABI constexpr
  333. const pair& operator=(const pair<_U1, _U2>& __p) const
  334. requires(is_assignable_v<const first_type&, const _U1&> &&
  335. is_assignable_v<const second_type&, const _U2&>) {
  336. first = __p.first;
  337. second = __p.second;
  338. return *this;
  339. }
  340. template<class _U1, class _U2>
  341. _LIBCPP_HIDE_FROM_ABI constexpr
  342. const pair& operator=(pair<_U1, _U2>&& __p) const
  343. requires(is_assignable_v<const first_type&, _U1> &&
  344. is_assignable_v<const second_type&, _U2>) {
  345. first = std::forward<_U1>(__p.first);
  346. second = std::forward<_U2>(__p.second);
  347. return *this;
  348. }
  349. template <__pair_like _PairLike>
  350. requires(__different_from<_PairLike, pair> &&
  351. !__is_specialization_of_subrange<remove_cvref_t<_PairLike>>::value &&
  352. is_assignable_v<first_type&, decltype(std::get<0>(std::declval<_PairLike>()))> &&
  353. is_assignable_v<second_type&, decltype(std::get<1>(std::declval<_PairLike>()))>)
  354. _LIBCPP_HIDE_FROM_ABI constexpr pair& operator=(_PairLike&& __p) {
  355. first = std::get<0>(std::forward<_PairLike>(__p));
  356. second = std::get<1>(std::forward<_PairLike>(__p));
  357. return *this;
  358. }
  359. template <__pair_like _PairLike>
  360. requires(__different_from<_PairLike, pair> &&
  361. !__is_specialization_of_subrange<remove_cvref_t<_PairLike>>::value &&
  362. is_assignable_v<first_type const&, decltype(std::get<0>(std::declval<_PairLike>()))> &&
  363. is_assignable_v<second_type const&, decltype(std::get<1>(std::declval<_PairLike>()))>)
  364. _LIBCPP_HIDE_FROM_ABI constexpr pair const& operator=(_PairLike&& __p) const {
  365. first = std::get<0>(std::forward<_PairLike>(__p));
  366. second = std::get<1>(std::forward<_PairLike>(__p));
  367. return *this;
  368. }
  369. # endif // _LIBCPP_STD_VER >= 23
  370. // Prior to C++23, we provide an approximation of constructors and assignment operators from
  371. // pair-like types. This was historically provided as an extension.
  372. #if _LIBCPP_STD_VER < 23
  373. // from std::tuple
  374. template<class _U1, class _U2, __enable_if_t<
  375. is_convertible<_U1 const&, _T1>::value &&
  376. is_convertible<_U2 const&, _T2>::value
  377. >* = nullptr>
  378. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  379. pair(tuple<_U1, _U2> const& __p)
  380. : first(std::get<0>(__p)),
  381. second(std::get<1>(__p)) {}
  382. template<class _U1, class _U2, __enable_if_t<
  383. is_constructible<_T1, _U1 const&>::value &&
  384. is_constructible<_T2, _U2 const&>::value &&
  385. !(is_convertible<_U1 const&, _T1>::value &&
  386. is_convertible<_U2 const&, _T2>::value)
  387. >* = nullptr>
  388. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  389. explicit
  390. pair(tuple<_U1, _U2> const& __p)
  391. : first(std::get<0>(__p)),
  392. second(std::get<1>(__p)) {}
  393. template<class _U1, class _U2, __enable_if_t<
  394. is_convertible<_U1, _T1>::value &&
  395. is_convertible<_U2, _T2>::value
  396. >* = nullptr>
  397. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  398. pair(tuple<_U1, _U2>&& __p)
  399. : first(std::get<0>(std::move(__p))),
  400. second(std::get<1>(std::move(__p))) {}
  401. template<class _U1, class _U2, __enable_if_t<
  402. is_constructible<_T1, _U1>::value &&
  403. is_constructible<_T2, _U2>::value &&
  404. !(is_convertible<_U1, _T1>::value &&
  405. is_convertible<_U2, _T2>::value)
  406. >* = nullptr>
  407. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  408. explicit
  409. pair(tuple<_U1, _U2>&& __p)
  410. : first(std::get<0>(std::move(__p))),
  411. second(std::get<1>(std::move(__p))) {}
  412. template<class _U1, class _U2, __enable_if_t<
  413. is_assignable<_T1&, _U1 const&>::value &&
  414. is_assignable<_T2&, _U2 const&>::value
  415. >* = nullptr>
  416. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  417. pair& operator=(tuple<_U1, _U2> const& __p) {
  418. first = std::get<0>(__p);
  419. second = std::get<1>(__p);
  420. return *this;
  421. }
  422. template<class _U1, class _U2, __enable_if_t<
  423. is_assignable<_T1&, _U1&&>::value &&
  424. is_assignable<_T2&, _U2&&>::value
  425. >* = nullptr>
  426. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  427. pair& operator=(tuple<_U1, _U2>&& __p) {
  428. first = std::get<0>(std::move(__p));
  429. second = std::get<1>(std::move(__p));
  430. return *this;
  431. }
  432. // from std::array
  433. template<class _Up, __enable_if_t<
  434. is_convertible<_Up const&, _T1>::value &&
  435. is_convertible<_Up const&, _T2>::value
  436. >* = nullptr>
  437. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  438. pair(array<_Up, 2> const& __p)
  439. : first(__p[0]),
  440. second(__p[1]) {}
  441. template<class _Up, __enable_if_t<
  442. is_constructible<_T1, _Up const&>::value &&
  443. is_constructible<_T2, _Up const&>::value &&
  444. !(is_convertible<_Up const&, _T1>::value &&
  445. is_convertible<_Up const&, _T2>::value)
  446. >* = nullptr>
  447. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  448. explicit
  449. pair(array<_Up, 2> const& __p)
  450. : first(__p[0]),
  451. second(__p[1]) {}
  452. template<class _Up, __enable_if_t<
  453. is_convertible<_Up, _T1>::value &&
  454. is_convertible<_Up, _T2>::value
  455. >* = nullptr>
  456. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  457. pair(array<_Up, 2>&& __p)
  458. : first(std::move(__p)[0]),
  459. second(std::move(__p)[1]) {}
  460. template<class _Up, __enable_if_t<
  461. is_constructible<_T1, _Up>::value &&
  462. is_constructible<_T2, _Up>::value &&
  463. !(is_convertible<_Up, _T1>::value &&
  464. is_convertible<_Up, _T2>::value)
  465. >* = nullptr>
  466. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  467. explicit
  468. pair(array<_Up, 2>&& __p)
  469. : first(std::move(__p)[0]),
  470. second(std::move(__p)[1]) {}
  471. template<class _Up, __enable_if_t<
  472. is_assignable<_T1&, _Up const&>::value &&
  473. is_assignable<_T2&, _Up const&>::value
  474. >* = nullptr>
  475. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  476. pair& operator=(array<_Up, 2> const& __p) {
  477. first = std::get<0>(__p);
  478. second = std::get<1>(__p);
  479. return *this;
  480. }
  481. template<class _Up, __enable_if_t<
  482. is_assignable<_T1&, _Up>::value &&
  483. is_assignable<_T2&, _Up>::value
  484. >* = nullptr>
  485. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  486. pair& operator=(array<_Up, 2>&& __p) {
  487. first = std::get<0>(std::move(__p));
  488. second = std::get<1>(std::move(__p));
  489. return *this;
  490. }
  491. #endif // _LIBCPP_STD_VER < 23
  492. #endif // _LIBCPP_CXX03_LANG
  493. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  494. void
  495. swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
  496. __is_nothrow_swappable<second_type>::value)
  497. {
  498. using std::swap;
  499. swap(first, __p.first);
  500. swap(second, __p.second);
  501. }
  502. #if _LIBCPP_STD_VER >= 23
  503. _LIBCPP_HIDE_FROM_ABI constexpr
  504. void swap(const pair& __p) const
  505. noexcept(__is_nothrow_swappable<const first_type>::value &&
  506. __is_nothrow_swappable<const second_type>::value)
  507. {
  508. using std::swap;
  509. swap(first, __p.first);
  510. swap(second, __p.second);
  511. }
  512. #endif
  513. private:
  514. #ifndef _LIBCPP_CXX03_LANG
  515. template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
  516. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  517. pair(piecewise_construct_t,
  518. tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
  519. __tuple_indices<_I1...>, __tuple_indices<_I2...>);
  520. #endif
  521. };
  522. #if _LIBCPP_STD_VER >= 17
  523. template<class _T1, class _T2>
  524. pair(_T1, _T2) -> pair<_T1, _T2>;
  525. #endif
  526. // [pairs.spec], specialized algorithms
  527. template <class _T1, class _T2, class _U1, class _U2>
  528. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  529. bool
  530. operator==(const pair<_T1,_T2>& __x, const pair<_U1,_U2>& __y)
  531. {
  532. return __x.first == __y.first && __x.second == __y.second;
  533. }
  534. #if _LIBCPP_STD_VER >= 20
  535. template <class _T1, class _T2, class _U1, class _U2>
  536. _LIBCPP_HIDE_FROM_ABI constexpr
  537. common_comparison_category_t<
  538. __synth_three_way_result<_T1, _U1>,
  539. __synth_three_way_result<_T2, _U2> >
  540. operator<=>(const pair<_T1,_T2>& __x, const pair<_U1,_U2>& __y)
  541. {
  542. if (auto __c = std::__synth_three_way(__x.first, __y.first); __c != 0) {
  543. return __c;
  544. }
  545. return std::__synth_three_way(__x.second, __y.second);
  546. }
  547. #else // _LIBCPP_STD_VER >= 20
  548. template <class _T1, class _T2, class _U1, class _U2>
  549. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  550. bool
  551. operator!=(const pair<_T1,_T2>& __x, const pair<_U1,_U2>& __y)
  552. {
  553. return !(__x == __y);
  554. }
  555. template <class _T1, class _T2, class _U1, class _U2>
  556. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  557. bool
  558. operator< (const pair<_T1,_T2>& __x, const pair<_U1,_U2>& __y)
  559. {
  560. return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
  561. }
  562. template <class _T1, class _T2, class _U1, class _U2>
  563. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  564. bool
  565. operator> (const pair<_T1,_T2>& __x, const pair<_U1,_U2>& __y)
  566. {
  567. return __y < __x;
  568. }
  569. template <class _T1, class _T2, class _U1, class _U2>
  570. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  571. bool
  572. operator>=(const pair<_T1,_T2>& __x, const pair<_U1,_U2>& __y)
  573. {
  574. return !(__x < __y);
  575. }
  576. template <class _T1, class _T2, class _U1, class _U2>
  577. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  578. bool
  579. operator<=(const pair<_T1,_T2>& __x, const pair<_U1,_U2>& __y)
  580. {
  581. return !(__y < __x);
  582. }
  583. #endif // _LIBCPP_STD_VER >= 20
  584. #if _LIBCPP_STD_VER >= 23
  585. template <class _T1, class _T2, class _U1, class _U2, template<class> class _TQual, template<class> class _UQual>
  586. requires requires { typename pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
  587. common_reference_t<_TQual<_T2>, _UQual<_U2>>>; }
  588. struct basic_common_reference<pair<_T1, _T2>, pair<_U1, _U2>, _TQual, _UQual> {
  589. using type = pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
  590. common_reference_t<_TQual<_T2>, _UQual<_U2>>>;
  591. };
  592. template <class _T1, class _T2, class _U1, class _U2>
  593. requires requires { typename pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>; }
  594. struct common_type<pair<_T1, _T2>, pair<_U1, _U2>> {
  595. using type = pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>;
  596. };
  597. #endif // _LIBCPP_STD_VER >= 23
  598. template <class _T1, class _T2, __enable_if_t<__is_swappable<_T1>::value && __is_swappable<_T2>::value, int> = 0>
  599. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  600. void
  601. swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
  602. _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
  603. __is_nothrow_swappable<_T2>::value))
  604. {
  605. __x.swap(__y);
  606. }
  607. #if _LIBCPP_STD_VER >= 23
  608. template <class _T1, class _T2>
  609. requires (__is_swappable<const _T1>::value &&
  610. __is_swappable<const _T2>::value)
  611. _LIBCPP_HIDE_FROM_ABI constexpr
  612. void swap(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  613. noexcept(noexcept(__x.swap(__y)))
  614. {
  615. __x.swap(__y);
  616. }
  617. #endif
  618. template <class _T1, class _T2>
  619. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  620. pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
  621. make_pair(_T1&& __t1, _T2&& __t2)
  622. {
  623. return pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
  624. (std::forward<_T1>(__t1), std::forward<_T2>(__t2));
  625. }
  626. template <class _T1, class _T2>
  627. struct _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
  628. : public integral_constant<size_t, 2> {};
  629. template <size_t _Ip, class _T1, class _T2>
  630. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
  631. {
  632. static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
  633. };
  634. template <class _T1, class _T2>
  635. struct _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
  636. {
  637. using type _LIBCPP_NODEBUG = _T1;
  638. };
  639. template <class _T1, class _T2>
  640. struct _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> >
  641. {
  642. using type _LIBCPP_NODEBUG = _T2;
  643. };
  644. template <size_t _Ip> struct __get_pair;
  645. template <>
  646. struct __get_pair<0>
  647. {
  648. template <class _T1, class _T2>
  649. static
  650. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  651. _T1&
  652. get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
  653. template <class _T1, class _T2>
  654. static
  655. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  656. const _T1&
  657. get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
  658. template <class _T1, class _T2>
  659. static
  660. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  661. _T1&&
  662. get(pair<_T1, _T2>&& __p) _NOEXCEPT {return std::forward<_T1>(__p.first);}
  663. template <class _T1, class _T2>
  664. static
  665. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  666. const _T1&&
  667. get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return std::forward<const _T1>(__p.first);}
  668. };
  669. template <>
  670. struct __get_pair<1>
  671. {
  672. template <class _T1, class _T2>
  673. static
  674. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  675. _T2&
  676. get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
  677. template <class _T1, class _T2>
  678. static
  679. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  680. const _T2&
  681. get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
  682. template <class _T1, class _T2>
  683. static
  684. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  685. _T2&&
  686. get(pair<_T1, _T2>&& __p) _NOEXCEPT {return std::forward<_T2>(__p.second);}
  687. template <class _T1, class _T2>
  688. static
  689. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  690. const _T2&&
  691. get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return std::forward<const _T2>(__p.second);}
  692. };
  693. template <size_t _Ip, class _T1, class _T2>
  694. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  695. typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  696. get(pair<_T1, _T2>& __p) _NOEXCEPT
  697. {
  698. return __get_pair<_Ip>::get(__p);
  699. }
  700. template <size_t _Ip, class _T1, class _T2>
  701. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  702. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  703. get(const pair<_T1, _T2>& __p) _NOEXCEPT
  704. {
  705. return __get_pair<_Ip>::get(__p);
  706. }
  707. template <size_t _Ip, class _T1, class _T2>
  708. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  709. typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  710. get(pair<_T1, _T2>&& __p) _NOEXCEPT
  711. {
  712. return __get_pair<_Ip>::get(std::move(__p));
  713. }
  714. template <size_t _Ip, class _T1, class _T2>
  715. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  716. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  717. get(const pair<_T1, _T2>&& __p) _NOEXCEPT
  718. {
  719. return __get_pair<_Ip>::get(std::move(__p));
  720. }
  721. #if _LIBCPP_STD_VER >= 14
  722. template <class _T1, class _T2>
  723. inline _LIBCPP_HIDE_FROM_ABI
  724. constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
  725. {
  726. return __get_pair<0>::get(__p);
  727. }
  728. template <class _T1, class _T2>
  729. inline _LIBCPP_HIDE_FROM_ABI
  730. constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
  731. {
  732. return __get_pair<0>::get(__p);
  733. }
  734. template <class _T1, class _T2>
  735. inline _LIBCPP_HIDE_FROM_ABI
  736. constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
  737. {
  738. return __get_pair<0>::get(std::move(__p));
  739. }
  740. template <class _T1, class _T2>
  741. inline _LIBCPP_HIDE_FROM_ABI
  742. constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
  743. {
  744. return __get_pair<0>::get(std::move(__p));
  745. }
  746. template <class _T1, class _T2>
  747. inline _LIBCPP_HIDE_FROM_ABI
  748. constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
  749. {
  750. return __get_pair<1>::get(__p);
  751. }
  752. template <class _T1, class _T2>
  753. inline _LIBCPP_HIDE_FROM_ABI
  754. constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
  755. {
  756. return __get_pair<1>::get(__p);
  757. }
  758. template <class _T1, class _T2>
  759. inline _LIBCPP_HIDE_FROM_ABI
  760. constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
  761. {
  762. return __get_pair<1>::get(std::move(__p));
  763. }
  764. template <class _T1, class _T2>
  765. inline _LIBCPP_HIDE_FROM_ABI
  766. constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
  767. {
  768. return __get_pair<1>::get(std::move(__p));
  769. }
  770. #endif // _LIBCPP_STD_VER >= 14
  771. _LIBCPP_END_NAMESPACE_STD
  772. _LIBCPP_POP_MACROS
  773. #endif // _LIBCPP___UTILITY_PAIR_H