1 // Pair implementation -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
59 #include <bits/move.h> // for std::move / std::forward, and std::swap
61 #if __cplusplus >= 201103L
62 #include <type_traits> // for std::__decay_and_strip too
65 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 * @addtogroup utilities
74 #if __cplusplus >= 201103L
75 /// piecewise_construct_t
76 struct piecewise_construct_t
{ explicit piecewise_construct_t() = default; };
78 /// piecewise_construct
79 _GLIBCXX17_INLINE
constexpr piecewise_construct_t piecewise_construct
=
80 piecewise_construct_t();
82 // Forward declarations.
86 template<std::size_t...>
89 // Concept utility functions, reused in conditionally-explicit
91 // See PR 70437, don't look at is_constructible or
92 // is_convertible if the types are the same to
93 // avoid querying those properties for incomplete types.
94 template <bool, typename _T1
, typename _T2
>
97 template <typename _U1
, typename _U2
>
98 static constexpr bool _ConstructiblePair()
100 return __and_
<is_constructible
<_T1
, const _U1
&>,
101 is_constructible
<_T2
, const _U2
&>>::value
;
104 template <typename _U1
, typename _U2
>
105 static constexpr bool _ImplicitlyConvertiblePair()
107 return __and_
<is_convertible
<const _U1
&, _T1
>,
108 is_convertible
<const _U2
&, _T2
>>::value
;
111 template <typename _U1
, typename _U2
>
112 static constexpr bool _MoveConstructiblePair()
114 return __and_
<is_constructible
<_T1
, _U1
&&>,
115 is_constructible
<_T2
, _U2
&&>>::value
;
118 template <typename _U1
, typename _U2
>
119 static constexpr bool _ImplicitlyMoveConvertiblePair()
121 return __and_
<is_convertible
<_U1
&&, _T1
>,
122 is_convertible
<_U2
&&, _T2
>>::value
;
125 template <bool __implicit
, typename _U1
, typename _U2
>
126 static constexpr bool _CopyMovePair()
128 using __do_converts
= __and_
<is_convertible
<const _U1
&, _T1
>,
129 is_convertible
<_U2
&&, _T2
>>;
130 using __converts
= typename conditional
<__implicit
,
132 __not_
<__do_converts
>>::type
;
133 return __and_
<is_constructible
<_T1
, const _U1
&>,
134 is_constructible
<_T2
, _U2
&&>,
139 template <bool __implicit
, typename _U1
, typename _U2
>
140 static constexpr bool _MoveCopyPair()
142 using __do_converts
= __and_
<is_convertible
<_U1
&&, _T1
>,
143 is_convertible
<const _U2
&, _T2
>>;
144 using __converts
= typename conditional
<__implicit
,
146 __not_
<__do_converts
>>::type
;
147 return __and_
<is_constructible
<_T1
, _U1
&&>,
148 is_constructible
<_T2
, const _U2
&&>,
154 template <typename _T1
, typename _T2
>
155 struct _PCC
<false, _T1
, _T2
>
157 template <typename _U1
, typename _U2
>
158 static constexpr bool _ConstructiblePair()
163 template <typename _U1
, typename _U2
>
164 static constexpr bool _ImplicitlyConvertiblePair()
169 template <typename _U1
, typename _U2
>
170 static constexpr bool _MoveConstructiblePair()
175 template <typename _U1
, typename _U2
>
176 static constexpr bool _ImplicitlyMoveConvertiblePair()
182 // PR libstdc++/79141, a utility type for preventing
183 // initialization of an argument of a disabled assignment
184 // operator from a pair of empty braces.
185 struct __nonesuch_no_braces
: std::__nonesuch
{
186 explicit __nonesuch_no_braces(const __nonesuch
&) = delete;
192 * @brief Struct holding two objects of arbitrary type.
194 * @tparam _T1 Type of first object.
195 * @tparam _T2 Type of second object.
197 template<typename _T1
, typename _T2
>
200 typedef _T1 first_type
; /// @c first_type is the first bound type
201 typedef _T2 second_type
; /// @c second_type is the second bound type
203 _T1 first
; /// @c first is a copy of the first object
204 _T2 second
; /// @c second is a copy of the second object
206 // _GLIBCXX_RESOLVE_LIB_DEFECTS
207 // 265. std::pair::pair() effects overly restrictive
208 /** The default constructor creates @c first and @c second using their
209 * respective default constructors. */
210 #if __cplusplus >= 201103L
211 template <typename _U1
= _T1
,
213 typename enable_if
<__and_
<
214 __is_implicitly_default_constructible
<_U1
>,
215 __is_implicitly_default_constructible
<_U2
>>
216 ::value
, bool>::type
= true>
218 _GLIBCXX_CONSTEXPR
pair()
219 : first(), second() { }
221 #if __cplusplus >= 201103L
222 template <typename _U1
= _T1
,
224 typename enable_if
<__and_
<
225 is_default_constructible
<_U1
>,
226 is_default_constructible
<_U2
>,
228 __and_
<__is_implicitly_default_constructible
<_U1
>,
229 __is_implicitly_default_constructible
<_U2
>>>>
230 ::value
, bool>::type
= false>
231 explicit constexpr pair()
232 : first(), second() { }
235 /** Two objects may be passed to a @c pair constructor to be copied. */
236 #if __cplusplus < 201103L
237 pair(const _T1
& __a
, const _T2
& __b
)
238 : first(__a
), second(__b
) { }
240 // Shortcut for constraining the templates that don't take pairs.
241 using _PCCP
= _PCC
<true, _T1
, _T2
>;
243 template<typename _U1
= _T1
, typename _U2
=_T2
, typename
244 enable_if
<_PCCP::template
245 _ConstructiblePair
<_U1
, _U2
>()
247 _ImplicitlyConvertiblePair
<_U1
, _U2
>(),
249 constexpr pair(const _T1
& __a
, const _T2
& __b
)
250 : first(__a
), second(__b
) { }
252 template<typename _U1
= _T1
, typename _U2
=_T2
, typename
253 enable_if
<_PCCP::template
254 _ConstructiblePair
<_U1
, _U2
>()
256 _ImplicitlyConvertiblePair
<_U1
, _U2
>(),
258 explicit constexpr pair(const _T1
& __a
, const _T2
& __b
)
259 : first(__a
), second(__b
) { }
262 /** There is also a templated copy ctor for the @c pair class itself. */
263 #if __cplusplus < 201103L
264 template<typename _U1
, typename _U2
>
265 pair(const pair
<_U1
, _U2
>& __p
)
266 : first(__p
.first
), second(__p
.second
) { }
268 // Shortcut for constraining the templates that take pairs.
269 template <typename _U1
, typename _U2
>
270 using _PCCFP
= _PCC
<!is_same
<_T1
, _U1
>::value
271 || !is_same
<_T2
, _U2
>::value
,
274 template<typename _U1
, typename _U2
, typename
275 enable_if
<_PCCFP
<_U1
, _U2
>::template
276 _ConstructiblePair
<_U1
, _U2
>()
277 && _PCCFP
<_U1
, _U2
>::template
278 _ImplicitlyConvertiblePair
<_U1
, _U2
>(),
280 constexpr pair(const pair
<_U1
, _U2
>& __p
)
281 : first(__p
.first
), second(__p
.second
) { }
283 template<typename _U1
, typename _U2
, typename
284 enable_if
<_PCCFP
<_U1
, _U2
>::template
285 _ConstructiblePair
<_U1
, _U2
>()
286 && !_PCCFP
<_U1
, _U2
>::template
287 _ImplicitlyConvertiblePair
<_U1
, _U2
>(),
289 explicit constexpr pair(const pair
<_U1
, _U2
>& __p
)
290 : first(__p
.first
), second(__p
.second
) { }
292 constexpr pair(const pair
&) = default;
293 constexpr pair(pair
&&) = default;
296 template<typename _U1
, typename
297 enable_if
<_PCCP::template
298 _MoveCopyPair
<true, _U1
, _T2
>(),
300 constexpr pair(_U1
&& __x
, const _T2
& __y
)
301 : first(std::forward
<_U1
>(__x
)), second(__y
) { }
303 template<typename _U1
, typename
304 enable_if
<_PCCP::template
305 _MoveCopyPair
<false, _U1
, _T2
>(),
307 explicit constexpr pair(_U1
&& __x
, const _T2
& __y
)
308 : first(std::forward
<_U1
>(__x
)), second(__y
) { }
310 template<typename _U2
, typename
311 enable_if
<_PCCP::template
312 _CopyMovePair
<true, _T1
, _U2
>(),
314 constexpr pair(const _T1
& __x
, _U2
&& __y
)
315 : first(__x
), second(std::forward
<_U2
>(__y
)) { }
317 template<typename _U2
, typename
318 enable_if
<_PCCP::template
319 _CopyMovePair
<false, _T1
, _U2
>(),
321 explicit pair(const _T1
& __x
, _U2
&& __y
)
322 : first(__x
), second(std::forward
<_U2
>(__y
)) { }
324 template<typename _U1
, typename _U2
, typename
325 enable_if
<_PCCP::template
326 _MoveConstructiblePair
<_U1
, _U2
>()
328 _ImplicitlyMoveConvertiblePair
<_U1
, _U2
>(),
330 constexpr pair(_U1
&& __x
, _U2
&& __y
)
331 : first(std::forward
<_U1
>(__x
)), second(std::forward
<_U2
>(__y
)) { }
333 template<typename _U1
, typename _U2
, typename
334 enable_if
<_PCCP::template
335 _MoveConstructiblePair
<_U1
, _U2
>()
337 _ImplicitlyMoveConvertiblePair
<_U1
, _U2
>(),
339 explicit constexpr pair(_U1
&& __x
, _U2
&& __y
)
340 : first(std::forward
<_U1
>(__x
)), second(std::forward
<_U2
>(__y
)) { }
343 template<typename _U1
, typename _U2
, typename
344 enable_if
<_PCCFP
<_U1
, _U2
>::template
345 _MoveConstructiblePair
<_U1
, _U2
>()
346 && _PCCFP
<_U1
, _U2
>::template
347 _ImplicitlyMoveConvertiblePair
<_U1
, _U2
>(),
349 constexpr pair(pair
<_U1
, _U2
>&& __p
)
350 : first(std::forward
<_U1
>(__p
.first
)),
351 second(std::forward
<_U2
>(__p
.second
)) { }
353 template<typename _U1
, typename _U2
, typename
354 enable_if
<_PCCFP
<_U1
, _U2
>::template
355 _MoveConstructiblePair
<_U1
, _U2
>()
356 && !_PCCFP
<_U1
, _U2
>::template
357 _ImplicitlyMoveConvertiblePair
<_U1
, _U2
>(),
359 explicit constexpr pair(pair
<_U1
, _U2
>&& __p
)
360 : first(std::forward
<_U1
>(__p
.first
)),
361 second(std::forward
<_U2
>(__p
.second
)) { }
363 template<typename
... _Args1
, typename
... _Args2
>
364 pair(piecewise_construct_t
, tuple
<_Args1
...>, tuple
<_Args2
...>);
367 operator=(typename conditional
<
368 __and_
<is_copy_assignable
<_T1
>,
369 is_copy_assignable
<_T2
>>::value
,
370 const pair
&, const __nonesuch_no_braces
&>::type __p
)
378 operator=(typename conditional
<
379 __not_
<__and_
<is_copy_assignable
<_T1
>,
380 is_copy_assignable
<_T2
>>>::value
,
381 const pair
&, const __nonesuch_no_braces
&>::type __p
) = delete;
384 operator=(typename conditional
<
385 __and_
<is_move_assignable
<_T1
>,
386 is_move_assignable
<_T2
>>::value
,
387 pair
&&, __nonesuch_no_braces
&&>::type __p
)
388 noexcept(__and_
<is_nothrow_move_assignable
<_T1
>,
389 is_nothrow_move_assignable
<_T2
>>::value
)
391 first
= std::forward
<first_type
>(__p
.first
);
392 second
= std::forward
<second_type
>(__p
.second
);
396 template<typename _U1
, typename _U2
>
397 typename enable_if
<__and_
<is_assignable
<_T1
&, const _U1
&>,
398 is_assignable
<_T2
&, const _U2
&>>::value
,
400 operator=(const pair
<_U1
, _U2
>& __p
)
407 template<typename _U1
, typename _U2
>
408 typename enable_if
<__and_
<is_assignable
<_T1
&, _U1
&&>,
409 is_assignable
<_T2
&, _U2
&&>>::value
,
411 operator=(pair
<_U1
, _U2
>&& __p
)
413 first
= std::forward
<_U1
>(__p
.first
);
414 second
= std::forward
<_U2
>(__p
.second
);
420 noexcept(__and_
<__is_nothrow_swappable
<_T1
>,
421 __is_nothrow_swappable
<_T2
>>::value
)
424 swap(first
, __p
.first
);
425 swap(second
, __p
.second
);
429 template<typename
... _Args1
, std::size_t... _Indexes1
,
430 typename
... _Args2
, std::size_t... _Indexes2
>
431 pair(tuple
<_Args1
...>&, tuple
<_Args2
...>&,
432 _Index_tuple
<_Indexes1
...>, _Index_tuple
<_Indexes2
...>);
436 #if __cpp_deduction_guides >= 201606
437 template<typename _T1
, typename _T2
> pair(_T1
, _T2
) -> pair
<_T1
, _T2
>;
440 /// Two pairs of the same type are equal iff their members are equal.
441 template<typename _T1
, typename _T2
>
442 inline _GLIBCXX_CONSTEXPR
bool
443 operator==(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
444 { return __x
.first
== __y
.first
&& __x
.second
== __y
.second
; }
446 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
447 template<typename _T1
, typename _T2
>
448 inline _GLIBCXX_CONSTEXPR
bool
449 operator<(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
450 { return __x
.first
< __y
.first
451 || (!(__y
.first
< __x
.first
) && __x
.second
< __y
.second
); }
453 /// Uses @c operator== to find the result.
454 template<typename _T1
, typename _T2
>
455 inline _GLIBCXX_CONSTEXPR
bool
456 operator!=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
457 { return !(__x
== __y
); }
459 /// Uses @c operator< to find the result.
460 template<typename _T1
, typename _T2
>
461 inline _GLIBCXX_CONSTEXPR
bool
462 operator>(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
463 { return __y
< __x
; }
465 /// Uses @c operator< to find the result.
466 template<typename _T1
, typename _T2
>
467 inline _GLIBCXX_CONSTEXPR
bool
468 operator<=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
469 { return !(__y
< __x
); }
471 /// Uses @c operator< to find the result.
472 template<typename _T1
, typename _T2
>
473 inline _GLIBCXX_CONSTEXPR
bool
474 operator>=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
475 { return !(__x
< __y
); }
477 #if __cplusplus >= 201103L
478 /// See std::pair::swap().
479 // Note: no std::swap overloads in C++03 mode, this has performance
480 // implications, see, eg, libstdc++/38466.
481 template<typename _T1
, typename _T2
>
483 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
484 // Constrained free swap overload, see p0185r1
485 typename enable_if
<__and_
<__is_swappable
<_T1
>,
486 __is_swappable
<_T2
>>::value
>::type
490 swap(pair
<_T1
, _T2
>& __x
, pair
<_T1
, _T2
>& __y
)
491 noexcept(noexcept(__x
.swap(__y
)))
494 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
495 template<typename _T1
, typename _T2
>
496 typename enable_if
<!__and_
<__is_swappable
<_T1
>,
497 __is_swappable
<_T2
>>::value
>::type
498 swap(pair
<_T1
, _T2
>&, pair
<_T1
, _T2
>&) = delete;
500 #endif // __cplusplus >= 201103L
503 * @brief A convenience wrapper for creating a pair from two objects.
504 * @param __x The first object.
505 * @param __y The second object.
506 * @return A newly-constructed pair<> object of the appropriate type.
508 * The standard requires that the objects be passed by reference-to-const,
509 * but LWG issue #181 says they should be passed by const value. We follow
510 * the LWG by default.
512 // _GLIBCXX_RESOLVE_LIB_DEFECTS
513 // 181. make_pair() unintended behavior
514 #if __cplusplus >= 201103L
516 template<typename _T1
, typename _T2
>
517 constexpr pair
<typename __decay_and_strip
<_T1
>::__type
,
518 typename __decay_and_strip
<_T2
>::__type
>
519 make_pair(_T1
&& __x
, _T2
&& __y
)
521 typedef typename __decay_and_strip
<_T1
>::__type __ds_type1
;
522 typedef typename __decay_and_strip
<_T2
>::__type __ds_type2
;
523 typedef pair
<__ds_type1
, __ds_type2
> __pair_type
;
524 return __pair_type(std::forward
<_T1
>(__x
), std::forward
<_T2
>(__y
));
527 template<typename _T1
, typename _T2
>
528 inline pair
<_T1
, _T2
>
529 make_pair(_T1 __x
, _T2 __y
)
530 { return pair
<_T1
, _T2
>(__x
, __y
); }
535 _GLIBCXX_END_NAMESPACE_VERSION
538 #endif /* _STL_PAIR_H */