]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/tuple
stl_iterator.h (make_reverse_iterator): LWG DR 2285.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / tuple
1 // <tuple> -*- C++ -*-
2
3 // Copyright (C) 2007-2014 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 /** @file include/tuple
26 * This is a Standard C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_TUPLE
30 #define _GLIBCXX_TUPLE 1
31
32 #pragma GCC system_header
33
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <utility>
39 #include <array>
40 #include <bits/uses_allocator.h>
41
42 namespace std _GLIBCXX_VISIBILITY(default)
43 {
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 /**
47 * @addtogroup utilities
48 * @{
49 */
50
51 template<std::size_t _Idx, typename _Head, bool _IsEmptyNotFinal>
52 struct _Head_base;
53
54 template<std::size_t _Idx, typename _Head>
55 struct _Head_base<_Idx, _Head, true>
56 : public _Head
57 {
58 constexpr _Head_base()
59 : _Head() { }
60
61 constexpr _Head_base(const _Head& __h)
62 : _Head(__h) { }
63
64 constexpr _Head_base(const _Head_base&) = default;
65 constexpr _Head_base(_Head_base&&) = default;
66
67 template<typename _UHead>
68 constexpr _Head_base(_UHead&& __h)
69 : _Head(std::forward<_UHead>(__h)) { }
70
71 _Head_base(allocator_arg_t, __uses_alloc0)
72 : _Head() { }
73
74 template<typename _Alloc>
75 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
76 : _Head(allocator_arg, *__a._M_a) { }
77
78 template<typename _Alloc>
79 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
80 : _Head(*__a._M_a) { }
81
82 template<typename _UHead>
83 _Head_base(__uses_alloc0, _UHead&& __uhead)
84 : _Head(std::forward<_UHead>(__uhead)) { }
85
86 template<typename _Alloc, typename _UHead>
87 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
88 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
89
90 template<typename _Alloc, typename _UHead>
91 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
92 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
93
94 static constexpr _Head&
95 _M_head(_Head_base& __b) noexcept { return __b; }
96
97 static constexpr const _Head&
98 _M_head(const _Head_base& __b) noexcept { return __b; }
99 };
100
101 template<std::size_t _Idx, typename _Head>
102 struct _Head_base<_Idx, _Head, false>
103 {
104 constexpr _Head_base()
105 : _M_head_impl() { }
106
107 constexpr _Head_base(const _Head& __h)
108 : _M_head_impl(__h) { }
109
110 constexpr _Head_base(const _Head_base&) = default;
111 constexpr _Head_base(_Head_base&&) = default;
112
113 template<typename _UHead>
114 constexpr _Head_base(_UHead&& __h)
115 : _M_head_impl(std::forward<_UHead>(__h)) { }
116
117 _Head_base(allocator_arg_t, __uses_alloc0)
118 : _M_head_impl() { }
119
120 template<typename _Alloc>
121 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
122 : _M_head_impl(allocator_arg, *__a._M_a) { }
123
124 template<typename _Alloc>
125 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
126 : _M_head_impl(*__a._M_a) { }
127
128 template<typename _UHead>
129 _Head_base(__uses_alloc0, _UHead&& __uhead)
130 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
131
132 template<typename _Alloc, typename _UHead>
133 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
134 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
135 { }
136
137 template<typename _Alloc, typename _UHead>
138 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
139 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
140
141 static constexpr _Head&
142 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
143
144 static constexpr const _Head&
145 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
146
147 _Head _M_head_impl;
148 };
149
150 /**
151 * Contains the actual implementation of the @c tuple template, stored
152 * as a recursive inheritance hierarchy from the first element (most
153 * derived class) to the last (least derived class). The @c Idx
154 * parameter gives the 0-based index of the element stored at this
155 * point in the hierarchy; we use it to implement a constant-time
156 * get() operation.
157 */
158 template<std::size_t _Idx, typename... _Elements>
159 struct _Tuple_impl;
160
161 /**
162 * Zero-element tuple implementation. This is the basis case for the
163 * inheritance recursion.
164 */
165 template<std::size_t _Idx>
166 struct _Tuple_impl<_Idx>
167 {
168 template<std::size_t, typename...> friend class _Tuple_impl;
169
170 _Tuple_impl() = default;
171
172 template<typename _Alloc>
173 _Tuple_impl(allocator_arg_t, const _Alloc&) { }
174
175 template<typename _Alloc>
176 _Tuple_impl(allocator_arg_t, const _Alloc&, const _Tuple_impl&) { }
177
178 template<typename _Alloc>
179 _Tuple_impl(allocator_arg_t, const _Alloc&, _Tuple_impl&&) { }
180
181 protected:
182 void _M_swap(_Tuple_impl&) noexcept { /* no-op */ }
183 };
184
185 template<typename _Tp>
186 struct __is_empty_non_tuple : is_empty<_Tp> { };
187
188 // Using EBO for elements that are tuples causes ambiguous base errors.
189 template<typename _El0, typename... _El>
190 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
191
192 // Use the Empty Base-class Optimization for empty, non-final types.
193 template<typename _Tp>
194 using __empty_not_final
195 = typename conditional<__is_final(_Tp), false_type,
196 __is_empty_non_tuple<_Tp>>::type;
197
198 /**
199 * Recursive tuple implementation. Here we store the @c Head element
200 * and derive from a @c Tuple_impl containing the remaining elements
201 * (which contains the @c Tail).
202 */
203 template<std::size_t _Idx, typename _Head, typename... _Tail>
204 struct _Tuple_impl<_Idx, _Head, _Tail...>
205 : public _Tuple_impl<_Idx + 1, _Tail...>,
206 private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
207 {
208 template<std::size_t, typename...> friend class _Tuple_impl;
209
210 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
211 typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
212
213 static constexpr _Head&
214 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
215
216 static constexpr const _Head&
217 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
218
219 static constexpr _Inherited&
220 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
221
222 static constexpr const _Inherited&
223 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
224
225 constexpr _Tuple_impl()
226 : _Inherited(), _Base() { }
227
228 explicit
229 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
230 : _Inherited(__tail...), _Base(__head) { }
231
232 template<typename _UHead, typename... _UTail, typename = typename
233 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
234 explicit
235 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
236 : _Inherited(std::forward<_UTail>(__tail)...),
237 _Base(std::forward<_UHead>(__head)) { }
238
239 constexpr _Tuple_impl(const _Tuple_impl&) = default;
240
241 constexpr
242 _Tuple_impl(_Tuple_impl&& __in)
243 noexcept(__and_<is_nothrow_move_constructible<_Head>,
244 is_nothrow_move_constructible<_Inherited>>::value)
245 : _Inherited(std::move(_M_tail(__in))),
246 _Base(std::forward<_Head>(_M_head(__in))) { }
247
248 template<typename... _UElements>
249 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
250 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
251 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
252
253 template<typename _UHead, typename... _UTails>
254 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
255 : _Inherited(std::move
256 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
257 _Base(std::forward<_UHead>
258 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
259
260 template<typename _Alloc>
261 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
262 : _Inherited(__tag, __a),
263 _Base(__tag, __use_alloc<_Head>(__a)) { }
264
265 template<typename _Alloc>
266 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
267 const _Head& __head, const _Tail&... __tail)
268 : _Inherited(__tag, __a, __tail...),
269 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
270
271 template<typename _Alloc, typename _UHead, typename... _UTail,
272 typename = typename enable_if<sizeof...(_Tail)
273 == sizeof...(_UTail)>::type>
274 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
275 _UHead&& __head, _UTail&&... __tail)
276 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
277 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
278 std::forward<_UHead>(__head)) { }
279
280 template<typename _Alloc>
281 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
282 const _Tuple_impl& __in)
283 : _Inherited(__tag, __a, _M_tail(__in)),
284 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
285
286 template<typename _Alloc>
287 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
288 _Tuple_impl&& __in)
289 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
290 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
291 std::forward<_Head>(_M_head(__in))) { }
292
293 template<typename _Alloc, typename... _UElements>
294 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
295 const _Tuple_impl<_Idx, _UElements...>& __in)
296 : _Inherited(__tag, __a,
297 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
298 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
299 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
300
301 template<typename _Alloc, typename _UHead, typename... _UTails>
302 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
303 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
304 : _Inherited(__tag, __a, std::move
305 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
306 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
307 std::forward<_UHead>
308 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
309
310 _Tuple_impl&
311 operator=(const _Tuple_impl& __in)
312 {
313 _M_head(*this) = _M_head(__in);
314 _M_tail(*this) = _M_tail(__in);
315 return *this;
316 }
317
318 _Tuple_impl&
319 operator=(_Tuple_impl&& __in)
320 noexcept(__and_<is_nothrow_move_assignable<_Head>,
321 is_nothrow_move_assignable<_Inherited>>::value)
322 {
323 _M_head(*this) = std::forward<_Head>(_M_head(__in));
324 _M_tail(*this) = std::move(_M_tail(__in));
325 return *this;
326 }
327
328 template<typename... _UElements>
329 _Tuple_impl&
330 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
331 {
332 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
333 _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
334 return *this;
335 }
336
337 template<typename _UHead, typename... _UTails>
338 _Tuple_impl&
339 operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
340 {
341 _M_head(*this) = std::forward<_UHead>
342 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
343 _M_tail(*this) = std::move
344 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
345 return *this;
346 }
347
348 protected:
349 void
350 _M_swap(_Tuple_impl& __in)
351 noexcept(noexcept(swap(std::declval<_Head&>(),
352 std::declval<_Head&>()))
353 && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
354 {
355 using std::swap;
356 swap(_M_head(*this), _M_head(__in));
357 _Inherited::_M_swap(_M_tail(__in));
358 }
359 };
360
361 /// Primary class template, tuple
362 template<typename... _Elements>
363 class tuple : public _Tuple_impl<0, _Elements...>
364 {
365 typedef _Tuple_impl<0, _Elements...> _Inherited;
366
367 public:
368 constexpr tuple()
369 : _Inherited() { }
370
371 explicit
372 constexpr tuple(const _Elements&... __elements)
373 : _Inherited(__elements...) { }
374
375 template<typename... _UElements, typename = typename
376 enable_if<__and_<is_convertible<_UElements,
377 _Elements>...>::value>::type>
378 explicit
379 constexpr tuple(_UElements&&... __elements)
380 : _Inherited(std::forward<_UElements>(__elements)...) { }
381
382 constexpr tuple(const tuple&) = default;
383
384 constexpr tuple(tuple&&) = default;
385
386 template<typename... _UElements, typename = typename
387 enable_if<__and_<is_convertible<const _UElements&,
388 _Elements>...>::value>::type>
389 constexpr tuple(const tuple<_UElements...>& __in)
390 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
391 { }
392
393 template<typename... _UElements, typename = typename
394 enable_if<__and_<is_convertible<_UElements,
395 _Elements>...>::value>::type>
396 constexpr tuple(tuple<_UElements...>&& __in)
397 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
398
399 // Allocator-extended constructors.
400
401 template<typename _Alloc>
402 tuple(allocator_arg_t __tag, const _Alloc& __a)
403 : _Inherited(__tag, __a) { }
404
405 template<typename _Alloc>
406 tuple(allocator_arg_t __tag, const _Alloc& __a,
407 const _Elements&... __elements)
408 : _Inherited(__tag, __a, __elements...) { }
409
410 template<typename _Alloc, typename... _UElements, typename = typename
411 enable_if<sizeof...(_UElements)
412 == sizeof...(_Elements)>::type>
413 tuple(allocator_arg_t __tag, const _Alloc& __a,
414 _UElements&&... __elements)
415 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
416 { }
417
418 template<typename _Alloc>
419 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
420 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
421
422 template<typename _Alloc>
423 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
424 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
425
426 template<typename _Alloc, typename... _UElements, typename = typename
427 enable_if<sizeof...(_UElements)
428 == sizeof...(_Elements)>::type>
429 tuple(allocator_arg_t __tag, const _Alloc& __a,
430 const tuple<_UElements...>& __in)
431 : _Inherited(__tag, __a,
432 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
433 { }
434
435 template<typename _Alloc, typename... _UElements, typename = typename
436 enable_if<sizeof...(_UElements)
437 == sizeof...(_Elements)>::type>
438 tuple(allocator_arg_t __tag, const _Alloc& __a,
439 tuple<_UElements...>&& __in)
440 : _Inherited(__tag, __a,
441 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
442 { }
443
444 tuple&
445 operator=(const tuple& __in)
446 {
447 static_cast<_Inherited&>(*this) = __in;
448 return *this;
449 }
450
451 tuple&
452 operator=(tuple&& __in)
453 noexcept(is_nothrow_move_assignable<_Inherited>::value)
454 {
455 static_cast<_Inherited&>(*this) = std::move(__in);
456 return *this;
457 }
458
459 template<typename... _UElements, typename = typename
460 enable_if<sizeof...(_UElements)
461 == sizeof...(_Elements)>::type>
462 tuple&
463 operator=(const tuple<_UElements...>& __in)
464 {
465 static_cast<_Inherited&>(*this) = __in;
466 return *this;
467 }
468
469 template<typename... _UElements, typename = typename
470 enable_if<sizeof...(_UElements)
471 == sizeof...(_Elements)>::type>
472 tuple&
473 operator=(tuple<_UElements...>&& __in)
474 {
475 static_cast<_Inherited&>(*this) = std::move(__in);
476 return *this;
477 }
478
479 void
480 swap(tuple& __in)
481 noexcept(noexcept(__in._M_swap(__in)))
482 { _Inherited::_M_swap(__in); }
483 };
484
485 // Explicit specialization, zero-element tuple.
486 template<>
487 class tuple<>
488 {
489 public:
490 void swap(tuple&) noexcept { /* no-op */ }
491 };
492
493 /// Partial specialization, 2-element tuple.
494 /// Includes construction and assignment from a pair.
495 template<typename _T1, typename _T2>
496 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
497 {
498 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
499
500 public:
501 constexpr tuple()
502 : _Inherited() { }
503
504 explicit
505 constexpr tuple(const _T1& __a1, const _T2& __a2)
506 : _Inherited(__a1, __a2) { }
507
508 template<typename _U1, typename _U2, typename = typename
509 enable_if<__and_<is_convertible<_U1, _T1>,
510 is_convertible<_U2, _T2>>::value>::type>
511 explicit
512 constexpr tuple(_U1&& __a1, _U2&& __a2)
513 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
514
515 constexpr tuple(const tuple&) = default;
516
517 constexpr tuple(tuple&&) = default;
518
519 template<typename _U1, typename _U2, typename = typename
520 enable_if<__and_<is_convertible<const _U1&, _T1>,
521 is_convertible<const _U2&, _T2>>::value>::type>
522 constexpr tuple(const tuple<_U1, _U2>& __in)
523 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
524
525 template<typename _U1, typename _U2, typename = typename
526 enable_if<__and_<is_convertible<_U1, _T1>,
527 is_convertible<_U2, _T2>>::value>::type>
528 constexpr tuple(tuple<_U1, _U2>&& __in)
529 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
530
531 template<typename _U1, typename _U2, typename = typename
532 enable_if<__and_<is_convertible<const _U1&, _T1>,
533 is_convertible<const _U2&, _T2>>::value>::type>
534 constexpr tuple(const pair<_U1, _U2>& __in)
535 : _Inherited(__in.first, __in.second) { }
536
537 template<typename _U1, typename _U2, typename = typename
538 enable_if<__and_<is_convertible<_U1, _T1>,
539 is_convertible<_U2, _T2>>::value>::type>
540 constexpr tuple(pair<_U1, _U2>&& __in)
541 : _Inherited(std::forward<_U1>(__in.first),
542 std::forward<_U2>(__in.second)) { }
543
544 // Allocator-extended constructors.
545
546 template<typename _Alloc>
547 tuple(allocator_arg_t __tag, const _Alloc& __a)
548 : _Inherited(__tag, __a) { }
549
550 template<typename _Alloc>
551 tuple(allocator_arg_t __tag, const _Alloc& __a,
552 const _T1& __a1, const _T2& __a2)
553 : _Inherited(__tag, __a, __a1, __a2) { }
554
555 template<typename _Alloc, typename _U1, typename _U2>
556 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
557 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
558 std::forward<_U2>(__a2)) { }
559
560 template<typename _Alloc>
561 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
562 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
563
564 template<typename _Alloc>
565 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
566 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
567
568 template<typename _Alloc, typename _U1, typename _U2>
569 tuple(allocator_arg_t __tag, const _Alloc& __a,
570 const tuple<_U1, _U2>& __in)
571 : _Inherited(__tag, __a,
572 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
573 { }
574
575 template<typename _Alloc, typename _U1, typename _U2>
576 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
577 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
578 { }
579
580 template<typename _Alloc, typename _U1, typename _U2>
581 tuple(allocator_arg_t __tag, const _Alloc& __a,
582 const pair<_U1, _U2>& __in)
583 : _Inherited(__tag, __a, __in.first, __in.second) { }
584
585 template<typename _Alloc, typename _U1, typename _U2>
586 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
587 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
588 std::forward<_U2>(__in.second)) { }
589
590 tuple&
591 operator=(const tuple& __in)
592 {
593 static_cast<_Inherited&>(*this) = __in;
594 return *this;
595 }
596
597 tuple&
598 operator=(tuple&& __in)
599 noexcept(is_nothrow_move_assignable<_Inherited>::value)
600 {
601 static_cast<_Inherited&>(*this) = std::move(__in);
602 return *this;
603 }
604
605 template<typename _U1, typename _U2>
606 tuple&
607 operator=(const tuple<_U1, _U2>& __in)
608 {
609 static_cast<_Inherited&>(*this) = __in;
610 return *this;
611 }
612
613 template<typename _U1, typename _U2>
614 tuple&
615 operator=(tuple<_U1, _U2>&& __in)
616 {
617 static_cast<_Inherited&>(*this) = std::move(__in);
618 return *this;
619 }
620
621 template<typename _U1, typename _U2>
622 tuple&
623 operator=(const pair<_U1, _U2>& __in)
624 {
625 this->_M_head(*this) = __in.first;
626 this->_M_tail(*this)._M_head(*this) = __in.second;
627 return *this;
628 }
629
630 template<typename _U1, typename _U2>
631 tuple&
632 operator=(pair<_U1, _U2>&& __in)
633 {
634 this->_M_head(*this) = std::forward<_U1>(__in.first);
635 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
636 return *this;
637 }
638
639 void
640 swap(tuple& __in)
641 noexcept(noexcept(__in._M_swap(__in)))
642 { _Inherited::_M_swap(__in); }
643 };
644
645
646 /// Gives the type of the ith element of a given tuple type.
647 template<std::size_t __i, typename _Tp>
648 struct tuple_element;
649
650 /**
651 * Recursive case for tuple_element: strip off the first element in
652 * the tuple and retrieve the (i-1)th element of the remaining tuple.
653 */
654 template<std::size_t __i, typename _Head, typename... _Tail>
655 struct tuple_element<__i, tuple<_Head, _Tail...> >
656 : tuple_element<__i - 1, tuple<_Tail...> > { };
657
658 /**
659 * Basis case for tuple_element: The first element is the one we're seeking.
660 */
661 template<typename _Head, typename... _Tail>
662 struct tuple_element<0, tuple<_Head, _Tail...> >
663 {
664 typedef _Head type;
665 };
666
667 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
668 template<std::size_t __i, typename _Tp>
669 using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
670
671 template<std::size_t __i, typename _Tp>
672 struct tuple_element<__i, const _Tp>
673 {
674 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
675 };
676
677 template<std::size_t __i, typename _Tp>
678 struct tuple_element<__i, volatile _Tp>
679 {
680 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
681 };
682
683 template<std::size_t __i, typename _Tp>
684 struct tuple_element<__i, const volatile _Tp>
685 {
686 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
687 };
688
689 #if __cplusplus > 201103L
690 #define __cpp_lib_tuple_element_t 201402
691
692 template<std::size_t __i, typename _Tp>
693 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
694 #endif
695
696 /// Finds the size of a given tuple type.
697 template<typename _Tp>
698 struct tuple_size;
699
700 // _GLIBCXX_RESOLVE_LIB_DEFECTS
701 // 2313. tuple_size should always derive from integral_constant<size_t, N>
702 template<typename _Tp>
703 struct tuple_size<const _Tp>
704 : integral_constant<size_t, tuple_size<_Tp>::value> { };
705
706 template<typename _Tp>
707 struct tuple_size<volatile _Tp>
708 : integral_constant<size_t, tuple_size<_Tp>::value> { };
709
710 template<typename _Tp>
711 struct tuple_size<const volatile _Tp>
712 : integral_constant<size_t, tuple_size<_Tp>::value> { };
713
714 /// class tuple_size
715 template<typename... _Elements>
716 struct tuple_size<tuple<_Elements...>>
717 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
718
719 template<std::size_t __i, typename _Head, typename... _Tail>
720 constexpr _Head&
721 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
722 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
723
724 template<std::size_t __i, typename _Head, typename... _Tail>
725 constexpr const _Head&
726 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
727 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
728
729 /// Return a reference to the ith element of a tuple.
730 template<std::size_t __i, typename... _Elements>
731 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
732 get(tuple<_Elements...>& __t) noexcept
733 { return std::__get_helper<__i>(__t); }
734
735 /// Return a const reference to the ith element of a const tuple.
736 template<std::size_t __i, typename... _Elements>
737 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
738 get(const tuple<_Elements...>& __t) noexcept
739 { return std::__get_helper<__i>(__t); }
740
741 /// Return an rvalue reference to the ith element of a tuple rvalue.
742 template<std::size_t __i, typename... _Elements>
743 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
744 get(tuple<_Elements...>&& __t) noexcept
745 {
746 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
747 return std::forward<__element_type&&>(std::get<__i>(__t));
748 }
749
750 #if __cplusplus > 201103L
751
752 #define __cpp_lib_tuples_by_type 201304
753
754 template<typename _Head, size_t __i, typename... _Tail>
755 constexpr _Head&
756 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
757 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
758
759 template<typename _Head, size_t __i, typename... _Tail>
760 constexpr const _Head&
761 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
762 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
763
764 /// Return a reference to the unique element of type _Tp of a tuple.
765 template <typename _Tp, typename... _Types>
766 constexpr _Tp&
767 get(tuple<_Types...>& __t) noexcept
768 { return std::__get_helper2<_Tp>(__t); }
769
770 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
771 template <typename _Tp, typename... _Types>
772 constexpr _Tp&&
773 get(tuple<_Types...>&& __t) noexcept
774 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
775
776 /// Return a const reference to the unique element of type _Tp of a tuple.
777 template <typename _Tp, typename... _Types>
778 constexpr const _Tp&
779 get(const tuple<_Types...>& __t) noexcept
780 { return std::__get_helper2<_Tp>(__t); }
781 #endif
782
783 // This class performs the comparison operations on tuples
784 template<typename _Tp, typename _Up, size_t __i, size_t __size>
785 struct __tuple_compare
786 {
787 static constexpr bool
788 __eq(const _Tp& __t, const _Up& __u)
789 {
790 return bool(std::get<__i>(__t) == std::get<__i>(__u))
791 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
792 }
793
794 static constexpr bool
795 __less(const _Tp& __t, const _Up& __u)
796 {
797 return bool(std::get<__i>(__t) < std::get<__i>(__u))
798 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
799 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
800 }
801 };
802
803 template<typename _Tp, typename _Up, size_t __size>
804 struct __tuple_compare<_Tp, _Up, __size, __size>
805 {
806 static constexpr bool
807 __eq(const _Tp&, const _Up&) { return true; }
808
809 static constexpr bool
810 __less(const _Tp&, const _Up&) { return false; }
811 };
812
813 template<typename... _TElements, typename... _UElements>
814 constexpr bool
815 operator==(const tuple<_TElements...>& __t,
816 const tuple<_UElements...>& __u)
817 {
818 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
819 "tuple objects can only be compared if they have equal sizes.");
820 using __compare = __tuple_compare<tuple<_TElements...>,
821 tuple<_UElements...>,
822 0, sizeof...(_TElements)>;
823 return __compare::__eq(__t, __u);
824 }
825
826 template<typename... _TElements, typename... _UElements>
827 constexpr bool
828 operator<(const tuple<_TElements...>& __t,
829 const tuple<_UElements...>& __u)
830 {
831 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
832 "tuple objects can only be compared if they have equal sizes.");
833 using __compare = __tuple_compare<tuple<_TElements...>,
834 tuple<_UElements...>,
835 0, sizeof...(_TElements)>;
836 return __compare::__less(__t, __u);
837 }
838
839 template<typename... _TElements, typename... _UElements>
840 constexpr bool
841 operator!=(const tuple<_TElements...>& __t,
842 const tuple<_UElements...>& __u)
843 { return !(__t == __u); }
844
845 template<typename... _TElements, typename... _UElements>
846 constexpr bool
847 operator>(const tuple<_TElements...>& __t,
848 const tuple<_UElements...>& __u)
849 { return __u < __t; }
850
851 template<typename... _TElements, typename... _UElements>
852 constexpr bool
853 operator<=(const tuple<_TElements...>& __t,
854 const tuple<_UElements...>& __u)
855 { return !(__u < __t); }
856
857 template<typename... _TElements, typename... _UElements>
858 constexpr bool
859 operator>=(const tuple<_TElements...>& __t,
860 const tuple<_UElements...>& __u)
861 { return !(__t < __u); }
862
863 // NB: DR 705.
864 template<typename... _Elements>
865 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
866 make_tuple(_Elements&&... __args)
867 {
868 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
869 __result_type;
870 return __result_type(std::forward<_Elements>(__args)...);
871 }
872
873 template<typename... _Elements>
874 tuple<_Elements&&...>
875 forward_as_tuple(_Elements&&... __args) noexcept
876 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
877
878 template<typename>
879 struct __is_tuple_like_impl : false_type
880 { };
881
882 template<typename... _Tps>
883 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
884 { };
885
886 template<typename _T1, typename _T2>
887 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
888 { };
889
890 template<typename _Tp, std::size_t _Nm>
891 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
892 { };
893
894 // Internal type trait that allows us to sfinae-protect tuple_cat.
895 template<typename _Tp>
896 struct __is_tuple_like
897 : public __is_tuple_like_impl<typename std::remove_cv
898 <typename std::remove_reference<_Tp>::type>::type>::type
899 { };
900
901 template<size_t, typename, typename, size_t>
902 struct __make_tuple_impl;
903
904 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
905 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
906 : __make_tuple_impl<_Idx + 1,
907 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
908 _Tuple, _Nm>
909 { };
910
911 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
912 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
913 {
914 typedef tuple<_Tp...> __type;
915 };
916
917 template<typename _Tuple>
918 struct __do_make_tuple
919 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
920 { };
921
922 // Returns the std::tuple equivalent of a tuple-like type.
923 template<typename _Tuple>
924 struct __make_tuple
925 : public __do_make_tuple<typename std::remove_cv
926 <typename std::remove_reference<_Tuple>::type>::type>
927 { };
928
929 // Combines several std::tuple's into a single one.
930 template<typename...>
931 struct __combine_tuples;
932
933 template<>
934 struct __combine_tuples<>
935 {
936 typedef tuple<> __type;
937 };
938
939 template<typename... _Ts>
940 struct __combine_tuples<tuple<_Ts...>>
941 {
942 typedef tuple<_Ts...> __type;
943 };
944
945 template<typename... _T1s, typename... _T2s, typename... _Rem>
946 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
947 {
948 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
949 _Rem...>::__type __type;
950 };
951
952 // Computes the result type of tuple_cat given a set of tuple-like types.
953 template<typename... _Tpls>
954 struct __tuple_cat_result
955 {
956 typedef typename __combine_tuples
957 <typename __make_tuple<_Tpls>::__type...>::__type __type;
958 };
959
960 // Helper to determine the index set for the first tuple-like
961 // type of a given set.
962 template<typename...>
963 struct __make_1st_indices;
964
965 template<>
966 struct __make_1st_indices<>
967 {
968 typedef std::_Index_tuple<> __type;
969 };
970
971 template<typename _Tp, typename... _Tpls>
972 struct __make_1st_indices<_Tp, _Tpls...>
973 {
974 typedef typename std::_Build_index_tuple<std::tuple_size<
975 typename std::remove_reference<_Tp>::type>::value>::__type __type;
976 };
977
978 // Performs the actual concatenation by step-wise expanding tuple-like
979 // objects into the elements, which are finally forwarded into the
980 // result tuple.
981 template<typename _Ret, typename _Indices, typename... _Tpls>
982 struct __tuple_concater;
983
984 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
985 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
986 {
987 template<typename... _Us>
988 static constexpr _Ret
989 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
990 {
991 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
992 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
993 return __next::_S_do(std::forward<_Tpls>(__tps)...,
994 std::forward<_Us>(__us)...,
995 std::get<_Is>(std::forward<_Tp>(__tp))...);
996 }
997 };
998
999 template<typename _Ret>
1000 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1001 {
1002 template<typename... _Us>
1003 static constexpr _Ret
1004 _S_do(_Us&&... __us)
1005 {
1006 return _Ret(std::forward<_Us>(__us)...);
1007 }
1008 };
1009
1010 /// tuple_cat
1011 template<typename... _Tpls, typename = typename
1012 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1013 constexpr auto
1014 tuple_cat(_Tpls&&... __tpls)
1015 -> typename __tuple_cat_result<_Tpls...>::__type
1016 {
1017 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1018 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1019 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1020 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1021 }
1022
1023 /// tie
1024 template<typename... _Elements>
1025 inline tuple<_Elements&...>
1026 tie(_Elements&... __args) noexcept
1027 { return tuple<_Elements&...>(__args...); }
1028
1029 /// swap
1030 template<typename... _Elements>
1031 inline void
1032 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1033 noexcept(noexcept(__x.swap(__y)))
1034 { __x.swap(__y); }
1035
1036 // A class (and instance) which can be used in 'tie' when an element
1037 // of a tuple is not required
1038 struct _Swallow_assign
1039 {
1040 template<class _Tp>
1041 const _Swallow_assign&
1042 operator=(const _Tp&) const
1043 { return *this; }
1044 };
1045
1046 const _Swallow_assign ignore{};
1047
1048 /// Partial specialization for tuples
1049 template<typename... _Types, typename _Alloc>
1050 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1051
1052 // See stl_pair.h...
1053 template<class _T1, class _T2>
1054 template<typename... _Args1, typename... _Args2>
1055 inline
1056 pair<_T1, _T2>::
1057 pair(piecewise_construct_t,
1058 tuple<_Args1...> __first, tuple<_Args2...> __second)
1059 : pair(__first, __second,
1060 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1061 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1062 { }
1063
1064 template<class _T1, class _T2>
1065 template<typename... _Args1, std::size_t... _Indexes1,
1066 typename... _Args2, std::size_t... _Indexes2>
1067 inline
1068 pair<_T1, _T2>::
1069 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1070 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1071 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1072 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1073 { }
1074
1075 /// @}
1076
1077 _GLIBCXX_END_NAMESPACE_VERSION
1078 } // namespace std
1079
1080 #endif // C++11
1081
1082 #endif // _GLIBCXX_TUPLE