7057030d9d7f0c327199d48c6947ffdc3ba76844
[gcc.git] / libstdc++-v3 / include / bits / stl_pair.h
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
65 namespace 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
79 constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
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 template <typename _T1, typename _T2, typename _U1, typename _U2>
91 constexpr bool _ConstructiblePair()
92 {
93 return __and_<is_constructible<_T1, const _U1&>,
94 is_constructible<_T2, const _U2&>>::value;
95 }
96
97 template <typename _T1, typename _T2, typename _U1, typename _U2>
98 constexpr bool _ImplicitlyConvertiblePair()
99 {
100 return __and_<is_convertible<const _U1&, _T1>,
101 is_convertible<const _U2&, _T2>>::value;
102 }
103
104 template <typename _T1, typename _T2, typename _U1, typename _U2>
105 constexpr bool _MoveConstructiblePair()
106 {
107 return __and_<is_constructible<_T1, _U1&&>,
108 is_constructible<_T2, _U2&&>>::value;
109 }
110
111 template <typename _T1, typename _T2, typename _U1, typename _U2>
112 constexpr bool _ImplicitlyMoveConvertiblePair()
113 {
114 return __and_<is_convertible<_U1&&, _T1>,
115 is_convertible<_U2&&, _T2>>::value;
116 }
117
118
119 #endif
120
121 /**
122 * @brief Struct holding two objects of arbitrary type.
123 *
124 * @tparam _T1 Type of first object.
125 * @tparam _T2 Type of second object.
126 */
127 template<typename _T1, typename _T2>
128 struct pair
129 {
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
132
133 _T1 first; /// @c first is a copy of the first object
134 _T2 second; /// @c second is a copy of the second object
135
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,
142 typename _U2 = _T2,
143 typename enable_if<__and_<
144 __is_implicitly_default_constructible<_U1>,
145 __is_implicitly_default_constructible<_U2>>
146 ::value, bool>::type = true>
147 #endif
148 _GLIBCXX_CONSTEXPR pair()
149 : first(), second() { }
150
151 #if __cplusplus >= 201103L
152 template <typename _U1 = _T1,
153 typename _U2 = _T2,
154 typename enable_if<__and_<
155 is_default_constructible<_U1>,
156 is_default_constructible<_U2>,
157 __not_<
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() { }
163 #endif
164
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) { }
169 #else
170 template<typename _U1 = _T1, typename _U2=_T2, typename
171 enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>()
172 && _ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(),
173 bool>::type=true>
174 constexpr pair(const _T1& __a, const _T2& __b)
175 : first(__a), second(__b) { }
176
177 template<typename _U1 = _T1, typename _U2=_T2, typename
178 enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>()
179 && !_ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(),
180 bool>::type=false>
181 explicit constexpr pair(const _T1& __a, const _T2& __b)
182 : first(__a), second(__b) { }
183 #endif
184
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) { }
190 #else
191 template<typename _U1, typename _U2, typename
192 enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>()
193 && _ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(),
194 bool>::type=true>
195 constexpr pair(const pair<_U1, _U2>& __p)
196 : first(__p.first), second(__p.second) { }
197
198 template<typename _U1, typename _U2, typename
199 enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>()
200 && !_ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(),
201 bool>::type=false>
202 explicit constexpr pair(const pair<_U1, _U2>& __p)
203 : first(__p.first), second(__p.second) { }
204
205 constexpr pair(const pair&) = default;
206 constexpr pair(pair&&) = default;
207
208 // DR 811.
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,
214 _U1, _T2>(),
215 bool>::type=true>
216 constexpr pair(_U1&& __x, const _T2& __y)
217 : first(std::forward<_U1>(__x)), second(__y) { }
218
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,
224 _U1, _T2>()),
225 bool>::type=false>
226 explicit constexpr pair(_U1&& __x, const _T2& __y)
227 : first(std::forward<_U1>(__x)), second(__y) { }
228
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,
234 _T1, _U2>(),
235 bool>::type=true>
236 constexpr pair(const _T1& __x, _U2&& __y)
237 : first(__x), second(std::forward<_U2>(__y)) { }
238
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,
244 _T1, _U2>()),
245 bool>::type=false>
246 explicit pair(const _T1& __x, _U2&& __y)
247 : first(__x), second(std::forward<_U2>(__y)) { }
248
249 template<typename _U1, typename _U2, typename
250 enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>()
251 && _ImplicitlyMoveConvertiblePair<_T1, _T2,
252 _U1, _U2>(),
253 bool>::type=true>
254 constexpr pair(_U1&& __x, _U2&& __y)
255 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
256
257 template<typename _U1, typename _U2, typename
258 enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>()
259 && !_ImplicitlyMoveConvertiblePair<_T1, _T2,
260 _U1, _U2>(),
261 bool>::type=false>
262 explicit constexpr pair(_U1&& __x, _U2&& __y)
263 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
264
265
266 template<typename _U1, typename _U2, typename
267 enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>()
268 && _ImplicitlyMoveConvertiblePair<_T1, _T2,
269 _U1, _U2>(),
270 bool>::type=true>
271 constexpr pair(pair<_U1, _U2>&& __p)
272 : first(std::forward<_U1>(__p.first)),
273 second(std::forward<_U2>(__p.second)) { }
274
275 template<typename _U1, typename _U2, typename
276 enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>()
277 && !_ImplicitlyMoveConvertiblePair<_T1, _T2,
278 _U1, _U2>(),
279 bool>::type=false>
280 explicit constexpr pair(pair<_U1, _U2>&& __p)
281 : first(std::forward<_U1>(__p.first)),
282 second(std::forward<_U2>(__p.second)) { }
283
284 template<typename... _Args1, typename... _Args2>
285 pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
286
287 pair&
288 operator=(const pair& __p)
289 {
290 first = __p.first;
291 second = __p.second;
292 return *this;
293 }
294
295 pair&
296 operator=(pair&& __p)
297 noexcept(__and_<is_nothrow_move_assignable<_T1>,
298 is_nothrow_move_assignable<_T2>>::value)
299 {
300 first = std::forward<first_type>(__p.first);
301 second = std::forward<second_type>(__p.second);
302 return *this;
303 }
304
305 template<typename _U1, typename _U2>
306 pair&
307 operator=(const pair<_U1, _U2>& __p)
308 {
309 first = __p.first;
310 second = __p.second;
311 return *this;
312 }
313
314 template<typename _U1, typename _U2>
315 pair&
316 operator=(pair<_U1, _U2>&& __p)
317 {
318 first = std::forward<_U1>(__p.first);
319 second = std::forward<_U2>(__p.second);
320 return *this;
321 }
322
323 void
324 swap(pair& __p)
325 noexcept(__is_nothrow_swappable<_T1>::value
326 && __is_nothrow_swappable<_T2>::value)
327 {
328 using std::swap;
329 swap(first, __p.first);
330 swap(second, __p.second);
331 }
332
333 private:
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...>);
338 #endif
339 };
340
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; }
346
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); }
353
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); }
359
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; }
365
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); }
371
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); }
377
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>
383 inline void
384 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
385 noexcept(noexcept(__x.swap(__y)))
386 { __x.swap(__y); }
387 #endif
388
389 /**
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.
394 *
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.
398 */
399 // _GLIBCXX_RESOLVE_LIB_DEFECTS
400 // 181. make_pair() unintended behavior
401 #if __cplusplus >= 201103L
402 // NB: DR 706.
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)
407 {
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));
412 }
413 #else
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); }
418 #endif
419
420 /// @}
421
422 _GLIBCXX_END_NAMESPACE_VERSION
423 } // namespace std
424
425 #endif /* _STL_PAIR_H */