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