pair.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 <__config>
  13. #include <__functional/unwrap_ref.h>
  14. #include <__fwd/get.h>
  15. #include <__fwd/tuple.h>
  16. #include <__tuple_dir/sfinae_helpers.h>
  17. #include <__tuple_dir/tuple_element.h>
  18. #include <__tuple_dir/tuple_indices.h>
  19. #include <__tuple_dir/tuple_size.h>
  20. #include <__type_traits/common_reference.h>
  21. #include <__type_traits/common_type.h>
  22. #include <__type_traits/conditional.h>
  23. #include <__type_traits/is_assignable.h>
  24. #include <__type_traits/is_constructible.h>
  25. #include <__type_traits/is_convertible.h>
  26. #include <__type_traits/is_copy_assignable.h>
  27. #include <__type_traits/is_default_constructible.h>
  28. #include <__type_traits/is_implicitly_default_constructible.h>
  29. #include <__type_traits/is_move_assignable.h>
  30. #include <__type_traits/is_nothrow_assignable.h>
  31. #include <__type_traits/is_nothrow_constructible.h>
  32. #include <__type_traits/is_nothrow_copy_assignable.h>
  33. #include <__type_traits/is_nothrow_copy_constructible.h>
  34. #include <__type_traits/is_nothrow_default_constructible.h>
  35. #include <__type_traits/is_nothrow_move_assignable.h>
  36. #include <__type_traits/is_same.h>
  37. #include <__type_traits/is_swappable.h>
  38. #include <__type_traits/nat.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_BEGIN_NAMESPACE_STD
  47. #if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
  48. template <class, class>
  49. struct __non_trivially_copyable_base {
  50. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  51. __non_trivially_copyable_base() _NOEXCEPT {}
  52. _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
  53. __non_trivially_copyable_base(__non_trivially_copyable_base const&) _NOEXCEPT {}
  54. };
  55. #endif
  56. template <class _T1, class _T2>
  57. struct _LIBCPP_TEMPLATE_VIS pair
  58. #if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
  59. : private __non_trivially_copyable_base<_T1, _T2>
  60. #endif
  61. {
  62. typedef _T1 first_type;
  63. typedef _T2 second_type;
  64. _T1 first;
  65. _T2 second;
  66. pair(pair const&) = default;
  67. pair(pair&&) = default;
  68. #ifdef _LIBCPP_CXX03_LANG
  69. _LIBCPP_INLINE_VISIBILITY
  70. pair() : first(), second() {}
  71. _LIBCPP_INLINE_VISIBILITY
  72. pair(_T1 const& __t1, _T2 const& __t2) : first(__t1), second(__t2) {}
  73. template <class _U1, class _U2>
  74. _LIBCPP_INLINE_VISIBILITY
  75. pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
  76. _LIBCPP_INLINE_VISIBILITY
  77. pair& operator=(pair const& __p) {
  78. first = __p.first;
  79. second = __p.second;
  80. return *this;
  81. }
  82. #else
  83. struct _CheckArgs {
  84. template <int&...>
  85. static constexpr bool __enable_explicit_default() {
  86. return is_default_constructible<_T1>::value
  87. && is_default_constructible<_T2>::value
  88. && !__enable_implicit_default<>();
  89. }
  90. template <int&...>
  91. static constexpr bool __enable_implicit_default() {
  92. return __is_implicitly_default_constructible<_T1>::value
  93. && __is_implicitly_default_constructible<_T2>::value;
  94. }
  95. template <class _U1, class _U2>
  96. static constexpr bool __is_pair_constructible() {
  97. return is_constructible<first_type, _U1>::value
  98. && is_constructible<second_type, _U2>::value;
  99. }
  100. template <class _U1, class _U2>
  101. static constexpr bool __is_implicit() {
  102. return is_convertible<_U1, first_type>::value
  103. && is_convertible<_U2, second_type>::value;
  104. }
  105. template <class _U1, class _U2>
  106. static constexpr bool __enable_explicit() {
  107. return __is_pair_constructible<_U1, _U2>() && !__is_implicit<_U1, _U2>();
  108. }
  109. template <class _U1, class _U2>
  110. static constexpr bool __enable_implicit() {
  111. return __is_pair_constructible<_U1, _U2>() && __is_implicit<_U1, _U2>();
  112. }
  113. };
  114. template <bool _MaybeEnable>
  115. using _CheckArgsDep _LIBCPP_NODEBUG = typename conditional<
  116. _MaybeEnable, _CheckArgs, __check_tuple_constructor_fail>::type;
  117. struct _CheckTupleLikeConstructor {
  118. template <class _Tuple>
  119. static constexpr bool __enable_implicit() {
  120. return __tuple_convertible<_Tuple, pair>::value;
  121. }
  122. template <class _Tuple>
  123. static constexpr bool __enable_explicit() {
  124. return __tuple_constructible<_Tuple, pair>::value
  125. && !__tuple_convertible<_Tuple, pair>::value;
  126. }
  127. template <class _Tuple>
  128. static constexpr bool __enable_assign() {
  129. return __tuple_assignable<_Tuple, pair>::value;
  130. }
  131. };
  132. template <class _Tuple>
  133. using _CheckTLC _LIBCPP_NODEBUG = __conditional_t<
  134. __tuple_like_with_size<_Tuple, 2>::value
  135. && !is_same<typename decay<_Tuple>::type, pair>::value,
  136. _CheckTupleLikeConstructor,
  137. __check_tuple_constructor_fail
  138. >;
  139. template<bool _Dummy = true, typename enable_if<
  140. _CheckArgsDep<_Dummy>::__enable_explicit_default()
  141. >::type* = nullptr>
  142. explicit _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  143. pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
  144. is_nothrow_default_constructible<second_type>::value)
  145. : first(), second() {}
  146. template<bool _Dummy = true, typename enable_if<
  147. _CheckArgsDep<_Dummy>::__enable_implicit_default()
  148. >::type* = nullptr>
  149. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  150. pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
  151. is_nothrow_default_constructible<second_type>::value)
  152. : first(), second() {}
  153. template <bool _Dummy = true, typename enable_if<
  154. _CheckArgsDep<_Dummy>::template __enable_explicit<_T1 const&, _T2 const&>()
  155. >::type* = nullptr>
  156. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  157. explicit pair(_T1 const& __t1, _T2 const& __t2)
  158. _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
  159. is_nothrow_copy_constructible<second_type>::value)
  160. : first(__t1), second(__t2) {}
  161. template<bool _Dummy = true, typename enable_if<
  162. _CheckArgsDep<_Dummy>::template __enable_implicit<_T1 const&, _T2 const&>()
  163. >::type* = nullptr>
  164. _LIBCPP_INLINE_VISIBILITY _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 > 20 // http://wg21.link/P1951
  171. class _U1 = _T1, class _U2 = _T2,
  172. #else
  173. class _U1, class _U2,
  174. #endif
  175. typename enable_if<_CheckArgs::template __enable_explicit<_U1, _U2>()>::type* = nullptr
  176. >
  177. _LIBCPP_INLINE_VISIBILITY _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(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
  182. template <
  183. #if _LIBCPP_STD_VER > 20 // http://wg21.link/P1951
  184. class _U1 = _T1, class _U2 = _T2,
  185. #else
  186. class _U1, class _U2,
  187. #endif
  188. typename enable_if<_CheckArgs::template __enable_implicit<_U1, _U2>()>::type* = nullptr
  189. >
  190. _LIBCPP_INLINE_VISIBILITY _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(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
  195. #if _LIBCPP_STD_VER > 20
  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, typename enable_if<
  206. _CheckArgs::template __enable_explicit<_U1 const&, _U2 const&>()
  207. >::type* = nullptr>
  208. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  209. explicit pair(pair<_U1, _U2> const& __p)
  210. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
  211. is_nothrow_constructible<second_type, _U2 const&>::value))
  212. : first(__p.first), second(__p.second) {}
  213. template<class _U1, class _U2, typename enable_if<
  214. _CheckArgs::template __enable_implicit<_U1 const&, _U2 const&>()
  215. >::type* = nullptr>
  216. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  217. pair(pair<_U1, _U2> const& __p)
  218. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
  219. is_nothrow_constructible<second_type, _U2 const&>::value))
  220. : first(__p.first), second(__p.second) {}
  221. template<class _U1, class _U2, typename enable_if<
  222. _CheckArgs::template __enable_explicit<_U1, _U2>()
  223. >::type* = nullptr>
  224. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  225. explicit pair(pair<_U1, _U2>&&__p)
  226. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
  227. is_nothrow_constructible<second_type, _U2&&>::value))
  228. : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
  229. template<class _U1, class _U2, typename enable_if<
  230. _CheckArgs::template __enable_implicit<_U1, _U2>()
  231. >::type* = nullptr>
  232. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  233. pair(pair<_U1, _U2>&& __p)
  234. _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
  235. is_nothrow_constructible<second_type, _U2&&>::value))
  236. : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
  237. #if _LIBCPP_STD_VER > 20
  238. template<class _U1, class _U2, __enable_if_t<
  239. _CheckArgs::template __is_pair_constructible<const _U1&&, const _U2&&>()
  240. >* = nullptr>
  241. _LIBCPP_HIDE_FROM_ABI constexpr
  242. explicit(!_CheckArgs::template __is_implicit<const _U1&&, const _U2&&>())
  243. pair(const pair<_U1, _U2>&& __p)
  244. noexcept(is_nothrow_constructible<first_type, const _U1&&>::value &&
  245. is_nothrow_constructible<second_type, const _U2&&>::value)
  246. : first(std::move(__p.first)), second(std::move(__p.second)) {}
  247. #endif
  248. template<class _Tuple, typename enable_if<
  249. _CheckTLC<_Tuple>::template __enable_explicit<_Tuple>()
  250. >::type* = nullptr>
  251. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  252. explicit pair(_Tuple&& __p)
  253. : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
  254. second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
  255. template<class _Tuple, typename enable_if<
  256. _CheckTLC<_Tuple>::template __enable_implicit<_Tuple>()
  257. >::type* = nullptr>
  258. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  259. pair(_Tuple&& __p)
  260. : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
  261. second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
  262. template <class... _Args1, class... _Args2>
  263. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  264. pair(piecewise_construct_t __pc,
  265. tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
  266. _NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value &&
  267. is_nothrow_constructible<second_type, _Args2...>::value))
  268. : pair(__pc, __first_args, __second_args,
  269. typename __make_tuple_indices<sizeof...(_Args1)>::type(),
  270. typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
  271. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  272. pair& operator=(__conditional_t<
  273. is_copy_assignable<first_type>::value &&
  274. is_copy_assignable<second_type>::value,
  275. pair, __nat> const& __p)
  276. _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
  277. is_nothrow_copy_assignable<second_type>::value)
  278. {
  279. first = __p.first;
  280. second = __p.second;
  281. return *this;
  282. }
  283. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  284. pair& operator=(__conditional_t<
  285. is_move_assignable<first_type>::value &&
  286. is_move_assignable<second_type>::value,
  287. pair, __nat>&& __p)
  288. _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
  289. is_nothrow_move_assignable<second_type>::value)
  290. {
  291. first = _VSTD::forward<first_type>(__p.first);
  292. second = _VSTD::forward<second_type>(__p.second);
  293. return *this;
  294. }
  295. #if _LIBCPP_STD_VER > 20
  296. _LIBCPP_HIDE_FROM_ABI constexpr
  297. const pair& operator=(pair const& __p) const
  298. noexcept(is_nothrow_copy_assignable_v<const first_type> &&
  299. is_nothrow_copy_assignable_v<const second_type>)
  300. requires(is_copy_assignable_v<const first_type> &&
  301. is_copy_assignable_v<const second_type>) {
  302. first = __p.first;
  303. second = __p.second;
  304. return *this;
  305. }
  306. _LIBCPP_HIDE_FROM_ABI constexpr
  307. const pair& operator=(pair&& __p) const
  308. noexcept(is_nothrow_assignable_v<const first_type&, first_type> &&
  309. is_nothrow_assignable_v<const second_type&, second_type>)
  310. requires(is_assignable_v<const first_type&, first_type> &&
  311. is_assignable_v<const second_type&, second_type>) {
  312. first = std::forward<first_type>(__p.first);
  313. second = std::forward<second_type>(__p.second);
  314. return *this;
  315. }
  316. template<class _U1, class _U2>
  317. _LIBCPP_HIDE_FROM_ABI constexpr
  318. const pair& operator=(const pair<_U1, _U2>& __p) const
  319. requires(is_assignable_v<const first_type&, const _U1&> &&
  320. is_assignable_v<const second_type&, const _U2&>) {
  321. first = __p.first;
  322. second = __p.second;
  323. return *this;
  324. }
  325. template<class _U1, class _U2>
  326. _LIBCPP_HIDE_FROM_ABI constexpr
  327. const pair& operator=(pair<_U1, _U2>&& __p) const
  328. requires(is_assignable_v<const first_type&, _U1> &&
  329. is_assignable_v<const second_type&, _U2>) {
  330. first = std::forward<_U1>(__p.first);
  331. second = std::forward<_U2>(__p.second);
  332. return *this;
  333. }
  334. #endif // _LIBCPP_STD_VER > 20
  335. template <class _Tuple, typename enable_if<
  336. _CheckTLC<_Tuple>::template __enable_assign<_Tuple>()
  337. >::type* = nullptr>
  338. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  339. pair& operator=(_Tuple&& __p) {
  340. first = _VSTD::get<0>(_VSTD::forward<_Tuple>(__p));
  341. second = _VSTD::get<1>(_VSTD::forward<_Tuple>(__p));
  342. return *this;
  343. }
  344. #endif
  345. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  346. void
  347. swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
  348. __is_nothrow_swappable<second_type>::value)
  349. {
  350. using _VSTD::swap;
  351. swap(first, __p.first);
  352. swap(second, __p.second);
  353. }
  354. #if _LIBCPP_STD_VER > 20
  355. _LIBCPP_HIDE_FROM_ABI constexpr
  356. void swap(const pair& __p) const
  357. noexcept(__is_nothrow_swappable<const first_type>::value &&
  358. __is_nothrow_swappable<const second_type>::value)
  359. {
  360. using std::swap;
  361. swap(first, __p.first);
  362. swap(second, __p.second);
  363. }
  364. #endif
  365. private:
  366. #ifndef _LIBCPP_CXX03_LANG
  367. template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
  368. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  369. pair(piecewise_construct_t,
  370. tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
  371. __tuple_indices<_I1...>, __tuple_indices<_I2...>);
  372. #endif
  373. };
  374. #if _LIBCPP_STD_VER > 14
  375. template<class _T1, class _T2>
  376. pair(_T1, _T2) -> pair<_T1, _T2>;
  377. #endif
  378. // [pairs.spec], specialized algorithms
  379. template <class _T1, class _T2>
  380. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  381. bool
  382. operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  383. {
  384. return __x.first == __y.first && __x.second == __y.second;
  385. }
  386. #if _LIBCPP_STD_VER > 17
  387. template <class _T1, class _T2>
  388. _LIBCPP_HIDE_FROM_ABI constexpr
  389. common_comparison_category_t<
  390. __synth_three_way_result<_T1>,
  391. __synth_three_way_result<_T2> >
  392. operator<=>(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  393. {
  394. if (auto __c = _VSTD::__synth_three_way(__x.first, __y.first); __c != 0) {
  395. return __c;
  396. }
  397. return _VSTD::__synth_three_way(__x.second, __y.second);
  398. }
  399. #else // _LIBCPP_STD_VER > 17
  400. template <class _T1, class _T2>
  401. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  402. bool
  403. operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  404. {
  405. return !(__x == __y);
  406. }
  407. template <class _T1, class _T2>
  408. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  409. bool
  410. operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  411. {
  412. return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
  413. }
  414. template <class _T1, class _T2>
  415. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  416. bool
  417. operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  418. {
  419. return __y < __x;
  420. }
  421. template <class _T1, class _T2>
  422. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  423. bool
  424. operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  425. {
  426. return !(__x < __y);
  427. }
  428. template <class _T1, class _T2>
  429. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  430. bool
  431. operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  432. {
  433. return !(__y < __x);
  434. }
  435. #endif // _LIBCPP_STD_VER > 17
  436. #if _LIBCPP_STD_VER > 20
  437. template <class _T1, class _T2, class _U1, class _U2, template<class> class _TQual, template<class> class _UQual>
  438. requires requires { typename pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
  439. common_reference_t<_TQual<_T2>, _UQual<_U2>>>; }
  440. struct basic_common_reference<pair<_T1, _T2>, pair<_U1, _U2>, _TQual, _UQual> {
  441. using type = pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
  442. common_reference_t<_TQual<_T2>, _UQual<_U2>>>;
  443. };
  444. template <class _T1, class _T2, class _U1, class _U2>
  445. requires requires { typename pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>; }
  446. struct common_type<pair<_T1, _T2>, pair<_U1, _U2>> {
  447. using type = pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>;
  448. };
  449. #endif // _LIBCPP_STD_VER > 20
  450. template <class _T1, class _T2>
  451. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  452. typename enable_if
  453. <
  454. __is_swappable<_T1>::value &&
  455. __is_swappable<_T2>::value,
  456. void
  457. >::type
  458. swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
  459. _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
  460. __is_nothrow_swappable<_T2>::value))
  461. {
  462. __x.swap(__y);
  463. }
  464. #if _LIBCPP_STD_VER > 20
  465. template <class _T1, class _T2>
  466. requires (__is_swappable<const _T1>::value &&
  467. __is_swappable<const _T2>::value)
  468. _LIBCPP_HIDE_FROM_ABI constexpr
  469. void swap(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
  470. noexcept(noexcept(__x.swap(__y)))
  471. {
  472. __x.swap(__y);
  473. }
  474. #endif
  475. template <class _T1, class _T2>
  476. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  477. pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
  478. make_pair(_T1&& __t1, _T2&& __t2)
  479. {
  480. return pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
  481. (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
  482. }
  483. template <class _T1, class _T2>
  484. struct _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
  485. : public integral_constant<size_t, 2> {};
  486. template <size_t _Ip, class _T1, class _T2>
  487. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
  488. {
  489. static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
  490. };
  491. template <class _T1, class _T2>
  492. struct _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
  493. {
  494. typedef _LIBCPP_NODEBUG _T1 type;
  495. };
  496. template <class _T1, class _T2>
  497. struct _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> >
  498. {
  499. typedef _LIBCPP_NODEBUG _T2 type;
  500. };
  501. template <size_t _Ip> struct __get_pair;
  502. template <>
  503. struct __get_pair<0>
  504. {
  505. template <class _T1, class _T2>
  506. static
  507. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  508. _T1&
  509. get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
  510. template <class _T1, class _T2>
  511. static
  512. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  513. const _T1&
  514. get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
  515. template <class _T1, class _T2>
  516. static
  517. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  518. _T1&&
  519. get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
  520. template <class _T1, class _T2>
  521. static
  522. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  523. const _T1&&
  524. get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T1>(__p.first);}
  525. };
  526. template <>
  527. struct __get_pair<1>
  528. {
  529. template <class _T1, class _T2>
  530. static
  531. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  532. _T2&
  533. get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
  534. template <class _T1, class _T2>
  535. static
  536. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  537. const _T2&
  538. get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
  539. template <class _T1, class _T2>
  540. static
  541. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  542. _T2&&
  543. get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
  544. template <class _T1, class _T2>
  545. static
  546. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  547. const _T2&&
  548. get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T2>(__p.second);}
  549. };
  550. template <size_t _Ip, class _T1, class _T2>
  551. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  552. typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  553. get(pair<_T1, _T2>& __p) _NOEXCEPT
  554. {
  555. return __get_pair<_Ip>::get(__p);
  556. }
  557. template <size_t _Ip, class _T1, class _T2>
  558. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  559. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  560. get(const pair<_T1, _T2>& __p) _NOEXCEPT
  561. {
  562. return __get_pair<_Ip>::get(__p);
  563. }
  564. template <size_t _Ip, class _T1, class _T2>
  565. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  566. typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  567. get(pair<_T1, _T2>&& __p) _NOEXCEPT
  568. {
  569. return __get_pair<_Ip>::get(_VSTD::move(__p));
  570. }
  571. template <size_t _Ip, class _T1, class _T2>
  572. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  573. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  574. get(const pair<_T1, _T2>&& __p) _NOEXCEPT
  575. {
  576. return __get_pair<_Ip>::get(_VSTD::move(__p));
  577. }
  578. #if _LIBCPP_STD_VER > 11
  579. template <class _T1, class _T2>
  580. inline _LIBCPP_INLINE_VISIBILITY
  581. constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
  582. {
  583. return __get_pair<0>::get(__p);
  584. }
  585. template <class _T1, class _T2>
  586. inline _LIBCPP_INLINE_VISIBILITY
  587. constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
  588. {
  589. return __get_pair<0>::get(__p);
  590. }
  591. template <class _T1, class _T2>
  592. inline _LIBCPP_INLINE_VISIBILITY
  593. constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
  594. {
  595. return __get_pair<0>::get(_VSTD::move(__p));
  596. }
  597. template <class _T1, class _T2>
  598. inline _LIBCPP_INLINE_VISIBILITY
  599. constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
  600. {
  601. return __get_pair<0>::get(_VSTD::move(__p));
  602. }
  603. template <class _T1, class _T2>
  604. inline _LIBCPP_INLINE_VISIBILITY
  605. constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
  606. {
  607. return __get_pair<1>::get(__p);
  608. }
  609. template <class _T1, class _T2>
  610. inline _LIBCPP_INLINE_VISIBILITY
  611. constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
  612. {
  613. return __get_pair<1>::get(__p);
  614. }
  615. template <class _T1, class _T2>
  616. inline _LIBCPP_INLINE_VISIBILITY
  617. constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
  618. {
  619. return __get_pair<1>::get(_VSTD::move(__p));
  620. }
  621. template <class _T1, class _T2>
  622. inline _LIBCPP_INLINE_VISIBILITY
  623. constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
  624. {
  625. return __get_pair<1>::get(_VSTD::move(__p));
  626. }
  627. #endif // _LIBCPP_STD_VER > 11
  628. _LIBCPP_END_NAMESPACE_STD
  629. #endif // _LIBCPP___UTILITY_PAIR_H