libstdc++
stl_pair.h
Go to the documentation of this file.
1// Pair implementation -*- C++ -*-
2
3// Copyright (C) 2001-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file bits/stl_pair.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{utility}
54 */
55
56#ifndef _STL_PAIR_H
57#define _STL_PAIR_H 1
58
59#if __cplusplus >= 201103L
60# include <type_traits> // for std::__decay_and_strip
61# include <bits/move.h> // for std::move / std::forward, and std::swap
62# include <bits/utility.h> // for std::tuple_element, std::tuple_size
63#endif
64#if __cplusplus >= 202002L
65# include <compare>
66#endif
67
68namespace std _GLIBCXX_VISIBILITY(default)
69{
70_GLIBCXX_BEGIN_NAMESPACE_VERSION
71
72 /**
73 * @addtogroup utilities
74 * @{
75 */
76
77#if __cplusplus >= 201103L
78 /// Tag type for piecewise construction of std::pair objects.
79 struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
80
81 /// Tag for piecewise construction of std::pair objects.
82 _GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct =
84
85 /// @cond undocumented
86
87 // Forward declarations.
88 template<typename _T1, typename _T2>
89 struct pair;
90
91 template<typename...>
92 class tuple;
93
94 // Declarations of std::array and its std::get overloads, so that
95 // std::tuple_cat can use them if <tuple> is included before <array>.
96 // We also declare the other std::get overloads here so that they're
97 // visible to the P2165R4 tuple-like constructors of pair and tuple.
98 template<typename _Tp, size_t _Nm>
99 struct array;
100
101 template<size_t...>
102 struct _Index_tuple;
103
104 template<size_t _Int, class _Tp1, class _Tp2>
105 constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
106 get(pair<_Tp1, _Tp2>& __in) noexcept;
107
108 template<size_t _Int, class _Tp1, class _Tp2>
109 constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
110 get(pair<_Tp1, _Tp2>&& __in) noexcept;
111
112 template<size_t _Int, class _Tp1, class _Tp2>
113 constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
114 get(const pair<_Tp1, _Tp2>& __in) noexcept;
115
116 template<size_t _Int, class _Tp1, class _Tp2>
117 constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
118 get(const pair<_Tp1, _Tp2>&& __in) noexcept;
119
120 template<size_t __i, typename... _Elements>
121 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
122 get(tuple<_Elements...>& __t) noexcept;
123
124 template<size_t __i, typename... _Elements>
125 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
126 get(const tuple<_Elements...>& __t) noexcept;
127
128 template<size_t __i, typename... _Elements>
129 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
130 get(tuple<_Elements...>&& __t) noexcept;
131
132 template<size_t __i, typename... _Elements>
133 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&&
134 get(const tuple<_Elements...>&& __t) noexcept;
135
136 template<size_t _Int, typename _Tp, size_t _Nm>
137 constexpr _Tp&
138 get(array<_Tp, _Nm>&) noexcept;
139
140 template<size_t _Int, typename _Tp, size_t _Nm>
141 constexpr _Tp&&
142 get(array<_Tp, _Nm>&&) noexcept;
143
144 template<size_t _Int, typename _Tp, size_t _Nm>
145 constexpr const _Tp&
146 get(const array<_Tp, _Nm>&) noexcept;
147
148 template<size_t _Int, typename _Tp, size_t _Nm>
149 constexpr const _Tp&&
150 get(const array<_Tp, _Nm>&&) noexcept;
151
152#if ! __cpp_lib_concepts
153 // Concept utility functions, reused in conditionally-explicit
154 // constructors.
155 // See PR 70437, don't look at is_constructible or
156 // is_convertible if the types are the same to
157 // avoid querying those properties for incomplete types.
158 template <bool, typename _T1, typename _T2>
159 struct _PCC
160 {
161 template <typename _U1, typename _U2>
162 static constexpr bool _ConstructiblePair()
163 {
164 return __and_<is_constructible<_T1, const _U1&>,
166 }
167
168 template <typename _U1, typename _U2>
169 static constexpr bool _ImplicitlyConvertiblePair()
170 {
171 return __and_<is_convertible<const _U1&, _T1>,
172 is_convertible<const _U2&, _T2>>::value;
173 }
174
175 template <typename _U1, typename _U2>
176 static constexpr bool _MoveConstructiblePair()
177 {
178 return __and_<is_constructible<_T1, _U1&&>,
179 is_constructible<_T2, _U2&&>>::value;
180 }
181
182 template <typename _U1, typename _U2>
183 static constexpr bool _ImplicitlyMoveConvertiblePair()
184 {
185 return __and_<is_convertible<_U1&&, _T1>,
186 is_convertible<_U2&&, _T2>>::value;
187 }
188 };
189
190 template <typename _T1, typename _T2>
191 struct _PCC<false, _T1, _T2>
192 {
193 template <typename _U1, typename _U2>
194 static constexpr bool _ConstructiblePair()
195 {
196 return false;
197 }
198
199 template <typename _U1, typename _U2>
200 static constexpr bool _ImplicitlyConvertiblePair()
201 {
202 return false;
203 }
204
205 template <typename _U1, typename _U2>
206 static constexpr bool _MoveConstructiblePair()
207 {
208 return false;
209 }
210
211 template <typename _U1, typename _U2>
212 static constexpr bool _ImplicitlyMoveConvertiblePair()
213 {
214 return false;
215 }
216 };
217#endif // lib concepts
218#endif // C++11
219
220#if __glibcxx_tuple_like // >= C++23
221 template<typename _Tp>
222 inline constexpr bool __is_tuple_v = false;
223
224 template<typename... _Ts>
225 inline constexpr bool __is_tuple_v<tuple<_Ts...>> = true;
226
227 // TODO: Reuse __is_tuple_like from <type_traits>?
228 template<typename _Tp>
229 inline constexpr bool __is_tuple_like_v = false;
230
231 template<typename... _Elements>
232 inline constexpr bool __is_tuple_like_v<tuple<_Elements...>> = true;
233
234 template<typename _T1, typename _T2>
235 inline constexpr bool __is_tuple_like_v<pair<_T1, _T2>> = true;
236
237 template<typename _Tp, size_t _Nm>
238 inline constexpr bool __is_tuple_like_v<array<_Tp, _Nm>> = true;
239
240 // __is_tuple_like_v<subrange> is defined in <bits/ranges_util.h>.
241
242 template<typename _Tp>
243 concept __tuple_like = __is_tuple_like_v<remove_cvref_t<_Tp>>;
244
245 template<typename _Tp>
246 concept __pair_like = __tuple_like<_Tp> && tuple_size_v<remove_cvref_t<_Tp>> == 2;
247
248 template<typename _Tp, typename _Tuple>
249 concept __eligible_tuple_like
250 = __detail::__different_from<_Tp, _Tuple> && __tuple_like<_Tp>
251 && (tuple_size_v<remove_cvref_t<_Tp>> == tuple_size_v<_Tuple>)
252 && !ranges::__detail::__is_subrange<remove_cvref_t<_Tp>>;
253
254 template<typename _Tp, typename _Pair>
255 concept __eligible_pair_like
256 = __detail::__different_from<_Tp, _Pair> && __pair_like<_Tp>
257 && !ranges::__detail::__is_subrange<remove_cvref_t<_Tp>>;
258#endif // C++23
259
260 template<typename _U1, typename _U2> class __pair_base
261 {
262#if __cplusplus >= 201103L && ! __cpp_lib_concepts
263 template<typename _T1, typename _T2> friend struct pair;
264 __pair_base() = default;
265 ~__pair_base() = default;
266 __pair_base(const __pair_base&) = default;
267 __pair_base& operator=(const __pair_base&) = delete;
268#endif // C++11
269 };
270
271 /// @endcond
272
273 /**
274 * @brief Struct holding two objects of arbitrary type.
275 *
276 * @tparam _T1 Type of first object.
277 * @tparam _T2 Type of second object.
278 *
279 * <https://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
280 *
281 * @headerfile utility
282 */
283 template<typename _T1, typename _T2>
284 struct pair
285 : public __pair_base<_T1, _T2>
286 {
287 typedef _T1 first_type; ///< The type of the `first` member
288 typedef _T2 second_type; ///< The type of the `second` member
289
290 _T1 first; ///< The first member
291 _T2 second; ///< The second member
292
293#if __cplusplus >= 201103L
294 constexpr pair(const pair&) = default; ///< Copy constructor
295 constexpr pair(pair&&) = default; ///< Move constructor
296
297 template<typename... _Args1, typename... _Args2>
298 _GLIBCXX20_CONSTEXPR
300
301 /// Swap the first members and then the second members.
302 _GLIBCXX20_CONSTEXPR void
303 swap(pair& __p)
304 noexcept(__and_<__is_nothrow_swappable<_T1>,
305 __is_nothrow_swappable<_T2>>::value)
306 {
307 using std::swap;
308 swap(first, __p.first);
309 swap(second, __p.second);
310 }
311
312#if __glibcxx_ranges_zip // >= C++23
313 // As an extension, we constrain the const swap member function in order
314 // to continue accepting explicit instantiation of pairs whose elements
315 // are not all const swappable. Without this constraint, such an
316 // explicit instantiation would also instantiate the ill-formed body of
317 // this function and yield a hard error. This constraint shouldn't
318 // affect the behavior of valid programs.
319 constexpr void
320 swap(const pair& __p) const
321 noexcept(__and_v<__is_nothrow_swappable<const _T1>,
322 __is_nothrow_swappable<const _T2>>)
323 requires is_swappable_v<const _T1> && is_swappable_v<const _T2>
324 {
325 using std::swap;
326 swap(first, __p.first);
327 swap(second, __p.second);
328 }
329#endif // C++23
330
331 private:
332 template<typename... _Args1, size_t... _Indexes1,
333 typename... _Args2, size_t... _Indexes2>
334 _GLIBCXX20_CONSTEXPR
335 pair(tuple<_Args1...>&, tuple<_Args2...>&,
336 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
337 public:
338
339#if __cpp_lib_concepts
340 // C++20 implementation using concepts, explicit(bool), fully constexpr.
341
342 /// Default constructor
343 constexpr
344 explicit(__not_<__and_<__is_implicitly_default_constructible<_T1>,
345 __is_implicitly_default_constructible<_T2>>>())
346 pair()
347 noexcept(is_nothrow_default_constructible_v<_T1>
348 && is_nothrow_default_constructible_v<_T2>)
349 requires is_default_constructible_v<_T1>
350 && is_default_constructible_v<_T2>
351 : first(), second()
352 { }
353
354 private:
355
356 /// @cond undocumented
357 template<typename _U1, typename _U2>
358 static constexpr bool
359 _S_constructible()
360 {
361 if constexpr (is_constructible_v<_T1, _U1>)
362 return is_constructible_v<_T2, _U2>;
363 return false;
364 }
365
366 template<typename _U1, typename _U2>
367 static constexpr bool
368 _S_nothrow_constructible()
369 {
370 if constexpr (is_nothrow_constructible_v<_T1, _U1>)
371 return is_nothrow_constructible_v<_T2, _U2>;
372 return false;
373 }
374
375 template<typename _U1, typename _U2>
376 static constexpr bool
377 _S_convertible()
378 {
379 if constexpr (is_convertible_v<_U1, _T1>)
380 return is_convertible_v<_U2, _T2>;
381 return false;
382 }
383
384 // True if construction from _U1 and _U2 would create a dangling ref.
385 template<typename _U1, typename _U2>
386 static constexpr bool
387 _S_dangles()
388 {
389#if __has_builtin(__reference_constructs_from_temporary)
390 if constexpr (__reference_constructs_from_temporary(_T1, _U1&&))
391 return true;
392 else
393 return __reference_constructs_from_temporary(_T2, _U2&&);
394#else
395 return false;
396#endif
397 }
398
399#if __glibcxx_tuple_like // >= C++23
400 template<typename _UPair>
401 static constexpr bool
402 _S_constructible_from_pair_like()
403 {
404 return _S_constructible<decltype(std::get<0>(std::declval<_UPair>())),
405 decltype(std::get<1>(std::declval<_UPair>()))>();
406 }
407
408 template<typename _UPair>
409 static constexpr bool
410 _S_convertible_from_pair_like()
411 {
412 return _S_convertible<decltype(std::get<0>(std::declval<_UPair>())),
413 decltype(std::get<1>(std::declval<_UPair>()))>();
414 }
415
416 template<typename _UPair>
417 static constexpr bool
418 _S_dangles_from_pair_like()
419 {
420 return _S_dangles<decltype(std::get<0>(std::declval<_UPair>())),
421 decltype(std::get<1>(std::declval<_UPair>()))>();
422 }
423#endif // C++23
424 /// @endcond
425
426 public:
427
428 /// Constructor accepting lvalues of `first_type` and `second_type`
429 constexpr explicit(!_S_convertible<const _T1&, const _T2&>())
430 pair(const _T1& __x, const _T2& __y)
431 noexcept(_S_nothrow_constructible<const _T1&, const _T2&>())
432 requires (_S_constructible<const _T1&, const _T2&>())
433 : first(__x), second(__y)
434 { }
435
436 /// Constructor accepting two values of arbitrary types
437#if __cplusplus > 202002L
438 template<typename _U1 = _T1, typename _U2 = _T2>
439#else
440 template<typename _U1, typename _U2>
441#endif
442 requires (_S_constructible<_U1, _U2>()) && (!_S_dangles<_U1, _U2>())
443 constexpr explicit(!_S_convertible<_U1, _U2>())
444 pair(_U1&& __x, _U2&& __y)
445 noexcept(_S_nothrow_constructible<_U1, _U2>())
446 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
447 { }
448
449#if __cplusplus > 202002L
450 template<typename _U1 = _T1, typename _U2 = _T2>
451#else
452 template<typename _U1, typename _U2>
453#endif
454 requires (_S_constructible<_U1, _U2>()) && (_S_dangles<_U1, _U2>())
455 constexpr explicit(!_S_convertible<_U1, _U2>())
456 pair(_U1&&, _U2&&) = delete;
457
458 /// Converting constructor from a const `pair<U1, U2>` lvalue
459 template<typename _U1, typename _U2>
460 requires (_S_constructible<const _U1&, const _U2&>())
461 && (!_S_dangles<_U1, _U2>())
462 constexpr explicit(!_S_convertible<const _U1&, const _U2&>())
463 pair(const pair<_U1, _U2>& __p)
464 noexcept(_S_nothrow_constructible<const _U1&, const _U2&>())
465 : first(__p.first), second(__p.second)
466 { }
467
468 template<typename _U1, typename _U2>
469 requires (_S_constructible<const _U1&, const _U2&>())
470 && (_S_dangles<const _U1&, const _U2&>())
471 constexpr explicit(!_S_convertible<const _U1&, const _U2&>())
472 pair(const pair<_U1, _U2>&) = delete;
473
474 /// Converting constructor from a non-const `pair<U1, U2>` rvalue
475 template<typename _U1, typename _U2>
476 requires (_S_constructible<_U1, _U2>()) && (!_S_dangles<_U1, _U2>())
477 constexpr explicit(!_S_convertible<_U1, _U2>())
478 pair(pair<_U1, _U2>&& __p)
479 noexcept(_S_nothrow_constructible<_U1, _U2>())
480 : first(std::forward<_U1>(__p.first)),
481 second(std::forward<_U2>(__p.second))
482 { }
483
484 template<typename _U1, typename _U2>
485 requires (_S_constructible<_U1, _U2>()) && (_S_dangles<_U1, _U2>())
486 constexpr explicit(!_S_convertible<_U1, _U2>())
487 pair(pair<_U1, _U2>&&) = delete;
488
489#if __glibcxx_ranges_zip // >= C++23
490 /// Converting constructor from a non-const `pair<U1, U2>` lvalue
491 template<typename _U1, typename _U2>
492 requires (_S_constructible<_U1&, _U2&>()) && (!_S_dangles<_U1&, _U2&>())
493 constexpr explicit(!_S_convertible<_U1&, _U2&>())
494 pair(pair<_U1, _U2>& __p)
495 noexcept(_S_nothrow_constructible<_U1&, _U2&>())
496 : first(__p.first), second(__p.second)
497 { }
498
499 template<typename _U1, typename _U2>
500 requires (_S_constructible<_U1&, _U2&>()) && (_S_dangles<_U1&, _U2&>())
501 constexpr explicit(!_S_convertible<_U1&, _U2&>())
502 pair(pair<_U1, _U2>&) = delete;
503
504 /// Converting constructor from a const `pair<U1, U2>` rvalue
505 template<typename _U1, typename _U2>
506 requires (_S_constructible<const _U1, const _U2>())
507 && (!_S_dangles<const _U1, const _U2>())
508 constexpr explicit(!_S_convertible<const _U1, const _U2>())
509 pair(const pair<_U1, _U2>&& __p)
510 noexcept(_S_nothrow_constructible<const _U1, const _U2>())
511 : first(std::forward<const _U1>(__p.first)),
512 second(std::forward<const _U2>(__p.second))
513 { }
514
515 template<typename _U1, typename _U2>
516 requires (_S_constructible<const _U1, const _U2>())
517 && (_S_dangles<const _U1, const _U2>())
518 constexpr explicit(!_S_convertible<const _U1, const _U2>())
519 pair(const pair<_U1, _U2>&&) = delete;
520#endif // C++23
521
522#if __glibcxx_tuple_like // >= C++23
523 template<__eligible_pair_like<pair> _UPair>
524 requires (_S_constructible_from_pair_like<_UPair>())
525 && (!_S_dangles_from_pair_like<_UPair>())
526 constexpr explicit(!_S_convertible_from_pair_like<_UPair>())
527 pair(_UPair&& __p)
528 : first(std::get<0>(std::forward<_UPair>(__p))),
529 second(std::get<1>(std::forward<_UPair>(__p)))
530 { }
531
532 template<__eligible_pair_like<pair> _UPair>
533 requires (_S_constructible_from_pair_like<_UPair>())
534 && (_S_dangles_from_pair_like<_UPair>())
535 constexpr explicit(!_S_convertible_from_pair_like<_UPair>())
536 pair(_UPair&&) = delete;
537#endif // C++23
538
539 private:
540 /// @cond undocumented
541 template<typename _U1, typename _U2>
542 static constexpr bool
543 _S_assignable()
544 {
545 if constexpr (is_assignable_v<_T1&, _U1>)
546 return is_assignable_v<_T2&, _U2>;
547 return false;
548 }
549
550 template<typename _U1, typename _U2>
551 static constexpr bool
552 _S_const_assignable()
553 {
554 if constexpr (is_assignable_v<const _T1&, _U1>)
555 return is_assignable_v<const _T2&, _U2>;
556 return false;
557 }
558
559 template<typename _U1, typename _U2>
560 static constexpr bool
561 _S_nothrow_assignable()
562 {
563 if constexpr (is_nothrow_assignable_v<_T1&, _U1>)
564 return is_nothrow_assignable_v<_T2&, _U2>;
565 return false;
566 }
567
568#if __glibcxx_tuple_like // >= C++23
569 template<typename _UPair>
570 static constexpr bool
571 _S_assignable_from_tuple_like()
572 {
573 return _S_assignable<decltype(std::get<0>(std::declval<_UPair>())),
574 decltype(std::get<1>(std::declval<_UPair>()))>();
575 }
576
577 template<typename _UPair>
578 static constexpr bool
579 _S_const_assignable_from_tuple_like()
580 {
581 return _S_const_assignable<decltype(std::get<0>(std::declval<_UPair>())),
582 decltype(std::get<1>(std::declval<_UPair>()))>();
583 }
584#endif // C++23
585 /// @endcond
586
587 public:
588
589 pair& operator=(const pair&) = delete;
590
591 /// Copy assignment operator
592 constexpr pair&
593 operator=(const pair& __p)
594 noexcept(_S_nothrow_assignable<const _T1&, const _T2&>())
595 requires (_S_assignable<const _T1&, const _T2&>())
596 {
597 first = __p.first;
598 second = __p.second;
599 return *this;
600 }
601
602 /// Move assignment operator
603 constexpr pair&
604 operator=(pair&& __p)
605 noexcept(_S_nothrow_assignable<_T1, _T2>())
606 requires (_S_assignable<_T1, _T2>())
607 {
608 first = std::forward<first_type>(__p.first);
609 second = std::forward<second_type>(__p.second);
610 return *this;
611 }
612
613 /// Converting assignment from a const `pair<U1, U2>` lvalue
614 template<typename _U1, typename _U2>
615 constexpr pair&
616 operator=(const pair<_U1, _U2>& __p)
617 noexcept(_S_nothrow_assignable<const _U1&, const _U2&>())
618 requires (_S_assignable<const _U1&, const _U2&>())
619 {
620 first = __p.first;
621 second = __p.second;
622 return *this;
623 }
624
625 /// Converting assignment from a non-const `pair<U1, U2>` rvalue
626 template<typename _U1, typename _U2>
627 constexpr pair&
628 operator=(pair<_U1, _U2>&& __p)
629 noexcept(_S_nothrow_assignable<_U1, _U2>())
630 requires (_S_assignable<_U1, _U2>())
631 {
632 first = std::forward<_U1>(__p.first);
633 second = std::forward<_U2>(__p.second);
634 return *this;
635 }
636
637#if __glibcxx_ranges_zip // >= C++23
638 /// Copy assignment operator (const)
639 constexpr const pair&
640 operator=(const pair& __p) const
641 requires (_S_const_assignable<const first_type&, const second_type&>())
642 {
643 first = __p.first;
644 second = __p.second;
645 return *this;
646 }
647
648 /// Move assignment operator (const)
649 constexpr const pair&
650 operator=(pair&& __p) const
651 requires (_S_const_assignable<first_type, second_type>())
652 {
653 first = std::forward<first_type>(__p.first);
654 second = std::forward<second_type>(__p.second);
655 return *this;
656 }
657
658 /// Converting assignment from a const `pair<U1, U2>` lvalue
659 template<typename _U1, typename _U2>
660 constexpr const pair&
661 operator=(const pair<_U1, _U2>& __p) const
662 requires (_S_const_assignable<const _U1&, const _U2&>())
663 {
664 first = __p.first;
665 second = __p.second;
666 return *this;
667 }
668
669 /// Converting assignment from a non-const `pair<U1, U2>` rvalue
670 template<typename _U1, typename _U2>
671 constexpr const pair&
672 operator=(pair<_U1, _U2>&& __p) const
673 requires (_S_const_assignable<_U1, _U2>())
674 {
675 first = std::forward<_U1>(__p.first);
676 second = std::forward<_U2>(__p.second);
677 return *this;
678 }
679#endif // C++23
680
681#if __glibcxx_tuple_like // >= C++23
682 template<__eligible_pair_like<pair> _UPair>
683 requires (_S_assignable_from_tuple_like<_UPair>())
684 constexpr pair&
685 operator=(_UPair&& __p)
686 {
687 first = std::get<0>(std::forward<_UPair>(__p));
688 second = std::get<1>(std::forward<_UPair>(__p));
689 return *this;
690 }
691
692 template<__eligible_pair_like<pair> _UPair>
693 requires (_S_const_assignable_from_tuple_like<_UPair>())
694 constexpr const pair&
695 operator=(_UPair&& __p) const
696 {
697 first = std::get<0>(std::forward<_UPair>(__p));
698 second = std::get<1>(std::forward<_UPair>(__p));
699 return *this;
700 }
701#endif // C++23
702
703#else // !__cpp_lib_concepts
704 // C++11/14/17 implementation using enable_if, partially constexpr.
705
706 /// @cond undocumented
707 // Error if construction from _U1 and _U2 would create a dangling ref.
708#if __has_builtin(__reference_constructs_from_temporary) \
709 && defined _GLIBCXX_DEBUG
710# define __glibcxx_no_dangling_refs(_U1, _U2) \
711 static_assert(!__reference_constructs_from_temporary(_T1, _U1) \
712 && !__reference_constructs_from_temporary(_T2, _U2), \
713 "std::pair constructor creates a dangling reference")
714#else
715# define __glibcxx_no_dangling_refs(_U1, _U2)
716#endif
717 /// @endcond
718
719 /** The default constructor creates @c first and @c second using their
720 * respective default constructors. */
721 template <typename _U1 = _T1,
722 typename _U2 = _T2,
723 typename enable_if<__and_<
724 __is_implicitly_default_constructible<_U1>,
725 __is_implicitly_default_constructible<_U2>>
726 ::value, bool>::type = true>
727 constexpr pair()
728 : first(), second() { }
729
730 template <typename _U1 = _T1,
731 typename _U2 = _T2,
732 typename enable_if<__and_<
735 __not_<
736 __and_<__is_implicitly_default_constructible<_U1>,
737 __is_implicitly_default_constructible<_U2>>>>
738 ::value, bool>::type = false>
739 explicit constexpr pair()
740 : first(), second() { }
741
742 // Shortcut for constraining the templates that don't take pairs.
743 /// @cond undocumented
744 using _PCCP = _PCC<true, _T1, _T2>;
745 /// @endcond
746
747 /// Construct from two const lvalues, allowing implicit conversions.
748 template<typename _U1 = _T1, typename _U2=_T2, typename
749 enable_if<_PCCP::template
750 _ConstructiblePair<_U1, _U2>()
751 && _PCCP::template
752 _ImplicitlyConvertiblePair<_U1, _U2>(),
753 bool>::type=true>
754 constexpr pair(const _T1& __a, const _T2& __b)
755 : first(__a), second(__b) { }
756
757 /// Construct from two const lvalues, disallowing implicit conversions.
758 template<typename _U1 = _T1, typename _U2=_T2, typename
759 enable_if<_PCCP::template
760 _ConstructiblePair<_U1, _U2>()
761 && !_PCCP::template
762 _ImplicitlyConvertiblePair<_U1, _U2>(),
763 bool>::type=false>
764 explicit constexpr pair(const _T1& __a, const _T2& __b)
765 : first(__a), second(__b) { }
766
767 // Shortcut for constraining the templates that take pairs.
768 /// @cond undocumented
769 template <typename _U1, typename _U2>
770 using _PCCFP = _PCC<!is_same<_T1, _U1>::value
772 _T1, _T2>;
773 /// @endcond
774
775 template<typename _U1, typename _U2, typename
777 _ConstructiblePair<_U1, _U2>()
778 && _PCCFP<_U1, _U2>::template
779 _ImplicitlyConvertiblePair<_U1, _U2>(),
780 bool>::type=true>
781 constexpr pair(const pair<_U1, _U2>& __p)
782 : first(__p.first), second(__p.second)
783 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
784
785 template<typename _U1, typename _U2, typename
786 enable_if<_PCCFP<_U1, _U2>::template
787 _ConstructiblePair<_U1, _U2>()
788 && !_PCCFP<_U1, _U2>::template
789 _ImplicitlyConvertiblePair<_U1, _U2>(),
790 bool>::type=false>
791 explicit constexpr pair(const pair<_U1, _U2>& __p)
792 : first(__p.first), second(__p.second)
793 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
794
795#if _GLIBCXX_USE_DEPRECATED
796#if defined(__DEPRECATED)
797# define _GLIBCXX_DEPRECATED_PAIR_CTOR \
798 __attribute__ ((__deprecated__ ("use 'nullptr' instead of '0' to " \
799 "initialize std::pair of move-only " \
800 "type and pointer")))
801#else
802# define _GLIBCXX_DEPRECATED_PAIR_CTOR
803#endif
804
805 private:
806 /// @cond undocumented
807
808 // A type which can be constructed from literal zero, but not nullptr
809 struct __zero_as_null_pointer_constant
810 {
811 __zero_as_null_pointer_constant(int __zero_as_null_pointer_constant::*)
812 { }
813 template<typename _Tp,
814 typename = __enable_if_t<is_null_pointer<_Tp>::value>>
815 __zero_as_null_pointer_constant(_Tp) = delete;
816 };
817 /// @endcond
818 public:
819
820 // Deprecated extensions to DR 811.
821 // These allow construction from an rvalue and a literal zero,
822 // in cases where the standard says the zero should be deduced as int
823 template<typename _U1,
824 __enable_if_t<__and_<__not_<is_reference<_U1>>,
825 is_pointer<_T2>,
826 is_constructible<_T1, _U1>,
827 __not_<is_constructible<_T1, const _U1&>>,
828 is_convertible<_U1, _T1>>::value,
829 bool> = true>
830 _GLIBCXX_DEPRECATED_PAIR_CTOR
831 constexpr
832 pair(_U1&& __x, __zero_as_null_pointer_constant, ...)
833 : first(std::forward<_U1>(__x)), second(nullptr)
834 { __glibcxx_no_dangling_refs(_U1&&, std::nullptr_t); }
835
836 template<typename _U1,
837 __enable_if_t<__and_<__not_<is_reference<_U1>>,
838 is_pointer<_T2>,
839 is_constructible<_T1, _U1>,
840 __not_<is_constructible<_T1, const _U1&>>,
841 __not_<is_convertible<_U1, _T1>>>::value,
842 bool> = false>
843 _GLIBCXX_DEPRECATED_PAIR_CTOR
844 explicit constexpr
845 pair(_U1&& __x, __zero_as_null_pointer_constant, ...)
846 : first(std::forward<_U1>(__x)), second(nullptr)
847 { __glibcxx_no_dangling_refs(_U1&&, std::nullptr_t); }
848
849 template<typename _U2,
850 __enable_if_t<__and_<is_pointer<_T1>,
851 __not_<is_reference<_U2>>,
852 is_constructible<_T2, _U2>,
853 __not_<is_constructible<_T2, const _U2&>>,
854 is_convertible<_U2, _T2>>::value,
855 bool> = true>
856 _GLIBCXX_DEPRECATED_PAIR_CTOR
857 constexpr
858 pair(__zero_as_null_pointer_constant, _U2&& __y, ...)
859 : first(nullptr), second(std::forward<_U2>(__y))
860 { __glibcxx_no_dangling_refs(std::nullptr_t, _U2&&); }
861
862 template<typename _U2,
863 __enable_if_t<__and_<is_pointer<_T1>,
864 __not_<is_reference<_U2>>,
865 is_constructible<_T2, _U2>,
866 __not_<is_constructible<_T2, const _U2&>>,
867 __not_<is_convertible<_U2, _T2>>>::value,
868 bool> = false>
869 _GLIBCXX_DEPRECATED_PAIR_CTOR
870 explicit constexpr
871 pair(__zero_as_null_pointer_constant, _U2&& __y, ...)
872 : first(nullptr), second(std::forward<_U2>(__y))
873 { __glibcxx_no_dangling_refs(std::nullptr_t, _U2&&); }
874#undef _GLIBCXX_DEPRECATED_PAIR_CTOR
875#endif
876
877 template<typename _U1, typename _U2, typename
878 enable_if<_PCCP::template
879 _MoveConstructiblePair<_U1, _U2>()
880 && _PCCP::template
881 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
882 bool>::type=true>
883 constexpr pair(_U1&& __x, _U2&& __y)
884 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
885 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
886
887 template<typename _U1, typename _U2, typename
888 enable_if<_PCCP::template
889 _MoveConstructiblePair<_U1, _U2>()
890 && !_PCCP::template
891 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
892 bool>::type=false>
893 explicit constexpr pair(_U1&& __x, _U2&& __y)
894 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
895 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
896
897
898 template<typename _U1, typename _U2, typename
899 enable_if<_PCCFP<_U1, _U2>::template
900 _MoveConstructiblePair<_U1, _U2>()
901 && _PCCFP<_U1, _U2>::template
902 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
903 bool>::type=true>
904 constexpr pair(pair<_U1, _U2>&& __p)
905 : first(std::forward<_U1>(__p.first)),
906 second(std::forward<_U2>(__p.second))
907 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
908
909 template<typename _U1, typename _U2, typename
910 enable_if<_PCCFP<_U1, _U2>::template
911 _MoveConstructiblePair<_U1, _U2>()
912 && !_PCCFP<_U1, _U2>::template
913 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
914 bool>::type=false>
915 explicit constexpr pair(pair<_U1, _U2>&& __p)
916 : first(std::forward<_U1>(__p.first)),
917 second(std::forward<_U2>(__p.second))
918 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
919
920#undef __glibcxx_no_dangling_refs
921
922 pair&
923 operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
924 is_copy_assignable<_T2>>::value,
925 const pair&, const __nonesuch&> __p)
926 {
927 first = __p.first;
928 second = __p.second;
929 return *this;
930 }
931
932 pair&
933 operator=(__conditional_t<__and_<is_move_assignable<_T1>,
934 is_move_assignable<_T2>>::value,
935 pair&&, __nonesuch&&> __p)
936 noexcept(__and_<is_nothrow_move_assignable<_T1>,
937 is_nothrow_move_assignable<_T2>>::value)
938 {
939 first = std::forward<first_type>(__p.first);
940 second = std::forward<second_type>(__p.second);
941 return *this;
942 }
943
944 template<typename _U1, typename _U2>
945 typename enable_if<__and_<is_assignable<_T1&, const _U1&>,
946 is_assignable<_T2&, const _U2&>>::value,
947 pair&>::type
948 operator=(const pair<_U1, _U2>& __p)
949 {
950 first = __p.first;
951 second = __p.second;
952 return *this;
953 }
954
955 template<typename _U1, typename _U2>
956 typename enable_if<__and_<is_assignable<_T1&, _U1&&>,
957 is_assignable<_T2&, _U2&&>>::value,
958 pair&>::type
959 operator=(pair<_U1, _U2>&& __p)
960 {
961 first = std::forward<_U1>(__p.first);
962 second = std::forward<_U2>(__p.second);
963 return *this;
964 }
965#endif // lib concepts
966#else
967 // C++03 implementation
968
969 // _GLIBCXX_RESOLVE_LIB_DEFECTS
970 // 265. std::pair::pair() effects overly restrictive
971 /** The default constructor creates @c first and @c second using their
972 * respective default constructors. */
973 pair() : first(), second() { }
974
975 /// Two objects may be passed to a `pair` constructor to be copied.
976 pair(const _T1& __a, const _T2& __b)
977 : first(__a), second(__b) { }
978
979 /// Templated constructor to convert from other pairs.
980 template<typename _U1, typename _U2>
981 pair(const pair<_U1, _U2>& __p)
982 : first(__p.first), second(__p.second)
983 {
984#if __has_builtin(__reference_constructs_from_temporary)
985#pragma GCC diagnostic push
986#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
987 typedef int _DanglingCheck1[
988 __reference_constructs_from_temporary(_T1, const _U1&) ? -1 : 1
989 ];
990 typedef int _DanglingCheck2[
991 __reference_constructs_from_temporary(_T2, const _U2&) ? -1 : 1
992 ];
993#pragma GCC diagnostic pop
994#endif
995 }
996#endif // C++11
997 };
998
999 /// @relates pair @{
1000
1001#if __cpp_deduction_guides >= 201606
1002 template<typename _T1, typename _T2> pair(_T1, _T2) -> pair<_T1, _T2>;
1003#endif
1004
1005#if __cpp_lib_three_way_comparison
1006 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1007 // 3865. Sorting a range of pairs
1008
1009 /// Two pairs are equal iff their members are equal.
1010 template<typename _T1, typename _T2, typename _U1, typename _U2>
1011 [[nodiscard]]
1012 constexpr bool
1014 requires requires {
1015 { __x.first == __y.first } -> __detail::__boolean_testable;
1016 { __x.second == __y.second } -> __detail::__boolean_testable;
1017 }
1018 { return __x.first == __y.first && __x.second == __y.second; }
1019
1020 /** Defines a lexicographical order for pairs.
1021 *
1022 * For two pairs of comparable types, `P` is ordered before `Q` if
1023 * `P.first` is less than `Q.first`, or if `P.first` and `Q.first`
1024 * are equivalent (neither is less than the other) and `P.second` is
1025 * less than `Q.second`.
1026 */
1027 template<typename _T1, typename _T2, typename _U1, typename _U2>
1028 [[nodiscard]]
1029 constexpr common_comparison_category_t<__detail::__synth3way_t<_T1, _U1>,
1030 __detail::__synth3way_t<_T2, _U2>>
1031 operator<=>(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y)
1032 {
1033 if (auto __c = __detail::__synth3way(__x.first, __y.first); __c != 0)
1034 return __c;
1035 return __detail::__synth3way(__x.second, __y.second);
1036 }
1037#else
1038 /// Two pairs of the same type are equal iff their members are equal.
1039 template<typename _T1, typename _T2>
1040 _GLIBCXX_NODISCARD
1041 inline _GLIBCXX_CONSTEXPR bool
1042 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
1043 { return __x.first == __y.first && __x.second == __y.second; }
1044
1045 /** Defines a lexicographical order for pairs.
1046 *
1047 * For two pairs of the same type, `P` is ordered before `Q` if
1048 * `P.first` is less than `Q.first`, or if `P.first` and `Q.first`
1049 * are equivalent (neither is less than the other) and `P.second` is less
1050 * than `Q.second`.
1051 */
1052 template<typename _T1, typename _T2>
1053 _GLIBCXX_NODISCARD
1054 inline _GLIBCXX_CONSTEXPR bool
1055 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
1056 { return __x.first < __y.first
1057 || (!(__y.first < __x.first) && __x.second < __y.second); }
1058
1059 /// Uses @c operator== to find the result.
1060 template<typename _T1, typename _T2>
1061 _GLIBCXX_NODISCARD
1062 inline _GLIBCXX_CONSTEXPR bool
1063 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
1064 { return !(__x == __y); }
1065
1066 /// Uses @c operator< to find the result.
1067 template<typename _T1, typename _T2>
1068 _GLIBCXX_NODISCARD
1069 inline _GLIBCXX_CONSTEXPR bool
1070 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
1071 { return __y < __x; }
1072
1073 /// Uses @c operator< to find the result.
1074 template<typename _T1, typename _T2>
1075 _GLIBCXX_NODISCARD
1076 inline _GLIBCXX_CONSTEXPR bool
1077 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
1078 { return !(__y < __x); }
1079
1080 /// Uses @c operator< to find the result.
1081 template<typename _T1, typename _T2>
1082 _GLIBCXX_NODISCARD
1083 inline _GLIBCXX_CONSTEXPR bool
1084 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
1085 { return !(__x < __y); }
1086#endif // !(three_way_comparison && concepts)
1087
1088#if __cplusplus >= 201103L
1089 /** Swap overload for pairs. Calls std::pair::swap().
1090 *
1091 * @note This std::swap overload is not declared in C++03 mode,
1092 * which has performance implications, e.g. see https://gcc.gnu.org/PR38466
1093 */
1094 template<typename _T1, typename _T2>
1095 _GLIBCXX20_CONSTEXPR inline
1096#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1097 // Constrained free swap overload, see p0185r1
1098 typename enable_if<__and_<__is_swappable<_T1>,
1099 __is_swappable<_T2>>::value>::type
1100#else
1101 void
1102#endif
1104 noexcept(noexcept(__x.swap(__y)))
1105 { __x.swap(__y); }
1106
1107#if __glibcxx_ranges_zip // >= C++23
1108 template<typename _T1, typename _T2>
1109 requires is_swappable_v<const _T1> && is_swappable_v<const _T2>
1110 constexpr void
1111 swap(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
1112 noexcept(noexcept(__x.swap(__y)))
1113 { __x.swap(__y); }
1114#endif // C++23
1115
1116#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1117 template<typename _T1, typename _T2>
1118 typename enable_if<!__and_<__is_swappable<_T1>,
1119 __is_swappable<_T2>>::value>::type
1120 swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
1121#endif
1122#endif // __cplusplus >= 201103L
1123
1124 /// @} relates pair
1125
1126 /**
1127 * @brief A convenience wrapper for creating a pair from two objects.
1128 * @param __x The first object.
1129 * @param __y The second object.
1130 * @return A newly-constructed pair<> object of the appropriate type.
1131 *
1132 * The C++98 standard says the objects are passed by reference-to-const,
1133 * but C++03 says they are passed by value (this was LWG issue #181).
1134 *
1135 * Since C++11 they have been passed by forwarding reference and then
1136 * forwarded to the new members of the pair. To create a pair with a
1137 * member of reference type, pass a `reference_wrapper` to this function.
1138 */
1139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1140 // 181. make_pair() unintended behavior
1141#if __cplusplus >= 201103L
1142 // NB: DR 706.
1143 template<typename _T1, typename _T2>
1144 constexpr pair<typename __decay_and_strip<_T1>::__type,
1145 typename __decay_and_strip<_T2>::__type>
1146 make_pair(_T1&& __x, _T2&& __y)
1147 {
1148 typedef typename __decay_and_strip<_T1>::__type __ds_type1;
1149 typedef typename __decay_and_strip<_T2>::__type __ds_type2;
1150 typedef pair<__ds_type1, __ds_type2> __pair_type;
1151 return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
1152 }
1153#else
1154 template<typename _T1, typename _T2>
1155 inline pair<_T1, _T2>
1156 make_pair(_T1 __x, _T2 __y)
1157 { return pair<_T1, _T2>(__x, __y); }
1158#endif
1159
1160 /// @}
1161
1162#if __cplusplus >= 201103L
1163 // Various functions which give std::pair a tuple-like interface.
1164
1165 /// @cond undocumented
1166 template<typename _T1, typename _T2>
1167 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
1168 { };
1169 /// @endcond
1170
1171 /// Partial specialization for std::pair
1172 template<class _Tp1, class _Tp2>
1173 struct tuple_size<pair<_Tp1, _Tp2>>
1174 : public integral_constant<size_t, 2> { };
1175
1176 /// Partial specialization for std::pair
1177 template<class _Tp1, class _Tp2>
1178 struct tuple_element<0, pair<_Tp1, _Tp2>>
1179 { typedef _Tp1 type; };
1180
1181 /// Partial specialization for std::pair
1182 template<class _Tp1, class _Tp2>
1183 struct tuple_element<1, pair<_Tp1, _Tp2>>
1184 { typedef _Tp2 type; };
1185
1186#if __cplusplus >= 201703L
1187 template<typename _Tp1, typename _Tp2>
1188 inline constexpr size_t tuple_size_v<pair<_Tp1, _Tp2>> = 2;
1189
1190 template<typename _Tp1, typename _Tp2>
1191 inline constexpr size_t tuple_size_v<const pair<_Tp1, _Tp2>> = 2;
1192#endif
1193
1194#if __cplusplus >= 201103L
1195#pragma GCC diagnostic push
1196#pragma GCC diagnostic ignored "-Wc++14-extensions" // variable templates
1197#pragma GCC diagnostic ignored "-Wc++17-extensions" // inline variables
1198 template<typename _Tp>
1199 inline constexpr bool __is_pair = false;
1200
1201 template<typename _Tp, typename _Up>
1202 inline constexpr bool __is_pair<pair<_Tp, _Up>> = true;
1203#pragma GCC diagnostic pop
1204#endif
1205
1206 /// @cond undocumented
1207 template<size_t _Int>
1208 struct __pair_get;
1209
1210 template<>
1211 struct __pair_get<0>
1212 {
1213 template<typename _Tp1, typename _Tp2>
1214 static constexpr _Tp1&
1215 __get(pair<_Tp1, _Tp2>& __pair) noexcept
1216 { return __pair.first; }
1217
1218 template<typename _Tp1, typename _Tp2>
1219 static constexpr _Tp1&&
1220 __move_get(pair<_Tp1, _Tp2>&& __pair) noexcept
1221 { return std::forward<_Tp1>(__pair.first); }
1222
1223 template<typename _Tp1, typename _Tp2>
1224 static constexpr const _Tp1&
1225 __const_get(const pair<_Tp1, _Tp2>& __pair) noexcept
1226 { return __pair.first; }
1227
1228 template<typename _Tp1, typename _Tp2>
1229 static constexpr const _Tp1&&
1230 __const_move_get(const pair<_Tp1, _Tp2>&& __pair) noexcept
1231 { return std::forward<const _Tp1>(__pair.first); }
1232 };
1233
1234 template<>
1235 struct __pair_get<1>
1236 {
1237 template<typename _Tp1, typename _Tp2>
1238 static constexpr _Tp2&
1239 __get(pair<_Tp1, _Tp2>& __pair) noexcept
1240 { return __pair.second; }
1241
1242 template<typename _Tp1, typename _Tp2>
1243 static constexpr _Tp2&&
1244 __move_get(pair<_Tp1, _Tp2>&& __pair) noexcept
1245 { return std::forward<_Tp2>(__pair.second); }
1246
1247 template<typename _Tp1, typename _Tp2>
1248 static constexpr const _Tp2&
1249 __const_get(const pair<_Tp1, _Tp2>& __pair) noexcept
1250 { return __pair.second; }
1251
1252 template<typename _Tp1, typename _Tp2>
1253 static constexpr const _Tp2&&
1254 __const_move_get(const pair<_Tp1, _Tp2>&& __pair) noexcept
1255 { return std::forward<const _Tp2>(__pair.second); }
1256 };
1257 /// @endcond
1258
1259 /** @{
1260 * std::get overloads for accessing members of std::pair
1261 */
1262
1263 template<size_t _Int, class _Tp1, class _Tp2>
1264 constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
1265 get(pair<_Tp1, _Tp2>& __in) noexcept
1266 { return __pair_get<_Int>::__get(__in); }
1267
1268 template<size_t _Int, class _Tp1, class _Tp2>
1269 constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
1270 get(pair<_Tp1, _Tp2>&& __in) noexcept
1271 { return __pair_get<_Int>::__move_get(std::move(__in)); }
1272
1273 template<size_t _Int, class _Tp1, class _Tp2>
1274 constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
1275 get(const pair<_Tp1, _Tp2>& __in) noexcept
1276 { return __pair_get<_Int>::__const_get(__in); }
1277
1278 template<size_t _Int, class _Tp1, class _Tp2>
1279 constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
1280 get(const pair<_Tp1, _Tp2>&& __in) noexcept
1281 { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
1282
1283
1284#ifdef __glibcxx_tuples_by_type // C++ >= 14
1285 template <typename _Tp, typename _Up>
1286 constexpr _Tp&
1287 get(pair<_Tp, _Up>& __p) noexcept
1288 { return __p.first; }
1289
1290 template <typename _Tp, typename _Up>
1291 constexpr const _Tp&
1292 get(const pair<_Tp, _Up>& __p) noexcept
1293 { return __p.first; }
1294
1295 template <typename _Tp, typename _Up>
1296 constexpr _Tp&&
1297 get(pair<_Tp, _Up>&& __p) noexcept
1298 { return std::move(__p.first); }
1299
1300 template <typename _Tp, typename _Up>
1301 constexpr const _Tp&&
1302 get(const pair<_Tp, _Up>&& __p) noexcept
1303 { return std::move(__p.first); }
1304
1305 template <typename _Tp, typename _Up>
1306 constexpr _Tp&
1307 get(pair<_Up, _Tp>& __p) noexcept
1308 { return __p.second; }
1309
1310 template <typename _Tp, typename _Up>
1311 constexpr const _Tp&
1312 get(const pair<_Up, _Tp>& __p) noexcept
1313 { return __p.second; }
1314
1315 template <typename _Tp, typename _Up>
1316 constexpr _Tp&&
1317 get(pair<_Up, _Tp>&& __p) noexcept
1318 { return std::move(__p.second); }
1319
1320 template <typename _Tp, typename _Up>
1321 constexpr const _Tp&&
1322 get(const pair<_Up, _Tp>&& __p) noexcept
1323 { return std::move(__p.second); }
1324#endif // __glibcxx_tuples_by_type
1325
1326
1327#if __glibcxx_ranges_zip // >= C++23
1328 template<typename _T1, typename _T2, typename _U1, typename _U2,
1329 template<typename> class _TQual, template<typename> class _UQual>
1330 requires requires { typename pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
1331 common_reference_t<_TQual<_T2>, _UQual<_U2>>>; }
1332 struct basic_common_reference<pair<_T1, _T2>, pair<_U1, _U2>, _TQual, _UQual>
1333 {
1334 using type = pair<common_reference_t<_TQual<_T1>, _UQual<_U1>>,
1335 common_reference_t<_TQual<_T2>, _UQual<_U2>>>;
1336 };
1337
1338 template<typename _T1, typename _T2, typename _U1, typename _U2>
1339 requires requires { typename pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>; }
1340 struct common_type<pair<_T1, _T2>, pair<_U1, _U2>>
1341 { using type = pair<common_type_t<_T1, _U1>, common_type_t<_T2, _U2>>; };
1342#endif // C++23
1343
1344 /// @}
1345#endif // C++11
1346
1347_GLIBCXX_END_NAMESPACE_VERSION
1348} // namespace std
1349
1350#endif /* _STL_PAIR_H */
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:859
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:873
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:866
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:116
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr bool operator==(const pair< _T1, _T2 > &__x, const pair< _U1, _U2 > &__y)
Two pairs are equal iff their members are equal.
Definition stl_pair.h:1013
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr enable_if< __and_< __is_swappable< _T1 >, __is_swappable< _T2 > >::value >::type swap(pair< _T1, _T2 > &__x, pair< _T1, _T2 > &__y) noexcept(noexcept(__x.swap(__y)))
Definition stl_pair.h:1103
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition stl_pair.h:82
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition stl_pair.h:1146
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
A standard container for storing a fixed size sequence of elements.
Definition array:103
Primary class template, tuple.
Definition tuple:834
integral_constant
Definition type_traits:93
Define a member typedef type only if a boolean constant is true.
Definition type_traits:134
is_constructible
Definition type_traits:1158
is_default_constructible
Definition type_traits:1167
Struct holding two objects of arbitrary type.
Definition stl_pair.h:286
constexpr pair(const _T1 &__a, const _T2 &__b)
Construct from two const lvalues, allowing implicit conversions.
Definition stl_pair.h:754
_T1 first
The first member.
Definition stl_pair.h:290
_T1 first_type
The type of the first member.
Definition stl_pair.h:287
constexpr void swap(pair &__p) noexcept(__and_< __is_nothrow_swappable< _T1 >, __is_nothrow_swappable< _T2 > >::value)
Swap the first members and then the second members.
Definition stl_pair.h:303
constexpr pair(const pair &)=default
Copy constructor.
constexpr pair()
Definition stl_pair.h:727
_T2 second_type
The type of the second member.
Definition stl_pair.h:288
constexpr pair(pair &&)=default
Move constructor.
_T2 second
The second member.
Definition stl_pair.h:291
Tag type for piecewise construction of std::pair objects.
Definition stl_pair.h:79
Finds the size of a given tuple type.
Definition utility.h:51
Gives the type of the ith element of a given tuple type.
Definition utility.h:82