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