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 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
91 constexpr bool _ConstructiblePair()
93 return __and_
<is_constructible
<_T1
, const _U1
&>,
94 is_constructible
<_T2
, const _U2
&>>::value
;
97 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
98 constexpr bool _ImplicitlyConvertiblePair()
100 return __and_
<is_convertible
<const _U1
&, _T1
>,
101 is_convertible
<const _U2
&, _T2
>>::value
;
104 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
105 constexpr bool _MoveConstructiblePair()
107 return __and_
<is_constructible
<_T1
, _U1
&&>,
108 is_constructible
<_T2
, _U2
&&>>::value
;
111 template <typename _T1
, typename _T2
, typename _U1
, typename _U2
>
112 constexpr bool _ImplicitlyMoveConvertiblePair()
114 return __and_
<is_convertible
<_U1
&&, _T1
>,
115 is_convertible
<_U2
&&, _T2
>>::value
;
122 * @brief Struct holding two objects of arbitrary type.
124 * @tparam _T1 Type of first object.
125 * @tparam _T2 Type of second object.
127 template<typename _T1
, typename _T2
>
130 typedef _T1 first_type
; /// @c first_type is the first bound type
131 typedef _T2 second_type
; /// @c second_type is the second bound type
133 _T1 first
; /// @c first is a copy of the first object
134 _T2 second
; /// @c second is a copy of the second object
136 // _GLIBCXX_RESOLVE_LIB_DEFECTS
137 // 265. std::pair::pair() effects overly restrictive
138 /** The default constructor creates @c first and @c second using their
139 * respective default constructors. */
140 #if __cplusplus >= 201103L
141 template <typename _U1
= _T1
,
143 typename enable_if
<__and_
<
144 __is_implicitly_default_constructible
<_U1
>,
145 __is_implicitly_default_constructible
<_U2
>>
146 ::value
, bool>::type
= true>
148 _GLIBCXX_CONSTEXPR
pair()
149 : first(), second() { }
151 #if __cplusplus >= 201103L
152 template <typename _U1
= _T1
,
154 typename enable_if
<__and_
<
155 is_default_constructible
<_U1
>,
156 is_default_constructible
<_U2
>,
158 __and_
<__is_implicitly_default_constructible
<_U1
>,
159 __is_implicitly_default_constructible
<_U2
>>>>
160 ::value
, bool>::type
= false>
161 explicit constexpr pair()
162 : first(), second() { }
165 /** Two objects may be passed to a @c pair constructor to be copied. */
166 #if __cplusplus < 201103L
167 pair(const _T1
& __a
, const _T2
& __b
)
168 : first(__a
), second(__b
) { }
170 template<typename _U1
= _T1
, typename _U2
=_T2
, typename
171 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
172 && _ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
174 constexpr pair(const _T1
& __a
, const _T2
& __b
)
175 : first(__a
), second(__b
) { }
177 template<typename _U1
= _T1
, typename _U2
=_T2
, typename
178 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
179 && !_ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
181 explicit constexpr pair(const _T1
& __a
, const _T2
& __b
)
182 : first(__a
), second(__b
) { }
185 /** There is also a templated copy ctor for the @c pair class itself. */
186 #if __cplusplus < 201103L
187 template<typename _U1
, typename _U2
>
188 pair(const pair
<_U1
, _U2
>& __p
)
189 : first(__p
.first
), second(__p
.second
) { }
191 template<typename _U1
, typename _U2
, typename
192 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
193 && _ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
195 constexpr pair(const pair
<_U1
, _U2
>& __p
)
196 : first(__p
.first
), second(__p
.second
) { }
198 template<typename _U1
, typename _U2
, typename
199 enable_if
<_ConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
200 && !_ImplicitlyConvertiblePair
<_T1
, _T2
, _U1
, _U2
>(),
202 explicit constexpr pair(const pair
<_U1
, _U2
>& __p
)
203 : first(__p
.first
), second(__p
.second
) { }
205 constexpr pair(const pair
&) = default;
206 constexpr pair(pair
&&) = default;
209 template<typename _U1
, typename
210 enable_if
<_ConstructiblePair
<_T2
, _T2
, _T2
, _T2
>()
211 && _MoveConstructiblePair
<_T1
, _T2
, _U1
, _T2
>()
212 && _ImplicitlyConvertiblePair
<_T2
, _T2
, _T2
, _T2
>()
213 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
216 constexpr pair(_U1
&& __x
, const _T2
& __y
)
217 : first(std::forward
<_U1
>(__x
)), second(__y
) { }
219 template<typename _U1
, typename
220 enable_if
<_ConstructiblePair
<_T2
, _T2
, _T2
, _T2
>()
221 && _MoveConstructiblePair
<_T1
, _T2
, _U1
, _T2
>()
222 && (!_ImplicitlyConvertiblePair
<_T2
, _T2
, _T2
, _T2
>()
223 || !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
226 explicit constexpr pair(_U1
&& __x
, const _T2
& __y
)
227 : first(std::forward
<_U1
>(__x
)), second(__y
) { }
229 template<typename _U2
, typename
230 enable_if
<_ConstructiblePair
<_T1
, _T1
, _T1
, _T1
>()
231 && _MoveConstructiblePair
<_T1
, _T2
, _T1
, _U2
>()
232 && _ImplicitlyConvertiblePair
<_T1
, _T1
, _T1
, _T1
>()
233 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
236 constexpr pair(const _T1
& __x
, _U2
&& __y
)
237 : first(__x
), second(std::forward
<_U2
>(__y
)) { }
239 template<typename _U2
, typename
240 enable_if
<_ConstructiblePair
<_T1
, _T1
, _T1
, _T1
>()
241 && _MoveConstructiblePair
<_T1
, _T2
, _T1
, _U2
>()
242 && (!_ImplicitlyConvertiblePair
<_T1
, _T1
, _T1
, _T1
>()
243 || !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
246 explicit pair(const _T1
& __x
, _U2
&& __y
)
247 : first(__x
), second(std::forward
<_U2
>(__y
)) { }
249 template<typename _U1
, typename _U2
, typename
250 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
251 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
254 constexpr pair(_U1
&& __x
, _U2
&& __y
)
255 : first(std::forward
<_U1
>(__x
)), second(std::forward
<_U2
>(__y
)) { }
257 template<typename _U1
, typename _U2
, typename
258 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
259 && !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
262 explicit constexpr pair(_U1
&& __x
, _U2
&& __y
)
263 : first(std::forward
<_U1
>(__x
)), second(std::forward
<_U2
>(__y
)) { }
266 template<typename _U1
, typename _U2
, typename
267 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
268 && _ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
271 constexpr pair(pair
<_U1
, _U2
>&& __p
)
272 : first(std::forward
<_U1
>(__p
.first
)),
273 second(std::forward
<_U2
>(__p
.second
)) { }
275 template<typename _U1
, typename _U2
, typename
276 enable_if
<_MoveConstructiblePair
<_T1
, _T2
, _U1
, _U2
>()
277 && !_ImplicitlyMoveConvertiblePair
<_T1
, _T2
,
280 explicit constexpr pair(pair
<_U1
, _U2
>&& __p
)
281 : first(std::forward
<_U1
>(__p
.first
)),
282 second(std::forward
<_U2
>(__p
.second
)) { }
284 template<typename
... _Args1
, typename
... _Args2
>
285 pair(piecewise_construct_t
, tuple
<_Args1
...>, tuple
<_Args2
...>);
288 operator=(const pair
& __p
)
296 operator=(pair
&& __p
)
297 noexcept(__and_
<is_nothrow_move_assignable
<_T1
>,
298 is_nothrow_move_assignable
<_T2
>>::value
)
300 first
= std::forward
<first_type
>(__p
.first
);
301 second
= std::forward
<second_type
>(__p
.second
);
305 template<typename _U1
, typename _U2
>
307 operator=(const pair
<_U1
, _U2
>& __p
)
314 template<typename _U1
, typename _U2
>
316 operator=(pair
<_U1
, _U2
>&& __p
)
318 first
= std::forward
<_U1
>(__p
.first
);
319 second
= std::forward
<_U2
>(__p
.second
);
325 noexcept(__is_nothrow_swappable
<_T1
>::value
326 && __is_nothrow_swappable
<_T2
>::value
)
329 swap(first
, __p
.first
);
330 swap(second
, __p
.second
);
334 template<typename
... _Args1
, std::size_t... _Indexes1
,
335 typename
... _Args2
, std::size_t... _Indexes2
>
336 pair(tuple
<_Args1
...>&, tuple
<_Args2
...>&,
337 _Index_tuple
<_Indexes1
...>, _Index_tuple
<_Indexes2
...>);
341 /// Two pairs of the same type are equal iff their members are equal.
342 template<typename _T1
, typename _T2
>
343 inline _GLIBCXX_CONSTEXPR
bool
344 operator==(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
345 { return __x
.first
== __y
.first
&& __x
.second
== __y
.second
; }
347 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
348 template<typename _T1
, typename _T2
>
349 inline _GLIBCXX_CONSTEXPR
bool
350 operator<(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
351 { return __x
.first
< __y
.first
352 || (!(__y
.first
< __x
.first
) && __x
.second
< __y
.second
); }
354 /// Uses @c operator== to find the result.
355 template<typename _T1
, typename _T2
>
356 inline _GLIBCXX_CONSTEXPR
bool
357 operator!=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
358 { return !(__x
== __y
); }
360 /// Uses @c operator< to find the result.
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 __y
< __x
; }
366 /// Uses @c operator< to find the result.
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 !(__y
< __x
); }
372 /// Uses @c operator< to find the result.
373 template<typename _T1
, typename _T2
>
374 inline _GLIBCXX_CONSTEXPR
bool
375 operator>=(const pair
<_T1
, _T2
>& __x
, const pair
<_T1
, _T2
>& __y
)
376 { return !(__x
< __y
); }
378 #if __cplusplus >= 201103L
379 /// See std::pair::swap().
380 // Note: no std::swap overloads in C++03 mode, this has performance
381 // implications, see, eg, libstdc++/38466.
382 template<typename _T1
, typename _T2
>
384 swap(pair
<_T1
, _T2
>& __x
, pair
<_T1
, _T2
>& __y
)
385 noexcept(noexcept(__x
.swap(__y
)))
390 * @brief A convenience wrapper for creating a pair from two objects.
391 * @param __x The first object.
392 * @param __y The second object.
393 * @return A newly-constructed pair<> object of the appropriate type.
395 * The standard requires that the objects be passed by reference-to-const,
396 * but LWG issue #181 says they should be passed by const value. We follow
397 * the LWG by default.
399 // _GLIBCXX_RESOLVE_LIB_DEFECTS
400 // 181. make_pair() unintended behavior
401 #if __cplusplus >= 201103L
403 template<typename _T1
, typename _T2
>
404 constexpr pair
<typename __decay_and_strip
<_T1
>::__type
,
405 typename __decay_and_strip
<_T2
>::__type
>
406 make_pair(_T1
&& __x
, _T2
&& __y
)
408 typedef typename __decay_and_strip
<_T1
>::__type __ds_type1
;
409 typedef typename __decay_and_strip
<_T2
>::__type __ds_type2
;
410 typedef pair
<__ds_type1
, __ds_type2
> __pair_type
;
411 return __pair_type(std::forward
<_T1
>(__x
), std::forward
<_T2
>(__y
));
414 template<typename _T1
, typename _T2
>
415 inline pair
<_T1
, _T2
>
416 make_pair(_T1 __x
, _T2 __y
)
417 { return pair
<_T1
, _T2
>(__x
, __y
); }
422 _GLIBCXX_END_NAMESPACE_VERSION
425 #endif /* _STL_PAIR_H */