libstdc++
stl_pair.h
Go to the documentation of this file.
1// Pair implementation -*- C++ -*-
2
3// Copyright (C) 2001-2016 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#include <bits/move.h> // for std::move / std::forward, and std::swap
60
61#if __cplusplus >= 201103L
62#include <type_traits> // for std::__decay_and_strip too
63#endif
64
65namespace std _GLIBCXX_VISIBILITY(default)
66{
67_GLIBCXX_BEGIN_NAMESPACE_VERSION
68
69 /**
70 * @addtogroup utilities
71 * @{
72 */
73
74#if __cplusplus >= 201103L
75 /// piecewise_construct_t
76 struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
77
78 /// piecewise_construct
80
81 // Forward declarations.
82 template<typename...>
83 class tuple;
84
85 template<std::size_t...>
86 struct _Index_tuple;
87
88 // Concept utility functions, reused in conditionally-explicit
89 // constructors.
90 // See PR 70437, don't look at is_constructible or
91 // is_convertible if the types are the same to
92 // avoid querying those properties for incomplete types.
93 template <bool, typename _T1, typename _T2>
94 struct _PCC
95 {
96 template <typename _U1, typename _U2>
97 static constexpr bool _ConstructiblePair()
98 {
101 }
102
103 template <typename _U1, typename _U2>
104 static constexpr bool _ImplicitlyConvertiblePair()
105 {
106 return __and_<is_convertible<const _U1&, _T1>,
107 is_convertible<const _U2&, _T2>>::value;
108 }
109
110 template <typename _U1, typename _U2>
111 static constexpr bool _MoveConstructiblePair()
112 {
113 return __and_<is_constructible<_T1, _U1&&>,
114 is_constructible<_T2, _U2&&>>::value;
115 }
116
117 template <typename _U1, typename _U2>
118 static constexpr bool _ImplicitlyMoveConvertiblePair()
119 {
120 return __and_<is_convertible<_U1&&, _T1>,
121 is_convertible<_U2&&, _T2>>::value;
122 }
123
124 template <bool __implicit, typename _U1, typename _U2>
125 static constexpr bool _CopyMovePair()
126 {
127 using __do_converts = __and_<is_convertible<const _U1&, _T1>,
128 is_convertible<_U2&&, _T2>>;
129 using __converts = typename conditional<__implicit,
130 __do_converts,
131 __not_<__do_converts>>::type;
132 return __and_<is_constructible<_T1, const _U1&>,
133 is_constructible<_T2, _U2&&>,
134 __converts
135 >::value;
136 }
137
138 template <bool __implicit, typename _U1, typename _U2>
139 static constexpr bool _MoveCopyPair()
140 {
141 using __do_converts = __and_<is_convertible<_U1&&, _T1>,
142 is_convertible<const _U2&, _T2>>;
143 using __converts = typename conditional<__implicit,
144 __do_converts,
145 __not_<__do_converts>>::type;
146 return __and_<is_constructible<_T1, _U1&&>,
147 is_constructible<_T2, const _U2&&>,
148 __converts
149 >::value;
150 }
151 };
152
153 template <typename _T1, typename _T2>
154 struct _PCC<false, _T1, _T2>
155 {
156 template <typename _U1, typename _U2>
157 static constexpr bool _ConstructiblePair()
158 {
159 return false;
160 }
161
162 template <typename _U1, typename _U2>
163 static constexpr bool _ImplicitlyConvertiblePair()
164 {
165 return false;
166 }
167
168 template <typename _U1, typename _U2>
169 static constexpr bool _MoveConstructiblePair()
170 {
171 return false;
172 }
173
174 template <typename _U1, typename _U2>
175 static constexpr bool _ImplicitlyMoveConvertiblePair()
176 {
177 return false;
178 }
179 };
180
181 struct __wrap_nonesuch : std::__nonesuch {
182 explicit __wrap_nonesuch(const __nonesuch&) = delete;
183 };
184#endif // C++11
185
186 template<typename _U1, typename _U2> class __pair_base
187 {
188#if __cplusplus >= 201103L
189 template<typename _T1, typename _T2> friend struct pair;
190 __pair_base() = default;
191 ~__pair_base() = default;
192 __pair_base(const __pair_base&) = default;
193 __pair_base& operator=(const __pair_base&) = delete;
194#endif // C++11
195 };
196
197 /**
198 * @brief Struct holding two objects of arbitrary type.
199 *
200 * @tparam _T1 Type of first object.
201 * @tparam _T2 Type of second object.
202 */
203 template<typename _T1, typename _T2>
204 struct pair
205 : private __pair_base<_T1, _T2>
206 {
207 typedef _T1 first_type; /// @c first_type is the first bound type
208 typedef _T2 second_type; /// @c second_type is the second bound type
209
210 _T1 first; /// @c first is a copy of the first object
211 _T2 second; /// @c second is a copy of the second object
212
213 // _GLIBCXX_RESOLVE_LIB_DEFECTS
214 // 265. std::pair::pair() effects overly restrictive
215 /** The default constructor creates @c first and @c second using their
216 * respective default constructors. */
217#if __cplusplus >= 201103L
218 template <typename _U1 = _T1,
219 typename _U2 = _T2,
220 typename enable_if<__and_<
223 ::value, bool>::type = true>
224#endif
227
228#if __cplusplus >= 201103L
229 template <typename _U1 = _T1,
230 typename _U2 = _T2,
231 typename enable_if<__and_<
234 __not_<
237 ::value, bool>::type = false>
238 explicit constexpr pair()
239 : first(), second() { }
240#endif
241
242 /** Two objects may be passed to a @c pair constructor to be copied. */
243#if __cplusplus < 201103L
244 pair(const _T1& __a, const _T2& __b)
245 : first(__a), second(__b) { }
246#else
247 // Shortcut for constraining the templates that don't take pairs.
249
250 template<typename _U1 = _T1, typename _U2=_T2, typename
251 enable_if<_PCCP::template
253 && _PCCP::template
255 bool>::type=true>
256 constexpr pair(const _T1& __a, const _T2& __b)
257 : first(__a), second(__b) { }
258
259 template<typename _U1 = _T1, typename _U2=_T2, typename
260 enable_if<_PCCP::template
262 && !_PCCP::template
264 bool>::type=false>
265 explicit constexpr pair(const _T1& __a, const _T2& __b)
266 : first(__a), second(__b) { }
267#endif
268
269 /** There is also a templated copy ctor for the @c pair class itself. */
270#if __cplusplus < 201103L
271 template<typename _U1, typename _U2>
272 pair(const pair<_U1, _U2>& __p)
273 : first(__p.first), second(__p.second) { }
274#else
275 // Shortcut for constraining the templates that take pairs.
276 template <typename _U1, typename _U2>
278 || !is_same<_T2, _U2>::value,
279 _T1, _T2>;
280
281 template<typename _U1, typename _U2, typename
286 bool>::type=true>
287 constexpr pair(const pair<_U1, _U2>& __p)
288 : first(__p.first), second(__p.second) { }
289
290 template<typename _U1, typename _U2, typename
293 && !_PCCFP<_U1, _U2>::template
295 bool>::type=false>
296 explicit constexpr pair(const pair<_U1, _U2>& __p)
297 : first(__p.first), second(__p.second) { }
298
299 constexpr pair(const pair&) = default;
300 constexpr pair(pair&&) = default;
301
302 // DR 811.
303 template<typename _U1, typename
304 enable_if<_PCCP::template
305 _MoveCopyPair<true, _U1, _T2>(),
306 bool>::type=true>
307 constexpr pair(_U1&& __x, const _T2& __y)
308 : first(std::forward<_U1>(__x)), second(__y) { }
309
310 template<typename _U1, typename
311 enable_if<_PCCP::template
312 _MoveCopyPair<false, _U1, _T2>(),
313 bool>::type=false>
314 explicit constexpr pair(_U1&& __x, const _T2& __y)
315 : first(std::forward<_U1>(__x)), second(__y) { }
316
317 template<typename _U2, typename
318 enable_if<_PCCP::template
319 _CopyMovePair<true, _T1, _U2>(),
320 bool>::type=true>
321 constexpr pair(const _T1& __x, _U2&& __y)
322 : first(__x), second(std::forward<_U2>(__y)) { }
323
324 template<typename _U2, typename
325 enable_if<_PCCP::template
326 _CopyMovePair<false, _T1, _U2>(),
327 bool>::type=false>
328 explicit pair(const _T1& __x, _U2&& __y)
329 : first(__x), second(std::forward<_U2>(__y)) { }
330
331 template<typename _U1, typename _U2, typename
332 enable_if<_PCCP::template
333 _MoveConstructiblePair<_U1, _U2>()
334 && _PCCP::template
335 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
336 bool>::type=true>
337 constexpr pair(_U1&& __x, _U2&& __y)
338 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
339
340 template<typename _U1, typename _U2, typename
341 enable_if<_PCCP::template
342 _MoveConstructiblePair<_U1, _U2>()
343 && !_PCCP::template
344 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
345 bool>::type=false>
346 explicit constexpr pair(_U1&& __x, _U2&& __y)
347 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
348
349
350 template<typename _U1, typename _U2, typename
351 enable_if<_PCCFP<_U1, _U2>::template
352 _MoveConstructiblePair<_U1, _U2>()
353 && _PCCFP<_U1, _U2>::template
354 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
355 bool>::type=true>
356 constexpr pair(pair<_U1, _U2>&& __p)
357 : first(std::forward<_U1>(__p.first)),
358 second(std::forward<_U2>(__p.second)) { }
359
360 template<typename _U1, typename _U2, typename
361 enable_if<_PCCFP<_U1, _U2>::template
362 _MoveConstructiblePair<_U1, _U2>()
363 && !_PCCFP<_U1, _U2>::template
364 _ImplicitlyMoveConvertiblePair<_U1, _U2>(),
365 bool>::type=false>
366 explicit constexpr pair(pair<_U1, _U2>&& __p)
367 : first(std::forward<_U1>(__p.first)),
368 second(std::forward<_U2>(__p.second)) { }
369
370 template<typename... _Args1, typename... _Args2>
371 pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
372
373 pair&
374 operator=(typename conditional<
375 __and_<is_copy_assignable<_T1>,
376 is_copy_assignable<_T2>>::value,
377 const pair&, const __wrap_nonesuch&>::type __p)
378 {
379 first = __p.first;
380 second = __p.second;
381 return *this;
382 }
383
384 pair&
385 operator=(typename conditional<
386 __and_<is_move_assignable<_T1>,
387 is_move_assignable<_T2>>::value,
388 pair&&, __wrap_nonesuch&&>::type __p)
389 noexcept(__and_<is_nothrow_move_assignable<_T1>,
390 is_nothrow_move_assignable<_T2>>::value)
391 {
392 first = std::forward<first_type>(__p.first);
393 second = std::forward<second_type>(__p.second);
394 return *this;
395 }
396
397 template<typename _U1, typename _U2>
398 typename enable_if<__and_<is_assignable<_T1&, const _U1&>,
399 is_assignable<_T2&, const _U2&>>::value,
400 pair&>::type
401 operator=(const pair<_U1, _U2>& __p)
402 {
403 first = __p.first;
404 second = __p.second;
405 return *this;
406 }
407
408 template<typename _U1, typename _U2>
409 typename enable_if<__and_<is_assignable<_T1&, _U1&&>,
410 is_assignable<_T2&, _U2&&>>::value,
411 pair&>::type
412 operator=(pair<_U1, _U2>&& __p)
413 {
414 first = std::forward<_U1>(__p.first);
415 second = std::forward<_U2>(__p.second);
416 return *this;
417 }
418
419 void
420 swap(pair& __p)
421 noexcept(__is_nothrow_swappable<_T1>::value
422 && __is_nothrow_swappable<_T2>::value)
423 {
424 using std::swap;
425 swap(first, __p.first);
426 swap(second, __p.second);
427 }
428
429 private:
430 template<typename... _Args1, std::size_t... _Indexes1,
431 typename... _Args2, std::size_t... _Indexes2>
432 pair(tuple<_Args1...>&, tuple<_Args2...>&,
433 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
434#endif
435 };
436
437 /// Two pairs of the same type are equal iff their members are equal.
438 template<typename _T1, typename _T2>
439 inline _GLIBCXX_CONSTEXPR bool
440 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
441 { return __x.first == __y.first && __x.second == __y.second; }
442
443 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
444 template<typename _T1, typename _T2>
446 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
447 { return __x.first < __y.first
448 || (!(__y.first < __x.first) && __x.second < __y.second); }
449
450 /// Uses @c operator== to find the result.
451 template<typename _T1, typename _T2>
452 inline _GLIBCXX_CONSTEXPR bool
453 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
454 { return !(__x == __y); }
455
456 /// Uses @c operator< to find the result.
457 template<typename _T1, typename _T2>
458 inline _GLIBCXX_CONSTEXPR bool
459 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
460 { return __y < __x; }
461
462 /// Uses @c operator< to find the result.
463 template<typename _T1, typename _T2>
465 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
466 { return !(__y < __x); }
467
468 /// Uses @c operator< to find the result.
469 template<typename _T1, typename _T2>
470 inline _GLIBCXX_CONSTEXPR bool
471 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
472 { return !(__x < __y); }
473
474#if __cplusplus >= 201103L
475 /// See std::pair::swap().
476 // Note: no std::swap overloads in C++03 mode, this has performance
477 // implications, see, eg, libstdc++/38466.
478 template<typename _T1, typename _T2>
479 inline void
481 noexcept(noexcept(__x.swap(__y)))
482 { __x.swap(__y); }
483#endif
484
485 /**
486 * @brief A convenience wrapper for creating a pair from two objects.
487 * @param __x The first object.
488 * @param __y The second object.
489 * @return A newly-constructed pair<> object of the appropriate type.
490 *
491 * The standard requires that the objects be passed by reference-to-const,
492 * but LWG issue #181 says they should be passed by const value. We follow
493 * the LWG by default.
494 */
495 // _GLIBCXX_RESOLVE_LIB_DEFECTS
496 // 181. make_pair() unintended behavior
497#if __cplusplus >= 201103L
498 // NB: DR 706.
499 template<typename _T1, typename _T2>
500 constexpr pair<typename __decay_and_strip<_T1>::__type,
501 typename __decay_and_strip<_T2>::__type>
509#else
510 template<typename _T1, typename _T2>
511 inline pair<_T1, _T2>
512 make_pair(_T1 __x, _T2 __y)
513 { return pair<_T1, _T2>(__x, __y); }
514#endif
515
516 /// @}
517
518_GLIBCXX_END_NAMESPACE_VERSION
519} // namespace std
520
521#endif /* _STL_PAIR_H */
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:502
constexpr piecewise_construct_t piecewise_construct
piecewise_construct
Definition stl_pair.h:79
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:76
ISO C++ entities toplevel namespace is std.
Primary class template, tuple.
Definition tuple:555
piecewise_construct_t
Definition stl_pair.h:76
Struct holding two objects of arbitrary type.
Definition stl_pair.h:206
_T1 first
second_type is the second bound type
Definition stl_pair.h:210
constexpr pair()
second is a copy of the second object
Definition stl_pair.h:225
_T2 second_type
first_type is the first bound type
Definition stl_pair.h:208
_T2 second
first is a copy of the first object
Definition stl_pair.h:211