1 // Pair implementation -*- C++ -*-
2
3 // Copyright (C) 2001-2021 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 # define __cpp_lib_constexpr_utility 201811L
67 #endif
68
_GLIBCXX_VISIBILITY(default)69 namespace std _GLIBCXX_VISIBILITY(default)
70 {
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72
73 /**
74 * @addtogroup utilities
75 * @{
76 */
77
78 #if __cplusplus >= 201103L
79 /// Tag type for piecewise construction of std::pair objects.
80 struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
81
82 /// Tag for piecewise construction of std::pair objects.
83 _GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct =
84 piecewise_construct_t();
85
86 /// @cond undocumented
87
88 // Forward declarations.
89 template<typename...>
90 class tuple;
91
92 template<size_t...>
93 struct _Index_tuple;
94
95 #if ! __cpp_lib_concepts
96 // Concept utility functions, reused in conditionally-explicit
97 // constructors.
98 // See PR 70437, don't look at is_constructible or
99 // is_convertible if the types are the same to
100 // avoid querying those properties for incomplete types.
101 template <bool, typename _T1, typename _T2>
102 struct _PCC
103 {
104 template <typename _U1, typename _U2>
105 static constexpr bool _ConstructiblePair()
106 {
107 return __and_<is_constructible<_T1, const _U1&>,
108 is_constructible<_T2, const _U2&>>::value;
109 }
110
111 template <typename _U1, typename _U2>
112 static constexpr bool _ImplicitlyConvertiblePair()
113 {
114 return __and_<is_convertible<const _U1&, _T1>,
115 is_convertible<const _U2&, _T2>>::value;
116 }
117
118 template <typename _U1, typename _U2>
119 static constexpr bool _MoveConstructiblePair()
120 {
121 return __and_<is_constructible<_T1, _U1&&>,
122 is_constructible<_T2, _U2&&>>::value;
123 }
124
125 template <typename _U1, typename _U2>
126 static constexpr bool _ImplicitlyMoveConvertiblePair()
127 {
128 return __and_<is_convertible<_U1&&, _T1>,
129 is_convertible<_U2&&, _T2>>::value;
130 }
131
132 template <bool __implicit, typename _U1, typename _U2>
133 static constexpr bool _DeprConsPair()
134 {
135 using __do_converts = __and_<is_convertible<_U1&&, _T1>,
136 is_convertible<_U2&&, _T2>>;
137 using __converts = __conditional_t<__implicit,
138 __do_converts,
139 __not_<__do_converts>>;
140 return __and_<is_constructible<_T1, _U1&&>,
141 is_constructible<_T2, _U2&&>,
142 __converts
143 >::value;
144 }
145 };
146
147 template <typename _T1, typename _T2>
148 struct _PCC<false, _T1, _T2>
149 {
150 template <typename _U1, typename _U2>
151 static constexpr bool _ConstructiblePair()
152 {
153 return false;
154 }
155
156 template <typename _U1, typename _U2>
157 static constexpr bool _ImplicitlyConvertiblePair()
158 {
159 return false;
160 }
161
162 template <typename _U1, typename _U2>
163 static constexpr bool _MoveConstructiblePair()
164 {
165 return false;
166 }
167
168 template <typename _U1, typename _U2>
169 static constexpr bool _ImplicitlyMoveConvertiblePair()
170 {
171 return false;
172 }
173 };
174 #endif // lib concepts
175 #endif // C++11
176
177 template<typename _U1, typename _U2> class __pair_base
178 {
179 #if __cplusplus >= 201103L && ! __cpp_lib_concepts
180 template<typename _T1, typename _T2> friend struct pair;
181 __pair_base() = default;
182 ~__pair_base() = default;
183 __pair_base(const __pair_base&) = default;
184 __pair_base& operator=(const __pair_base&) = delete;
185 #endif // C++11
186 };
187
188 /// @endcond
189
190 /**
191 * @brief Struct holding two objects of arbitrary type.
192 *
193 * @tparam _T1 Type of first object.
194 * @tparam _T2 Type of second object.
195 *
196 * <https://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
197 */
198 template<typename _T1, typename _T2>
199 struct pair
200 : public __pair_base<_T1, _T2>
201 {
202 typedef _T1 first_type; ///< The type of the `first` member
203 typedef _T2 second_type; ///< The type of the `second` member
204
205 _T1 first; ///< The first member
206 _T2 second; ///< The second member
207
208 #if __cplusplus >= 201103L
209 constexpr pair(const pair&) = default; ///< Copy constructor
210 constexpr pair(pair&&) = default; ///< Move constructor
211
212 template<typename... _Args1, typename... _Args2>
213 _GLIBCXX20_CONSTEXPR
214 pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
215
216 /// Swap the first members and then the second members.
217 _GLIBCXX20_CONSTEXPR void
218 swap(pair& __p)
219 noexcept(__and_<__is_nothrow_swappable<_T1>,
220 __is_nothrow_swappable<_T2>>::value)
221 {
222 using std::swap;
223 swap(first, __p.first);
224 swap(second, __p.second);
225 }
226
227 private:
228 template<typename... _Args1, size_t... _Indexes1,
229 typename... _Args2, size_t... _Indexes2>
230 _GLIBCXX20_CONSTEXPR
231 pair(tuple<_Args1...>&, tuple<_Args2...>&,
232 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
233 public:
234
235 #if __cpp_lib_concepts
236 // C++20 implementation using concepts, explicit(bool), fully constexpr.
237
238 /// Default constructor
239 constexpr
240 explicit(__not_<__and_<__is_implicitly_default_constructible<_T1>,
241 __is_implicitly_default_constructible<_T2>>>())
242 pair()
243 requires is_default_constructible_v<_T1>
244 && is_default_constructible_v<_T2>
245 : first(), second()
246 { }
247
248 private:
249
250 /// @cond undocumented
251 template<typename _U1, typename _U2>
252 static constexpr bool
253 _S_constructible()
254 {
255 if constexpr (is_constructible_v<_T1, _U1>)
256 return is_constructible_v<_T2, _U2>;
257 return false;
258 }
259
260 template<typename _U1, typename _U2>
261 static constexpr bool
262 _S_nothrow_constructible()
263 {
264 if constexpr (is_nothrow_constructible_v<_T1, _U1>)
265 return is_nothrow_constructible_v<_T2, _U2>;
266 return false;
267 }
268
269 template<typename _U1, typename _U2>
270 static constexpr bool
271 _S_convertible()
272 {
273 if constexpr (is_convertible_v<_U1, _T1>)
274 return is_convertible_v<_U2, _T2>;
275 return false;
276 }
277 /// @endcond
278
279 public:
280
281 /// Constructor accepting lvalues of `first_type` and `second_type`
282 constexpr explicit(!_S_convertible<const _T1&, const _T2&>())
283 pair(const _T1& __x, const _T2& __y)
284 noexcept(_S_nothrow_constructible<const _T1&, const _T2&>())
285 requires (_S_constructible<const _T1&, const _T2&>())
286 : first(__x), second(__y)
287 { }
288
289 /// Constructor accepting two values of arbitrary types
290 template<typename _U1, typename _U2>
291 requires (_S_constructible<_U1, _U2>())
292 constexpr explicit(!_S_convertible<_U1, _U2>())
293 pair(_U1&& __x, _U2&& __y)
294 noexcept(_S_nothrow_constructible<_U1, _U2>())
295 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y))
296 { }
297
298 /// Converting constructor from a `pair<U1, U2>` lvalue
299 template<typename _U1, typename _U2>
300 requires (_S_constructible<const _U1&, const _U2&>())
301 constexpr explicit(!_S_convertible<const _U1&, const _U2&>())
302 pair(const pair<_U1, _U2>& __p)
303 noexcept(_S_nothrow_constructible<const _U1&, const _U2&>())
304 : first(__p.first), second(__p.second)
305 { }
306
307 /// Converting constructor from a `pair<U1, U2>` rvalue
308 template<typename _U1, typename _U2>
309 requires (_S_constructible<_U1, _U2>())
310 constexpr explicit(!_S_convertible<_U1, _U2>())
311 pair(pair<_U1, _U2>&& __p)
312 noexcept(_S_nothrow_constructible<_U1, _U2>())
313 : first(std::forward<_U1>(__p.first)),
314 second(std::forward<_U2>(__p.second))
315 { }
316
317 private:
318 /// @cond undocumented
319 template<typename _U1, typename _U2>
320 static constexpr bool
321 _S_assignable()
322 {
323 if constexpr (is_assignable_v<_T1&, _U1>)
324 return is_assignable_v<_T2&, _U2>;
325 return false;
326 }
327
328 template<typename _U1, typename _U2>
329 static constexpr bool
330 _S_nothrow_assignable()
331 {
332 if constexpr (is_nothrow_assignable_v<_T1&, _U1>)
333 return is_nothrow_assignable_v<_T2&, _U2>;
334 return false;
335 }
336 /// @endcond
337
338 public:
339
340 pair& operator=(const pair&) = delete;
341
342 /// Copy assignment operator
343 constexpr pair&
344 operator=(const pair& __p)
345 noexcept(_S_nothrow_assignable<const _T1&, const _T2&>())
346 requires (_S_assignable<const _T1&, const _T2&>())
347 {
348 first = __p.first;
349 second = __p.second;
350 return *this;
351 }
352
353 /// Move assignment operator
354 constexpr pair&
355 operator=(pair&& __p)
356 noexcept(_S_nothrow_assignable<_T1, _T2>())
357 requires (_S_assignable<_T1, _T2>())
358 {
359 first = std::forward<first_type>(__p.first);
360 second = std::forward<second_type>(__p.second);
361 return *this;
362 }
363
364 /// Converting assignment from a `pair<U1, U2>` lvalue
365 template<typename _U1, typename _U2>
366 constexpr pair&
367 operator=(const pair<_U1, _U2>& __p)
368 noexcept(_S_nothrow_assignable<const _U1&, const _U2&>())
369 requires (_S_assignable<const _U1&, const _U2&>())
370 {
371 first = __p.first;
372 second = __p.second;
373 return *this;
374 }
375
376 /// Converting assignment from a `pair<U1, U2>` rvalue
377 template<typename _U1, typename _U2>
378 constexpr pair&
379 operator=(pair<_U1, _U2>&& __p)
380 noexcept(_S_nothrow_assignable<_U1, _U2>())
381 requires (_S_assignable<_U1, _U2>())
382 {
383 first = std::forward<_U1>(__p.first);
384 second = std::forward<_U2>(__p.second);
385 return *this;
386 }
387 #else
388 // C++11/14/17 implementation using enable_if, partially constexpr.
389
390 /** The default constructor creates @c first and @c second using their
391 * respective default constructors. */
392 template <typename _U1 = _T1,
393 typename _U2 = _T2,
394 typename enable_if<__and_<
395 __is_implicitly_default_constructible<_U1>,
396 __is_implicitly_default_constructible<_U2>>
397 ::value, bool>::type = true>
398 constexpr pair()
399 : first(), second() { }
400
401 template <typename _U1 = _T1,
402 typename _U2 = _T2,
403 typename enable_if<__and_<
404 is_default_constructible<_U1>,
405 is_default_constructible<_U2>,
406 __not_<
407 __and_<__is_implicitly_default_constructible<_U1>,
408 __is_implicitly_default_constructible<_U2>>>>
409 ::value, bool>::type = false>
410 explicit constexpr pair()
411 : first(), second() { }
412
413 // Shortcut for constraining the templates that don't take pairs.
414 /// @cond undocumented
415 using _PCCP = _PCC<true, _T1, _T2>;
416 /// @endcond
417
418 /// Construct from two const lvalues, allowing implicit conversions.
419 template<typename _U1 = _T1, typename _U2=_T2, typename
420 enable_if<_PCCP::template
421 _ConstructiblePair<_U1, _U2>()
422 && _PCCP::template
423 _ImplicitlyConvertiblePair<_U1, _U2>(),
424 bool>::type=true>
425 constexpr pair(const _T1& __a, const _T2& __b)
426 : first(__a), second(__b) { }
427
428 /// Construct from two const lvalues, disallowing implicit conversions.
429 template<typename _U1 = _T1, typename _U2=_T2, typename
430 enable_if<_PCCP::template
431 _ConstructiblePair<_U1, _U2>()
432 && !_PCCP::template
433 _ImplicitlyConvertiblePair<_U1, _U2>(),
434 bool>::type=false>
435 explicit constexpr pair(const _T1& __a, const _T2& __b)
436 : first(__a), second(__b) { }
437
438 // Shortcut for constraining the templates that take pairs.
439 /// @cond undocumented
440 template <typename _U1, typename _U2>
441 using _PCCFP = _PCC<!is_same<_T1, _U1>::value
442 || !is_same<_T2, _U2>::value,
443 _T1, _T2>;
444 /// @endcond
445
446 template<typename _U1, typename _U2, typename
447 enable_if<_PCCFP<_U1, _U2>::template
448 _ConstructiblePair<_U1, _U2>()
449 && _PCCFP<_U1, _U2>::template
450 _ImplicitlyConvertiblePair<_U1, _U2>(),
451 bool>::type=true>
452 constexpr pair(const pair<_U1, _U2>& __p)
453 : first(__p.first), second(__p.second) { }
454
455 template<typename _U1, typename _U2, typename
456 enable_if<_PCCFP<_U1, _U2>::template
457 _ConstructiblePair<_U1, _U2>()
458 && !_PCCFP<_U1, _U2>::template
459 _ImplicitlyConvertiblePair<_U1, _U2>(),
460 bool>::type=false>
461 explicit constexpr pair(const pair<_U1, _U2>& __p)
462 : first(__p.first), second(__p.second) { }
463
464 #if _GLIBCXX_USE_DEPRECATED
465 private:
466 /// @cond undocumented
467
468 // A type which can be constructed from literal zero, but not nullptr
469 struct __null_ptr_constant
470 {
471 __null_ptr_constant(int __null_ptr_constant::*) { }
472 template<typename _Tp,
473 typename = __enable_if_t<is_null_pointer<_Tp>::value>>
474 __null_ptr_constant(_Tp) = delete;
475 };
476
477 // True if type _Up is one of _Tp& or const _Tp&
478 template<typename _Up, typename _Tp>
479 using __is_lvalue_of
480 = __or_<is_same<_Up, const _Tp&>, is_same<_Up, _Tp&>>;
481
482 /// @endcond
483 public:
484
485 // Deprecated extensions to DR 811.
486 template<typename _U1,
487 __enable_if_t<!__is_lvalue_of<_U1, _T1>::value
488 && _PCCP::template
489 _DeprConsPair<true, _U1, nullptr_t>(),
490 bool> = true>
491 _GLIBCXX_DEPRECATED_SUGGEST("nullptr")
492 constexpr pair(_U1&& __x, __null_ptr_constant)
493 : first(std::forward<_U1>(__x)), second(nullptr) { }
494
495 template<typename _U1,
496 __enable_if_t<!__is_lvalue_of<_U1, _T1>::value
497 && _PCCP::template
498 _DeprConsPair<false, _U1, nullptr_t>(),
499 bool> = false>
500 _GLIBCXX_DEPRECATED_SUGGEST("nullptr")
501 explicit constexpr pair(_U1&& __x, __null_ptr_constant)
502 : first(std::forward<_U1>(__x)), second(nullptr) { }
503
504 template<typename _U2,
505 __enable_if_t<!__is_lvalue_of<_U2, _T2>::value
506 && _PCCP::template
507 _DeprConsPair<true, nullptr_t, _U2>(),
508 bool> = true>
509 _GLIBCXX_DEPRECATED_SUGGEST("nullptr")
510 constexpr pair(__null_ptr_constant, _U2&& __y)
511 : first(nullptr), second(std::forward<_U2>(__y)) { }
512
513 template<typename _U2,
514 __enable_if_t<!__is_lvalue_of<_U2, _T2>::value
515 && _PCCP::template
516 _DeprConsPair<false, nullptr_t, _U2>(),
517 bool> = false>
518 _GLIBCXX_DEPRECATED_SUGGEST("nullptr")
519 explicit pair(__null_ptr_constant, _U2&& __y)
520 : first(nullptr), second(std::forward<_U2>(__y)) { }
521 #endif
522
523 template<typename _U1, typename _U2, typename
524 enable_if<_PCCP::template
525 _MoveConstructiblePair<_U1, _U2>()
526 && _PCCP::template
527 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
528 bool>::type=true>
529 constexpr pair(_U1&& __x, _U2&& __y)
530 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
531
532 template<typename _U1, typename _U2, typename
533 enable_if<_PCCP::template
534 _MoveConstructiblePair<_U1, _U2>()
535 && !_PCCP::template
536 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
537 bool>::type=false>
538 explicit constexpr pair(_U1&& __x, _U2&& __y)
539 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
540
541
542 template<typename _U1, typename _U2, typename
543 enable_if<_PCCFP<_U1, _U2>::template
544 _MoveConstructiblePair<_U1, _U2>()
545 && _PCCFP<_U1, _U2>::template
546 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
547 bool>::type=true>
548 constexpr pair(pair<_U1, _U2>&& __p)
549 : first(std::forward<_U1>(__p.first)),
550 second(std::forward<_U2>(__p.second)) { }
551
552 template<typename _U1, typename _U2, typename
553 enable_if<_PCCFP<_U1, _U2>::template
554 _MoveConstructiblePair<_U1, _U2>()
555 && !_PCCFP<_U1, _U2>::template
556 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
557 bool>::type=false>
558 explicit constexpr pair(pair<_U1, _U2>&& __p)
559 : first(std::forward<_U1>(__p.first)),
560 second(std::forward<_U2>(__p.second)) { }
561
562 pair&
563 operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
564 is_copy_assignable<_T2>>::value,
565 const pair&, const __nonesuch&> __p)
566 {
567 first = __p.first;
568 second = __p.second;
569 return *this;
570 }
571
572 pair&
573 operator=(__conditional_t<__and_<is_move_assignable<_T1>,
574 is_move_assignable<_T2>>::value,
575 pair&&, __nonesuch&&> __p)
576 noexcept(__and_<is_nothrow_move_assignable<_T1>,
577 is_nothrow_move_assignable<_T2>>::value)
578 {
579 first = std::forward<first_type>(__p.first);
580 second = std::forward<second_type>(__p.second);
581 return *this;
582 }
583
584 template<typename _U1, typename _U2>
585 typename enable_if<__and_<is_assignable<_T1&, const _U1&>,
586 is_assignable<_T2&, const _U2&>>::value,
587 pair&>::type
588 operator=(const pair<_U1, _U2>& __p)
589 {
590 first = __p.first;
591 second = __p.second;
592 return *this;
593 }
594
595 template<typename _U1, typename _U2>
596 typename enable_if<__and_<is_assignable<_T1&, _U1&&>,
597 is_assignable<_T2&, _U2&&>>::value,
598 pair&>::type
599 operator=(pair<_U1, _U2>&& __p)
600 {
601 first = std::forward<_U1>(__p.first);
602 second = std::forward<_U2>(__p.second);
603 return *this;
604 }
605 #endif // lib concepts
606 #else
607 // C++03 implementation
608
609 // _GLIBCXX_RESOLVE_LIB_DEFECTS
610 // 265. std::pair::pair() effects overly restrictive
611 /** The default constructor creates @c first and @c second using their
612 * respective default constructors. */
613 pair() : first(), second() { }
614
615 /// Two objects may be passed to a `pair` constructor to be copied.
616 pair(const _T1& __a, const _T2& __b)
617 : first(__a), second(__b) { }
618
619 /// Templated constructor to convert from other pairs.
620 template<typename _U1, typename _U2>
621 pair(const pair<_U1, _U2>& __p)
622 : first(__p.first), second(__p.second) { }
623 #endif // C++11
624 };
625
626 /// @relates pair @{
627
628 #if __cpp_deduction_guides >= 201606
629 template<typename _T1, typename _T2> pair(_T1, _T2) -> pair<_T1, _T2>;
630 #endif
631
632 /// Two pairs of the same type are equal iff their members are equal.
633 template<typename _T1, typename _T2>
634 inline _GLIBCXX_CONSTEXPR bool
635 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
636 { return __x.first == __y.first && __x.second == __y.second; }
637
638 #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
639 template<typename _T1, typename _T2>
640 constexpr common_comparison_category_t<__detail::__synth3way_t<_T1>,
641 __detail::__synth3way_t<_T2>>
642 operator<=>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
643 {
644 if (auto __c = __detail::__synth3way(__x.first, __y.first); __c != 0)
645 return __c;
646 return __detail::__synth3way(__x.second, __y.second);
647 }
648 #else
649 /** Defines a lexicographical order for pairs.
650 *
651 * For two pairs of the same type, `P` is ordered before `Q` if
652 * `P.first` is less than `Q.first`, or if `P.first` and `Q.first`
653 * are equivalent (neither is less than the other) and `P.second` is less
654 * than `Q.second`.
655 */
656 template<typename _T1, typename _T2>
657 inline _GLIBCXX_CONSTEXPR bool
658 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
659 { return __x.first < __y.first
660 || (!(__y.first < __x.first) && __x.second < __y.second); }
661
662 /// Uses @c operator== to find the result.
663 template<typename _T1, typename _T2>
664 inline _GLIBCXX_CONSTEXPR bool
665 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
666 { return !(__x == __y); }
667
668 /// Uses @c operator< to find the result.
669 template<typename _T1, typename _T2>
670 inline _GLIBCXX_CONSTEXPR bool
671 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
672 { return __y < __x; }
673
674 /// Uses @c operator< to find the result.
675 template<typename _T1, typename _T2>
676 inline _GLIBCXX_CONSTEXPR bool
677 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
678 { return !(__y < __x); }
679
680 /// Uses @c operator< to find the result.
681 template<typename _T1, typename _T2>
682 inline _GLIBCXX_CONSTEXPR bool
683 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
684 { return !(__x < __y); }
685 #endif // !(three_way_comparison && concepts)
686
687 #if __cplusplus >= 201103L
688 /** Swap overload for pairs. Calls std::pair::swap().
689 *
690 * @note This std::swap overload is not declared in C++03 mode,
691 * which has performance implications, e.g. see https://gcc.gnu.org/PR38466
692 */
693 template<typename _T1, typename _T2>
694 _GLIBCXX20_CONSTEXPR inline
695 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
696 // Constrained free swap overload, see p0185r1
697 typename enable_if<__and_<__is_swappable<_T1>,
698 __is_swappable<_T2>>::value>::type
699 #else
700 void
701 #endif
702 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
703 noexcept(noexcept(__x.swap(__y)))
704 { __x.swap(__y); }
705
706 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
707 template<typename _T1, typename _T2>
708 typename enable_if<!__and_<__is_swappable<_T1>,
709 __is_swappable<_T2>>::value>::type
710 swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
711 #endif
712 #endif // __cplusplus >= 201103L
713
714 /// @} relates pair
715
716 /**
717 * @brief A convenience wrapper for creating a pair from two objects.
718 * @param __x The first object.
719 * @param __y The second object.
720 * @return A newly-constructed pair<> object of the appropriate type.
721 *
722 * The C++98 standard says the objects are passed by reference-to-const,
723 * but C++03 says they are passed by value (this was LWG issue #181).
724 *
725 * Since C++11 they have been passed by forwarding reference and then
726 * forwarded to the new members of the pair. To create a pair with a
727 * member of reference type, pass a `reference_wrapper` to this function.
728 */
729 // _GLIBCXX_RESOLVE_LIB_DEFECTS
730 // 181. make_pair() unintended behavior
731 #if __cplusplus >= 201103L
732 // NB: DR 706.
733 template<typename _T1, typename _T2>
734 constexpr pair<typename __decay_and_strip<_T1>::__type,
735 typename __decay_and_strip<_T2>::__type>
736 make_pair(_T1&& __x, _T2&& __y)
737 {
738 typedef typename __decay_and_strip<_T1>::__type __ds_type1;
739 typedef typename __decay_and_strip<_T2>::__type __ds_type2;
740 typedef pair<__ds_type1, __ds_type2> __pair_type;
741 return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
742 }
743 #else
744 template<typename _T1, typename _T2>
745 inline pair<_T1, _T2>
746 make_pair(_T1 __x, _T2 __y)
747 { return pair<_T1, _T2>(__x, __y); }
748 #endif
749
750 /// @}
751
752 #if __cplusplus >= 201103L
753 // Various functions which give std::pair a tuple-like interface.
754
755 template<typename _T1, typename _T2>
756 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
757 { };
758
759 /// Partial specialization for std::pair
760 template<class _Tp1, class _Tp2>
761 struct tuple_size<pair<_Tp1, _Tp2>>
762 : public integral_constant<size_t, 2> { };
763
764 /// Partial specialization for std::pair
765 template<class _Tp1, class _Tp2>
766 struct tuple_element<0, pair<_Tp1, _Tp2>>
767 { typedef _Tp1 type; };
768
769 /// Partial specialization for std::pair
770 template<class _Tp1, class _Tp2>
771 struct tuple_element<1, pair<_Tp1, _Tp2>>
772 { typedef _Tp2 type; };
773
774 #if __cplusplus >= 201703L
775 template<typename _Tp1, typename _Tp2>
776 inline constexpr size_t tuple_size_v<pair<_Tp1, _Tp2>> = 2;
777
778 template<typename _Tp1, typename _Tp2>
779 inline constexpr size_t tuple_size_v<const pair<_Tp1, _Tp2>> = 2;
780 #endif
781
782 /// @cond undocumented
783 template<size_t _Int>
784 struct __pair_get;
785
786 template<>
787 struct __pair_get<0>
788 {
789 template<typename _Tp1, typename _Tp2>
790 static constexpr _Tp1&
791 __get(pair<_Tp1, _Tp2>& __pair) noexcept
792 { return __pair.first; }
793
794 template<typename _Tp1, typename _Tp2>
795 static constexpr _Tp1&&
796 __move_get(pair<_Tp1, _Tp2>&& __pair) noexcept
797 { return std::forward<_Tp1>(__pair.first); }
798
799 template<typename _Tp1, typename _Tp2>
800 static constexpr const _Tp1&
801 __const_get(const pair<_Tp1, _Tp2>& __pair) noexcept
802 { return __pair.first; }
803
804 template<typename _Tp1, typename _Tp2>
805 static constexpr const _Tp1&&
806 __const_move_get(const pair<_Tp1, _Tp2>&& __pair) noexcept
807 { return std::forward<const _Tp1>(__pair.first); }
808 };
809
810 template<>
811 struct __pair_get<1>
812 {
813 template<typename _Tp1, typename _Tp2>
814 static constexpr _Tp2&
815 __get(pair<_Tp1, _Tp2>& __pair) noexcept
816 { return __pair.second; }
817
818 template<typename _Tp1, typename _Tp2>
819 static constexpr _Tp2&&
820 __move_get(pair<_Tp1, _Tp2>&& __pair) noexcept
821 { return std::forward<_Tp2>(__pair.second); }
822
823 template<typename _Tp1, typename _Tp2>
824 static constexpr const _Tp2&
825 __const_get(const pair<_Tp1, _Tp2>& __pair) noexcept
826 { return __pair.second; }
827
828 template<typename _Tp1, typename _Tp2>
829 static constexpr const _Tp2&&
830 __const_move_get(const pair<_Tp1, _Tp2>&& __pair) noexcept
831 { return std::forward<const _Tp2>(__pair.second); }
832 };
833 /// @endcond
834
835 /** @{
836 * std::get overloads for accessing members of std::pair
837 */
838
839 template<size_t _Int, class _Tp1, class _Tp2>
840 constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
841 get(pair<_Tp1, _Tp2>& __in) noexcept
842 { return __pair_get<_Int>::__get(__in); }
843
844 template<size_t _Int, class _Tp1, class _Tp2>
845 constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
846 get(pair<_Tp1, _Tp2>&& __in) noexcept
847 { return __pair_get<_Int>::__move_get(std::move(__in)); }
848
849 template<size_t _Int, class _Tp1, class _Tp2>
850 constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&
851 get(const pair<_Tp1, _Tp2>& __in) noexcept
852 { return __pair_get<_Int>::__const_get(__in); }
853
854 template<size_t _Int, class _Tp1, class _Tp2>
855 constexpr const typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type&&
856 get(const pair<_Tp1, _Tp2>&& __in) noexcept
857 { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
858
859 #if __cplusplus >= 201402L
860
861 #define __cpp_lib_tuples_by_type 201304L
862
863 template <typename _Tp, typename _Up>
864 constexpr _Tp&
865 get(pair<_Tp, _Up>& __p) noexcept
866 { return __p.first; }
867
868 template <typename _Tp, typename _Up>
869 constexpr const _Tp&
870 get(const pair<_Tp, _Up>& __p) noexcept
871 { return __p.first; }
872
873 template <typename _Tp, typename _Up>
874 constexpr _Tp&&
875 get(pair<_Tp, _Up>&& __p) noexcept
876 { return std::move(__p.first); }
877
878 template <typename _Tp, typename _Up>
879 constexpr const _Tp&&
880 get(const pair<_Tp, _Up>&& __p) noexcept
881 { return std::move(__p.first); }
882
883 template <typename _Tp, typename _Up>
884 constexpr _Tp&
885 get(pair<_Up, _Tp>& __p) noexcept
886 { return __p.second; }
887
888 template <typename _Tp, typename _Up>
889 constexpr const _Tp&
890 get(const pair<_Up, _Tp>& __p) noexcept
891 { return __p.second; }
892
893 template <typename _Tp, typename _Up>
894 constexpr _Tp&&
895 get(pair<_Up, _Tp>&& __p) noexcept
896 { return std::move(__p.second); }
897
898 template <typename _Tp, typename _Up>
899 constexpr const _Tp&&
900 get(const pair<_Up, _Tp>&& __p) noexcept
901 { return std::move(__p.second); }
902
903 #endif // C++14
904 /// @}
905 #endif // C++11
906
907 _GLIBCXX_END_NAMESPACE_VERSION
908 } // namespace std
909
910 #endif /* _STL_PAIR_H */
911