1 // Pair implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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 constexpr piecewise_construct_t piecewise_construct
= piecewise_construct_t();
81 // Forward declarations.
85 template<std::size_t...>
88 // Concept utility functions, reused in conditionally-explicit
90 // See PR 70437, don't look at is_constructible or
91 // is_convertible if the decayed types are the same to
92 // avoid querying those properties for incomplete types.
93 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
94 constexpr bool _ConstructiblePair()
96 return __and_
<__or_
<is_same
<typename decay
<_T1
>::type
,
97 typename decay
<_U1
>::type
>,
98 is_constructible
<_T1
, const _U1
&>>,
99 __or_
<is_same
<typename decay
<_T2
>::type
,
100 typename decay
<_U2
>::type
>,
101 is_constructible
<_T2
, const _U2
&>>>::value
;
104 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
105 constexpr bool _ImplicitlyConvertiblePair()
107 return __and_
<__or_
<is_same
<typename decay
<_T1
>::type
,
108 typename decay
<_U1
>::type
>,
109 is_convertible
<const _U1
&, _T1
>>,
110 __or_
<is_same
<typename decay
<_T2
>::type
,
111 typename decay
<_U2
>::type
>,
112 is_convertible
<const _U2
&, _T2
>>>::value
;
115 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
116 constexpr bool _MoveConstructiblePair()
118 return __and_
<__or_
<is_same
<typename decay
<_T1
>::type
,
119 typename decay
<_U1
>::type
>,
120 is_constructible
<_T1
, _U1
&&>>,
121 __or_
<is_same
<typename decay
<_T2
>::type
,
122 typename decay
<_U2
>::type
>,
123 is_constructible
<_T2
, _U2
&&>>>::value
;
126 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
127 constexpr bool _ImplicitlyMoveConvertiblePair()
129 return __and_
<__or_
<is_same
<typename decay
<_T1
>::type
,
130 typename decay
<_U1
>::type
>,
131 is_convertible
<_U1
&&, _T1
>>,
132 __or_
<is_same
<typename decay
<_T2
>::type
,
133 typename decay
<_U2
>::type
>,
134 is_convertible
<_U2
&&, _T2
>>>::value
;
141 * @brief Struct holding two objects of arbitrary type.
143 * @tparam _T1 Type of first object.
144 * @tparam _T2 Type of second object.
146 template<typename _T1
, typename _T2
>
149 typedef _T1 first_type
; /// @c first_type is the first bound type
150 typedef _T2 second_type
; /// @c second_type is the second bound type
152 _T1 first
; /// @c first is a copy of the first object
153 _T2 second
; /// @c second is a copy of the second object
155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
156 // 265. std::pair::pair() effects overly restrictive
157 /** The default constructor creates @c first and @c second using their
158 * respective default constructors. */
159 #if __cplusplus >= 201103L
160 template <typename _U1
= _T1
,
162 typename enable_if
<__and_
<
163 __is_implicitly_default_constructible
<_U1
>,
164 __is_implicitly_default_constructible
<_U2
>>
165 ::value
, bool>::type
= true>
167 _GLIBCXX_CONSTEXPR
pair()
168 : first(), second() { }
170 #if __cplusplus >= 201103L
171 template <typename _U1
= _T1
,
173 typename enable_if
<__and_
<
174 is_default_constructible
<_U1
>,
175 is_default_constructible
<_U2
>,
177 __and_
<__is_implicitly_default_constructible
<_U1
>,
178 __is_implicitly_default_constructible
<_U2
>>>>
179 ::value
, bool>::type
= false>
180 explicit constexpr pair()
181 : first(), second() { }
184 /** Two objects may be passed to a @c pair constructor to be copied. */
185 #if __cplusplus < 201103L
186 pair(const _T1
& __a
, const _T2
& __b
)
187 : first(__a
), second(__b
) { }
189 template<typename _U1
= _T1
, typename _U2
=_T2
, typename
190 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
191 && _ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
193 constexpr pair(const _T1
& __a
, const _T2
& __b
)
194 : first(__a
), second(__b
) { }
196 template<typename _U1
= _T1
, typename _U2
=_T2
, typename
197 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
198 && !_ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
200 explicit constexpr pair(const _T1
& __a
, const _T2
& __b
)
201 : first(__a
), second(__b
) { }
204 /** There is also a templated copy ctor for the @c pair class itself. */
205 #if __cplusplus < 201103L
206 template<typename _U1
, typename _U2
>
207 pair(const pair
<_U1
, _U2
>& __p
)
208 : first(__p
.first
), second(__p
.second
) { }
210 template<typename _U1
, typename _U2
, typename
211 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
212 && _ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
214 constexpr pair(const pair
<_U1
, _U2
>& __p
)
215 : first(__p
.first
), second(__p
.second
) { }
217 template<typename _U1
, typename _U2
, typename
218 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
219 && !_ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
221 explicit constexpr pair(const pair
<_U1
, _U2
>& __p
)
222 : first(__p
.first
), second(__p
.second
) { }
224 constexpr pair(const pair
&) = default;
225 constexpr pair(pair
&&) = default;
228 template<typename _U1
, typename
229 enable_if
<_ConstructiblePair
<_T2
, _T2
, _T2
, _T2
>()
230 && _MoveConstructiblePair
<_T1
, _T2
, _U1
, _T2
>()
231 && _ImplicitlyConvertiblePair
<_T2
, _T2
, _T2
, _T2
>()
232 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
235 constexpr pair(_U1
&& __x
, const _T2
& __y
)
236 : first(std::forward
<_U1
>(__x
)), second(__y
) { }
238 template<typename _U1
, typename
239 enable_if
<_ConstructiblePair
<_T2
, _T2
, _T2
, _T2
>()
240 && _MoveConstructiblePair
<_T1
, _T2
, _U1
, _T2
>()
241 && (!_ImplicitlyConvertiblePair
<_T2
, _T2
, _T2
, _T2
>()
242 || !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
245 explicit constexpr pair(_U1
&& __x
, const _T2
& __y
)
246 : first(std::forward
<_U1
>(__x
)), second(__y
) { }
248 template<typename _U2
, typename
249 enable_if
<_ConstructiblePair
<_T1
, _T1
, _T1
, _T1
>()
250 && _MoveConstructiblePair
<_T1
, _T2
, _T1
, _U2
>()
251 && _ImplicitlyConvertiblePair
<_T1
, _T1
, _T1
, _T1
>()
252 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
255 constexpr pair(const _T1
& __x
, _U2
&& __y
)
256 : first(__x
), second(std::forward
<_U2
>(__y
)) { }
258 template<typename _U2
, typename
259 enable_if
<_ConstructiblePair
<_T1
, _T1
, _T1
, _T1
>()
260 && _MoveConstructiblePair
<_T1
, _T2
, _T1
, _U2
>()
261 && (!_ImplicitlyConvertiblePair
<_T1
, _T1
, _T1
, _T1
>()
262 || !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
265 explicit pair(const _T1
& __x
, _U2
&& __y
)
266 : first(__x
), second(std::forward
<_U2
>(__y
)) { }
268 template<typename _U1
, typename _U2
, typename
269 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
270 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
273 constexpr pair(_U1
&& __x
, _U2
&& __y
)
274 : first(std::forward
<_U1
>(__x
)), second(std::forward
<_U2
>(__y
)) { }
276 template<typename _U1
, typename _U2
, typename
277 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
278 && !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
281 explicit constexpr pair(_U1
&& __x
, _U2
&& __y
)
282 : first(std::forward
<_U1
>(__x
)), second(std::forward
<_U2
>(__y
)) { }
285 template<typename _U1
, typename _U2
, typename
286 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
287 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
290 constexpr pair(pair
<_U1
, _U2
>&& __p
)
291 : first(std::forward
<_U1
>(__p
.first
)),
292 second(std::forward
<_U2
>(__p
.second
)) { }
294 template<typename _U1
, typename _U2
, typename
295 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
296 && !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
299 explicit constexpr pair(pair
<_U1
, _U2
>&& __p
)
300 : first(std::forward
<_U1
>(__p
.first
)),
301 second(std::forward
<_U2
>(__p
.second
)) { }
303 template<typename
... _Args1
, typename
... _Args2
>
304 pair(piecewise_construct_t
, tuple
<_Args1
...>, tuple
<_Args2
...>);
307 operator=(const pair
& __p
)
315 operator=(pair
&& __p
)
316 noexcept(__and_
<is_nothrow_move_assignable
<_T1
>,
317 is_nothrow_move_assignable
<_T2
>>::value
)
319 first
= std::forward
<first_type
>(__p
.first
);
320 second
= std::forward
<second_type
>(__p
.second
);
324 template<typename _U1
, typename _U2
>
326 operator=(const pair
<_U1
, _U2
>& __p
)
333 template<typename _U1
, typename _U2
>
335 operator=(pair
<_U1
, _U2
>&& __p
)
337 first
= std::forward
<_U1
>(__p
.first
);
338 second
= std::forward
<_U2
>(__p
.second
);
344 noexcept(__is_nothrow_swappable
<_T1
>::value
345 && __is_nothrow_swappable
<_T2
>::value
)
348 swap(first
, __p
.first
);
349 swap(second
, __p
.second
);
353 template<typename
... _Args1
, std::size_t... _Indexes1
,
354 typename
... _Args2
, std::size_t... _Indexes2
>
355 pair(tuple
<_Args1
...>&, tuple
<_Args2
...>&,
356 _Index_tuple
<_Indexes1
...>, _Index_tuple
<_Indexes2
...>);
360 /// Two pairs of the same type are equal iff their members are equal.
361 template<typename _T1
, typename _T2
>
362 inline _GLIBCXX_CONSTEXPR
bool
363 operator==(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
364 { return __x
.first
== __y
.first
&& __x
.second
== __y
.second
; }
366 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
367 template<typename _T1
, typename _T2
>
368 inline _GLIBCXX_CONSTEXPR
bool
369 operator<(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
370 { return __x
.first
< __y
.first
371 || (!(__y
.first
< __x
.first
) && __x
.second
< __y
.second
); }
373 /// Uses @c operator== to find the result.
374 template<typename _T1
, typename _T2
>
375 inline _GLIBCXX_CONSTEXPR
bool
376 operator!=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
377 { return !(__x
== __y
); }
379 /// Uses @c operator< to find the result.
380 template<typename _T1
, typename _T2
>
381 inline _GLIBCXX_CONSTEXPR
bool
382 operator>(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
383 { return __y
< __x
; }
385 /// Uses @c operator< to find the result.
386 template<typename _T1
, typename _T2
>
387 inline _GLIBCXX_CONSTEXPR
bool
388 operator<=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
389 { return !(__y
< __x
); }
391 /// Uses @c operator< to find the result.
392 template<typename _T1
, typename _T2
>
393 inline _GLIBCXX_CONSTEXPR
bool
394 operator>=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
395 { return !(__x
< __y
); }
397 #if __cplusplus >= 201103L
398 /// See std::pair::swap().
399 // Note: no std::swap overloads in C++03 mode, this has performance
400 // implications, see, eg, libstdc++/38466.
401 template<typename _T1
, typename _T2
>
403 swap(pair
<_T1
, _T2
>& __x
, pair
<_T1
, _T2
>& __y
)
404 noexcept(noexcept(__x
.swap(__y
)))
409 * @brief A convenience wrapper for creating a pair from two objects.
410 * @param __x The first object.
411 * @param __y The second object.
412 * @return A newly-constructed pair<> object of the appropriate type.
414 * The standard requires that the objects be passed by reference-to-const,
415 * but LWG issue #181 says they should be passed by const value. We follow
416 * the LWG by default.
418 // _GLIBCXX_RESOLVE_LIB_DEFECTS
419 // 181. make_pair() unintended behavior
420 #if __cplusplus >= 201103L
422 template<typename _T1
, typename _T2
>
423 constexpr pair
<typename __decay_and_strip
<_T1
>::__type
,
424 typename __decay_and_strip
<_T2
>::__type
>
425 make_pair(_T1
&& __x
, _T2
&& __y
)
427 typedef typename __decay_and_strip
<_T1
>::__type __ds_type1
;
428 typedef typename __decay_and_strip
<_T2
>::__type __ds_type2
;
429 typedef pair
<__ds_type1
, __ds_type2
> __pair_type
;
430 return __pair_type(std::forward
<_T1
>(__x
), std::forward
<_T2
>(__y
));
433 template<typename _T1
, typename _T2
>
434 inline pair
<_T1
, _T2
>
435 make_pair(_T1 __x
, _T2 __y
)
436 { return pair
<_T1
, _T2
>(__x
, __y
); }
441 _GLIBCXX_END_NAMESPACE_VERSION
444 #endif /* _STL_PAIR_H */