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