]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/tuple
Avoid creating dangling references in case of nested tuples
[thirdparty/gcc.git] / libstdc++-v3 / include / std / tuple
1 // <tuple> -*- C++ -*-
2
3 // Copyright (C) 2007-2015 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 template<typename _Tp>
162 struct __is_empty_non_tuple : is_empty<_Tp> { };
163
164 // Using EBO for elements that are tuples causes ambiguous base errors.
165 template<typename _El0, typename... _El>
166 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
167
168 // Use the Empty Base-class Optimization for empty, non-final types.
169 template<typename _Tp>
170 using __empty_not_final
171 = typename conditional<__is_final(_Tp), false_type,
172 __is_empty_non_tuple<_Tp>>::type;
173
174 /**
175 * Recursive tuple implementation. Here we store the @c Head element
176 * and derive from a @c Tuple_impl containing the remaining elements
177 * (which contains the @c Tail).
178 */
179 template<std::size_t _Idx, typename _Head, typename... _Tail>
180 struct _Tuple_impl<_Idx, _Head, _Tail...>
181 : public _Tuple_impl<_Idx + 1, _Tail...>,
182 private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
183 {
184 template<std::size_t, typename...> friend class _Tuple_impl;
185
186 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
187 typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
188
189 static constexpr _Head&
190 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
191
192 static constexpr const _Head&
193 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
194
195 static constexpr _Inherited&
196 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
197
198 static constexpr const _Inherited&
199 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
200
201 constexpr _Tuple_impl()
202 : _Inherited(), _Base() { }
203
204 explicit
205 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
206 : _Inherited(__tail...), _Base(__head) { }
207
208 template<typename _UHead, typename... _UTail, typename = typename
209 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
210 explicit
211 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
212 : _Inherited(std::forward<_UTail>(__tail)...),
213 _Base(std::forward<_UHead>(__head)) { }
214
215 constexpr _Tuple_impl(const _Tuple_impl&) = default;
216
217 constexpr
218 _Tuple_impl(_Tuple_impl&& __in)
219 noexcept(__and_<is_nothrow_move_constructible<_Head>,
220 is_nothrow_move_constructible<_Inherited>>::value)
221 : _Inherited(std::move(_M_tail(__in))),
222 _Base(std::forward<_Head>(_M_head(__in))) { }
223
224 template<typename... _UElements>
225 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
226 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
227 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
228
229 template<typename _UHead, typename... _UTails>
230 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
231 : _Inherited(std::move
232 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
233 _Base(std::forward<_UHead>
234 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
235
236 template<typename _Alloc>
237 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
238 : _Inherited(__tag, __a),
239 _Base(__tag, __use_alloc<_Head>(__a)) { }
240
241 template<typename _Alloc>
242 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
243 const _Head& __head, const _Tail&... __tail)
244 : _Inherited(__tag, __a, __tail...),
245 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
246
247 template<typename _Alloc, typename _UHead, typename... _UTail,
248 typename = typename enable_if<sizeof...(_Tail)
249 == sizeof...(_UTail)>::type>
250 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
251 _UHead&& __head, _UTail&&... __tail)
252 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
253 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
254 std::forward<_UHead>(__head)) { }
255
256 template<typename _Alloc>
257 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
258 const _Tuple_impl& __in)
259 : _Inherited(__tag, __a, _M_tail(__in)),
260 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
261
262 template<typename _Alloc>
263 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
264 _Tuple_impl&& __in)
265 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
266 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
267 std::forward<_Head>(_M_head(__in))) { }
268
269 template<typename _Alloc, typename... _UElements>
270 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
271 const _Tuple_impl<_Idx, _UElements...>& __in)
272 : _Inherited(__tag, __a,
273 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
274 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
275 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
276
277 template<typename _Alloc, typename _UHead, typename... _UTails>
278 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
279 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
280 : _Inherited(__tag, __a, std::move
281 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
282 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
283 std::forward<_UHead>
284 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
285
286 _Tuple_impl&
287 operator=(const _Tuple_impl& __in)
288 {
289 _M_head(*this) = _M_head(__in);
290 _M_tail(*this) = _M_tail(__in);
291 return *this;
292 }
293
294 _Tuple_impl&
295 operator=(_Tuple_impl&& __in)
296 noexcept(__and_<is_nothrow_move_assignable<_Head>,
297 is_nothrow_move_assignable<_Inherited>>::value)
298 {
299 _M_head(*this) = std::forward<_Head>(_M_head(__in));
300 _M_tail(*this) = std::move(_M_tail(__in));
301 return *this;
302 }
303
304 template<typename... _UElements>
305 _Tuple_impl&
306 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
307 {
308 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
309 _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
310 return *this;
311 }
312
313 template<typename _UHead, typename... _UTails>
314 _Tuple_impl&
315 operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
316 {
317 _M_head(*this) = std::forward<_UHead>
318 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
319 _M_tail(*this) = std::move
320 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
321 return *this;
322 }
323
324 protected:
325 void
326 _M_swap(_Tuple_impl& __in)
327 noexcept(__is_nothrow_swappable<_Head>::value
328 && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
329 {
330 using std::swap;
331 swap(_M_head(*this), _M_head(__in));
332 _Inherited::_M_swap(_M_tail(__in));
333 }
334 };
335
336 // Basis case of inheritance recursion.
337 template<std::size_t _Idx, typename _Head>
338 struct _Tuple_impl<_Idx, _Head>
339 : private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
340 {
341 template<std::size_t, typename...> friend class _Tuple_impl;
342
343 typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
344
345 static constexpr _Head&
346 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
347
348 static constexpr const _Head&
349 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
350
351 constexpr _Tuple_impl()
352 : _Base() { }
353
354 explicit
355 constexpr _Tuple_impl(const _Head& __head)
356 : _Base(__head) { }
357
358 template<typename _UHead>
359 explicit
360 constexpr _Tuple_impl(_UHead&& __head)
361 : _Base(std::forward<_UHead>(__head)) { }
362
363 constexpr _Tuple_impl(const _Tuple_impl&) = default;
364
365 constexpr
366 _Tuple_impl(_Tuple_impl&& __in)
367 noexcept(is_nothrow_move_constructible<_Head>::value)
368 : _Base(std::forward<_Head>(_M_head(__in))) { }
369
370 template<typename _UHead>
371 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
372 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
373
374 template<typename _UHead>
375 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
376 : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
377 { }
378
379 template<typename _Alloc>
380 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
381 : _Base(__tag, __use_alloc<_Head>(__a)) { }
382
383 template<typename _Alloc>
384 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
385 const _Head& __head)
386 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
387
388 template<typename _Alloc, typename _UHead>
389 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
390 _UHead&& __head)
391 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
392 std::forward<_UHead>(__head)) { }
393
394 template<typename _Alloc>
395 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
396 const _Tuple_impl& __in)
397 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
398
399 template<typename _Alloc>
400 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
401 _Tuple_impl&& __in)
402 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
403 std::forward<_Head>(_M_head(__in))) { }
404
405 template<typename _Alloc, typename _UHead>
406 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
407 const _Tuple_impl<_Idx, _UHead>& __in)
408 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
409 _Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
410
411 template<typename _Alloc, typename _UHead>
412 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
413 _Tuple_impl<_Idx, _UHead>&& __in)
414 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
415 std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
416 { }
417
418 _Tuple_impl&
419 operator=(const _Tuple_impl& __in)
420 {
421 _M_head(*this) = _M_head(__in);
422 return *this;
423 }
424
425 _Tuple_impl&
426 operator=(_Tuple_impl&& __in)
427 noexcept(is_nothrow_move_assignable<_Head>::value)
428 {
429 _M_head(*this) = std::forward<_Head>(_M_head(__in));
430 return *this;
431 }
432
433 template<typename _UHead>
434 _Tuple_impl&
435 operator=(const _Tuple_impl<_Idx, _UHead>& __in)
436 {
437 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
438 return *this;
439 }
440
441 template<typename _UHead>
442 _Tuple_impl&
443 operator=(_Tuple_impl<_Idx, _UHead>&& __in)
444 {
445 _M_head(*this)
446 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
447 return *this;
448 }
449
450 protected:
451 void
452 _M_swap(_Tuple_impl& __in)
453 noexcept(__is_nothrow_swappable<_Head>::value)
454 {
455 using std::swap;
456 swap(_M_head(*this), _M_head(__in));
457 }
458 };
459
460
461 // Concept utility functions, reused in conditionally-explicit
462 // constructors.
463 template<bool, typename... _Elements>
464 struct _TC
465 {
466 template<typename... _UElements>
467 static constexpr bool _ConstructibleTuple()
468 {
469 return __and_<is_constructible<_Elements, const _UElements&>...>::value;
470 }
471
472 template<typename... _UElements>
473 static constexpr bool _ImplicitlyConvertibleTuple()
474 {
475 return __and_<is_convertible<const _UElements&, _Elements>...>::value;
476 }
477
478 template<typename... _UElements>
479 static constexpr bool _MoveConstructibleTuple()
480 {
481 return __and_<is_constructible<_Elements, _UElements&&>...>::value;
482 }
483
484 template<typename... _UElements>
485 static constexpr bool _ImplicitlyMoveConvertibleTuple()
486 {
487 return __and_<is_convertible<_UElements&&, _Elements>...>::value;
488 }
489
490 template<typename _SrcTuple>
491 static constexpr bool _NonNestedTuple()
492 {
493 return __and_<__not_<is_convertible<_SrcTuple, _Elements...>>,
494 __not_<is_constructible<_Elements..., _SrcTuple>>
495 >::value;
496 }
497 };
498
499 template<typename... _Elements>
500 struct _TC<false, _Elements...>
501 {
502 template<typename... _UElements>
503 static constexpr bool _ConstructibleTuple()
504 {
505 return false;
506 }
507
508 template<typename... _UElements>
509 static constexpr bool _ImplicitlyConvertibleTuple()
510 {
511 return false;
512 }
513
514 template<typename... _UElements>
515 static constexpr bool _MoveConstructibleTuple()
516 {
517 return false;
518 }
519
520 template<typename... _UElements>
521 static constexpr bool _ImplicitlyMoveConvertibleTuple()
522 {
523 return false;
524 }
525
526 template<typename... _UElements>
527 static constexpr bool _NonNestedTuple()
528 {
529 return true;
530 }
531 };
532
533 /// Primary class template, tuple
534 template<typename... _Elements>
535 class tuple : public _Tuple_impl<0, _Elements...>
536 {
537 typedef _Tuple_impl<0, _Elements...> _Inherited;
538
539 // Used for constraining the default constructor so
540 // that it becomes dependent on the constraints.
541 template<typename _Dummy>
542 struct _TC2
543 {
544 static constexpr bool _DefaultConstructibleTuple()
545 {
546 return __and_<is_default_constructible<_Elements>...>::value;
547 }
548 };
549
550 public:
551 template<typename _Dummy = void,
552 typename enable_if<_TC2<_Dummy>::
553 _DefaultConstructibleTuple(),
554 bool>::type = true>
555 constexpr tuple()
556 : _Inherited() { }
557
558 // Shortcut for the cases where constructors taking _Elements...
559 // need to be constrained.
560 template<typename _Dummy> using _TCC =
561 _TC<is_same<_Dummy, void>::value,
562 _Elements...>;
563
564 template<typename _Dummy = void,
565 typename enable_if<
566 _TCC<_Dummy>::template
567 _ConstructibleTuple<_Elements...>()
568 && _TCC<_Dummy>::template
569 _ImplicitlyConvertibleTuple<_Elements...>()
570 && (sizeof...(_Elements) >= 1),
571 bool>::type=true>
572 constexpr tuple(const _Elements&... __elements)
573 : _Inherited(__elements...) { }
574
575 template<typename _Dummy = void,
576 typename enable_if<
577 _TCC<_Dummy>::template
578 _ConstructibleTuple<_Elements...>()
579 && !_TCC<_Dummy>::template
580 _ImplicitlyConvertibleTuple<_Elements...>()
581 && (sizeof...(_Elements) >= 1),
582 bool>::type=false>
583 explicit constexpr tuple(const _Elements&... __elements)
584 : _Inherited(__elements...) { }
585
586 // Shortcut for the cases where constructors taking _UElements...
587 // need to be constrained.
588 template<typename... _UElements> using _TMC =
589 _TC<(sizeof...(_Elements) == sizeof...(_UElements)),
590 _Elements...>;
591
592 template<typename... _UElements, typename
593 enable_if<_TMC<_UElements...>::template
594 _MoveConstructibleTuple<_UElements...>()
595 && _TMC<_UElements...>::template
596 _ImplicitlyMoveConvertibleTuple<_UElements...>()
597 && (sizeof...(_Elements) >= 1),
598 bool>::type=true>
599 constexpr tuple(_UElements&&... __elements)
600 : _Inherited(std::forward<_UElements>(__elements)...) { }
601
602 template<typename... _UElements, typename
603 enable_if<_TMC<_UElements...>::template
604 _MoveConstructibleTuple<_UElements...>()
605 && !_TMC<_UElements...>::template
606 _ImplicitlyMoveConvertibleTuple<_UElements...>()
607 && (sizeof...(_Elements) >= 1),
608 bool>::type=false>
609 explicit constexpr tuple(_UElements&&... __elements)
610 : _Inherited(std::forward<_UElements>(__elements)...) { }
611
612 constexpr tuple(const tuple&) = default;
613
614 constexpr tuple(tuple&&) = default;
615
616 // Shortcut for the cases where constructors taking tuples
617 // must avoid creating temporaries.
618 template<typename _Dummy> using _TNTC =
619 _TC<is_same<_Dummy, void>::value && sizeof...(_Elements) == 1,
620 _Elements...>;
621
622 template<typename... _UElements, typename _Dummy = void, typename
623 enable_if<_TMC<_UElements...>::template
624 _ConstructibleTuple<_UElements...>()
625 && _TMC<_UElements...>::template
626 _ImplicitlyConvertibleTuple<_UElements...>()
627 && _TNTC<_Dummy>::template
628 _NonNestedTuple<const tuple<_UElements...>&>(),
629 bool>::type=true>
630 constexpr tuple(const tuple<_UElements...>& __in)
631 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
632 { }
633
634 template<typename... _UElements, typename _Dummy = void, typename
635 enable_if<_TMC<_UElements...>::template
636 _ConstructibleTuple<_UElements...>()
637 && !_TMC<_UElements...>::template
638 _ImplicitlyConvertibleTuple<_UElements...>()
639 && _TNTC<_Dummy>::template
640 _NonNestedTuple<const tuple<_UElements...>&>(),
641 bool>::type=false>
642 explicit constexpr tuple(const tuple<_UElements...>& __in)
643 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
644 { }
645
646 template<typename... _UElements, typename _Dummy = void, typename
647 enable_if<_TMC<_UElements...>::template
648 _MoveConstructibleTuple<_UElements...>()
649 && _TMC<_UElements...>::template
650 _ImplicitlyMoveConvertibleTuple<_UElements...>()
651 && _TNTC<_Dummy>::template
652 _NonNestedTuple<tuple<_UElements...>&&>(),
653 bool>::type=true>
654 constexpr tuple(tuple<_UElements...>&& __in)
655 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
656
657 template<typename... _UElements, typename _Dummy = void, typename
658 enable_if<_TMC<_UElements...>::template
659 _MoveConstructibleTuple<_UElements...>()
660 && !_TMC<_UElements...>::template
661 _ImplicitlyMoveConvertibleTuple<_UElements...>()
662 && _TNTC<_Dummy>::template
663 _NonNestedTuple<tuple<_UElements...>&&>(),
664 bool>::type=false>
665 explicit constexpr tuple(tuple<_UElements...>&& __in)
666 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
667
668 // Allocator-extended constructors.
669
670 template<typename _Alloc>
671 tuple(allocator_arg_t __tag, const _Alloc& __a)
672 : _Inherited(__tag, __a) { }
673
674 template<typename _Alloc, typename _Dummy = void,
675 typename enable_if<
676 _TCC<_Dummy>::template
677 _ConstructibleTuple<_Elements...>()
678 && _TCC<_Dummy>::template
679 _ImplicitlyConvertibleTuple<_Elements...>(),
680 bool>::type=true>
681 tuple(allocator_arg_t __tag, const _Alloc& __a,
682 const _Elements&... __elements)
683 : _Inherited(__tag, __a, __elements...) { }
684
685 template<typename _Alloc, typename _Dummy = void,
686 typename enable_if<
687 _TCC<_Dummy>::template
688 _ConstructibleTuple<_Elements...>()
689 && !_TCC<_Dummy>::template
690 _ImplicitlyConvertibleTuple<_Elements...>(),
691 bool>::type=false>
692 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
693 const _Elements&... __elements)
694 : _Inherited(__tag, __a, __elements...) { }
695
696 template<typename _Alloc, typename... _UElements, typename
697 enable_if<_TMC<_UElements...>::template
698 _MoveConstructibleTuple<_UElements...>()
699 && _TMC<_UElements...>::template
700 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
701 bool>::type=true>
702 tuple(allocator_arg_t __tag, const _Alloc& __a,
703 _UElements&&... __elements)
704 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
705 { }
706
707 template<typename _Alloc, typename... _UElements, typename
708 enable_if<_TMC<_UElements...>::template
709 _MoveConstructibleTuple<_UElements...>()
710 && !_TMC<_UElements...>::template
711 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
712 bool>::type=false>
713 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
714 _UElements&&... __elements)
715 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
716 { }
717
718 template<typename _Alloc>
719 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
720 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
721
722 template<typename _Alloc>
723 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
724 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
725
726 template<typename _Alloc, typename... _UElements, typename
727 enable_if<_TMC<_UElements...>::template
728 _ConstructibleTuple<_UElements...>()
729 && _TMC<_UElements...>::template
730 _ImplicitlyConvertibleTuple<_UElements...>(),
731 bool>::type=true>
732 tuple(allocator_arg_t __tag, const _Alloc& __a,
733 const tuple<_UElements...>& __in)
734 : _Inherited(__tag, __a,
735 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
736 { }
737
738 template<typename _Alloc, typename... _UElements, typename
739 enable_if<_TMC<_UElements...>::template
740 _ConstructibleTuple<_UElements...>()
741 && !_TMC<_UElements...>::template
742 _ImplicitlyConvertibleTuple<_UElements...>(),
743 bool>::type=false>
744 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
745 const tuple<_UElements...>& __in)
746 : _Inherited(__tag, __a,
747 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
748 { }
749
750 template<typename _Alloc, typename... _UElements, typename
751 enable_if<_TMC<_UElements...>::template
752 _MoveConstructibleTuple<_UElements...>()
753 && _TMC<_UElements...>::template
754 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
755 bool>::type=true>
756 tuple(allocator_arg_t __tag, const _Alloc& __a,
757 tuple<_UElements...>&& __in)
758 : _Inherited(__tag, __a,
759 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
760 { }
761
762 template<typename _Alloc, typename... _UElements, typename
763 enable_if<_TMC<_UElements...>::template
764 _MoveConstructibleTuple<_UElements...>()
765 && !_TMC<_UElements...>::template
766 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
767 bool>::type=false>
768 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
769 tuple<_UElements...>&& __in)
770 : _Inherited(__tag, __a,
771 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
772 { }
773
774 tuple&
775 operator=(const tuple& __in)
776 {
777 static_cast<_Inherited&>(*this) = __in;
778 return *this;
779 }
780
781 tuple&
782 operator=(tuple&& __in)
783 noexcept(is_nothrow_move_assignable<_Inherited>::value)
784 {
785 static_cast<_Inherited&>(*this) = std::move(__in);
786 return *this;
787 }
788
789 template<typename... _UElements, typename = typename
790 enable_if<sizeof...(_UElements)
791 == sizeof...(_Elements)>::type>
792 tuple&
793 operator=(const tuple<_UElements...>& __in)
794 {
795 static_cast<_Inherited&>(*this) = __in;
796 return *this;
797 }
798
799 template<typename... _UElements, typename = typename
800 enable_if<sizeof...(_UElements)
801 == sizeof...(_Elements)>::type>
802 tuple&
803 operator=(tuple<_UElements...>&& __in)
804 {
805 static_cast<_Inherited&>(*this) = std::move(__in);
806 return *this;
807 }
808
809 void
810 swap(tuple& __in)
811 noexcept(noexcept(__in._M_swap(__in)))
812 { _Inherited::_M_swap(__in); }
813 };
814
815 // Explicit specialization, zero-element tuple.
816 template<>
817 class tuple<>
818 {
819 public:
820 void swap(tuple&) noexcept { /* no-op */ }
821 };
822
823 /// Partial specialization, 2-element tuple.
824 /// Includes construction and assignment from a pair.
825 template<typename _T1, typename _T2>
826 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
827 {
828 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
829
830 public:
831 template <typename _U1 = _T1,
832 typename _U2 = _T2,
833 typename enable_if<__and_<
834 is_default_constructible<_U1>,
835 is_default_constructible<_U2>>
836 ::value, bool>::type = true>
837
838 constexpr tuple()
839 : _Inherited() { }
840
841 // Shortcut for the cases where constructors taking _T1, _T2
842 // need to be constrained.
843 template<typename _Dummy> using _TCC =
844 _TC<is_same<_Dummy, void>::value, _T1, _T2>;
845
846 template<typename _Dummy = void, typename
847 enable_if<_TCC<_Dummy>::template
848 _ConstructibleTuple<_T1, _T2>()
849 && _TCC<_Dummy>::template
850 _ImplicitlyConvertibleTuple<_T1, _T2>(),
851 bool>::type = true>
852 constexpr tuple(const _T1& __a1, const _T2& __a2)
853 : _Inherited(__a1, __a2) { }
854
855 template<typename _Dummy = void, typename
856 enable_if<_TCC<_Dummy>::template
857 _ConstructibleTuple<_T1, _T2>()
858 && !_TCC<_Dummy>::template
859 _ImplicitlyConvertibleTuple<_T1, _T2>(),
860 bool>::type = false>
861 explicit constexpr tuple(const _T1& __a1, const _T2& __a2)
862 : _Inherited(__a1, __a2) { }
863
864 // Shortcut for the cases where constructors taking _U1, _U2
865 // need to be constrained.
866 using _TMC = _TC<true, _T1, _T2>;
867
868 template<typename _U1, typename _U2, typename
869 enable_if<_TMC::template
870 _MoveConstructibleTuple<_U1, _U2>()
871 && _TMC::template
872 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
873 bool>::type = true>
874 constexpr tuple(_U1&& __a1, _U2&& __a2)
875 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
876
877 template<typename _U1, typename _U2, typename
878 enable_if<_TMC::template
879 _MoveConstructibleTuple<_U1, _U2>()
880 && !_TMC::template
881 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
882 bool>::type = false>
883 explicit constexpr tuple(_U1&& __a1, _U2&& __a2)
884 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
885
886 constexpr tuple(const tuple&) = default;
887
888 constexpr tuple(tuple&&) = default;
889
890 template<typename _U1, typename _U2, typename
891 enable_if<_TMC::template
892 _ConstructibleTuple<_U1, _U2>()
893 && _TMC::template
894 _ImplicitlyConvertibleTuple<_U1, _U2>(),
895 bool>::type = true>
896 constexpr tuple(const tuple<_U1, _U2>& __in)
897 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
898
899 template<typename _U1, typename _U2, typename
900 enable_if<_TMC::template
901 _ConstructibleTuple<_U1, _U2>()
902 && !_TMC::template
903 _ImplicitlyConvertibleTuple<_U1, _U2>(),
904 bool>::type = false>
905 explicit constexpr tuple(const tuple<_U1, _U2>& __in)
906 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
907
908 template<typename _U1, typename _U2, typename
909 enable_if<_TMC::template
910 _MoveConstructibleTuple<_U1, _U2>()
911 && _TMC::template
912 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
913 bool>::type = true>
914 constexpr tuple(tuple<_U1, _U2>&& __in)
915 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
916
917 template<typename _U1, typename _U2, typename
918 enable_if<_TMC::template
919 _MoveConstructibleTuple<_U1, _U2>()
920 && !_TMC::template
921 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
922 bool>::type = false>
923 explicit constexpr tuple(tuple<_U1, _U2>&& __in)
924 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
925
926 template<typename _U1, typename _U2, typename
927 enable_if<_TMC::template
928 _ConstructibleTuple<_U1, _U2>()
929 && _TMC::template
930 _ImplicitlyConvertibleTuple<_U1, _U2>(),
931 bool>::type = true>
932 constexpr tuple(const pair<_U1, _U2>& __in)
933 : _Inherited(__in.first, __in.second) { }
934
935 template<typename _U1, typename _U2, typename
936 enable_if<_TMC::template
937 _ConstructibleTuple<_U1, _U2>()
938 && !_TMC::template
939 _ImplicitlyConvertibleTuple<_U1, _U2>(),
940 bool>::type = false>
941 explicit constexpr tuple(const pair<_U1, _U2>& __in)
942 : _Inherited(__in.first, __in.second) { }
943
944 template<typename _U1, typename _U2, typename
945 enable_if<_TMC::template
946 _MoveConstructibleTuple<_U1, _U2>()
947 && _TMC::template
948 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
949 bool>::type = true>
950 constexpr tuple(pair<_U1, _U2>&& __in)
951 : _Inherited(std::forward<_U1>(__in.first),
952 std::forward<_U2>(__in.second)) { }
953
954 template<typename _U1, typename _U2, typename
955 enable_if<_TMC::template
956 _MoveConstructibleTuple<_U1, _U2>()
957 && !_TMC::template
958 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
959 bool>::type = false>
960 explicit constexpr tuple(pair<_U1, _U2>&& __in)
961 : _Inherited(std::forward<_U1>(__in.first),
962 std::forward<_U2>(__in.second)) { }
963
964 // Allocator-extended constructors.
965
966 template<typename _Alloc>
967 tuple(allocator_arg_t __tag, const _Alloc& __a)
968 : _Inherited(__tag, __a) { }
969
970 template<typename _Alloc, typename _Dummy = void,
971 typename enable_if<
972 _TCC<_Dummy>::template
973 _ConstructibleTuple<_T1, _T2>()
974 && _TCC<_Dummy>::template
975 _ImplicitlyConvertibleTuple<_T1, _T2>(),
976 bool>::type=true>
977
978 tuple(allocator_arg_t __tag, const _Alloc& __a,
979 const _T1& __a1, const _T2& __a2)
980 : _Inherited(__tag, __a, __a1, __a2) { }
981
982 template<typename _Alloc, typename _Dummy = void,
983 typename enable_if<
984 _TCC<_Dummy>::template
985 _ConstructibleTuple<_T1, _T2>()
986 && !_TCC<_Dummy>::template
987 _ImplicitlyConvertibleTuple<_T1, _T2>(),
988 bool>::type=false>
989
990 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
991 const _T1& __a1, const _T2& __a2)
992 : _Inherited(__tag, __a, __a1, __a2) { }
993
994 template<typename _Alloc, typename _U1, typename _U2, typename
995 enable_if<_TMC::template
996 _MoveConstructibleTuple<_U1, _U2>()
997 && _TMC::template
998 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
999 bool>::type = true>
1000 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
1001 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1002 std::forward<_U2>(__a2)) { }
1003
1004 template<typename _Alloc, typename _U1, typename _U2, typename
1005 enable_if<_TMC::template
1006 _MoveConstructibleTuple<_U1, _U2>()
1007 && !_TMC::template
1008 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1009 bool>::type = false>
1010 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1011 _U1&& __a1, _U2&& __a2)
1012 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1013 std::forward<_U2>(__a2)) { }
1014
1015 template<typename _Alloc>
1016 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
1017 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
1018
1019 template<typename _Alloc>
1020 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
1021 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
1022
1023 template<typename _Alloc, typename _U1, typename _U2, typename
1024 enable_if<_TMC::template
1025 _ConstructibleTuple<_U1, _U2>()
1026 && _TMC::template
1027 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1028 bool>::type = true>
1029 tuple(allocator_arg_t __tag, const _Alloc& __a,
1030 const tuple<_U1, _U2>& __in)
1031 : _Inherited(__tag, __a,
1032 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1033 { }
1034
1035 template<typename _Alloc, typename _U1, typename _U2, typename
1036 enable_if<_TMC::template
1037 _ConstructibleTuple<_U1, _U2>()
1038 && !_TMC::template
1039 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1040 bool>::type = false>
1041 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1042 const tuple<_U1, _U2>& __in)
1043 : _Inherited(__tag, __a,
1044 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1045 { }
1046
1047 template<typename _Alloc, typename _U1, typename _U2, typename
1048 enable_if<_TMC::template
1049 _MoveConstructibleTuple<_U1, _U2>()
1050 && _TMC::template
1051 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1052 bool>::type = true>
1053 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1054 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1055 { }
1056
1057 template<typename _Alloc, typename _U1, typename _U2, typename
1058 enable_if<_TMC::template
1059 _MoveConstructibleTuple<_U1, _U2>()
1060 && !_TMC::template
1061 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1062 bool>::type = false>
1063 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1064 tuple<_U1, _U2>&& __in)
1065 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1066 { }
1067
1068 template<typename _Alloc, typename _U1, typename _U2, typename
1069 enable_if<_TMC::template
1070 _ConstructibleTuple<_U1, _U2>()
1071 && _TMC::template
1072 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1073 bool>::type = true>
1074 tuple(allocator_arg_t __tag, const _Alloc& __a,
1075 const pair<_U1, _U2>& __in)
1076 : _Inherited(__tag, __a, __in.first, __in.second) { }
1077
1078 template<typename _Alloc, typename _U1, typename _U2, typename
1079 enable_if<_TMC::template
1080 _ConstructibleTuple<_U1, _U2>()
1081 && !_TMC::template
1082 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1083 bool>::type = false>
1084 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1085 const pair<_U1, _U2>& __in)
1086 : _Inherited(__tag, __a, __in.first, __in.second) { }
1087
1088 template<typename _Alloc, typename _U1, typename _U2, typename
1089 enable_if<_TMC::template
1090 _MoveConstructibleTuple<_U1, _U2>()
1091 && _TMC::template
1092 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1093 bool>::type = true>
1094 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
1095 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1096 std::forward<_U2>(__in.second)) { }
1097
1098 template<typename _Alloc, typename _U1, typename _U2, typename
1099 enable_if<_TMC::template
1100 _MoveConstructibleTuple<_U1, _U2>()
1101 && !_TMC::template
1102 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1103 bool>::type = false>
1104 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1105 pair<_U1, _U2>&& __in)
1106 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1107 std::forward<_U2>(__in.second)) { }
1108
1109 tuple&
1110 operator=(const tuple& __in)
1111 {
1112 static_cast<_Inherited&>(*this) = __in;
1113 return *this;
1114 }
1115
1116 tuple&
1117 operator=(tuple&& __in)
1118 noexcept(is_nothrow_move_assignable<_Inherited>::value)
1119 {
1120 static_cast<_Inherited&>(*this) = std::move(__in);
1121 return *this;
1122 }
1123
1124 template<typename _U1, typename _U2>
1125 tuple&
1126 operator=(const tuple<_U1, _U2>& __in)
1127 {
1128 static_cast<_Inherited&>(*this) = __in;
1129 return *this;
1130 }
1131
1132 template<typename _U1, typename _U2>
1133 tuple&
1134 operator=(tuple<_U1, _U2>&& __in)
1135 {
1136 static_cast<_Inherited&>(*this) = std::move(__in);
1137 return *this;
1138 }
1139
1140 template<typename _U1, typename _U2>
1141 tuple&
1142 operator=(const pair<_U1, _U2>& __in)
1143 {
1144 this->_M_head(*this) = __in.first;
1145 this->_M_tail(*this)._M_head(*this) = __in.second;
1146 return *this;
1147 }
1148
1149 template<typename _U1, typename _U2>
1150 tuple&
1151 operator=(pair<_U1, _U2>&& __in)
1152 {
1153 this->_M_head(*this) = std::forward<_U1>(__in.first);
1154 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
1155 return *this;
1156 }
1157
1158 void
1159 swap(tuple& __in)
1160 noexcept(noexcept(__in._M_swap(__in)))
1161 { _Inherited::_M_swap(__in); }
1162 };
1163
1164
1165 /// Gives the type of the ith element of a given tuple type.
1166 template<std::size_t __i, typename _Tp>
1167 struct tuple_element;
1168
1169 /**
1170 * Recursive case for tuple_element: strip off the first element in
1171 * the tuple and retrieve the (i-1)th element of the remaining tuple.
1172 */
1173 template<std::size_t __i, typename _Head, typename... _Tail>
1174 struct tuple_element<__i, tuple<_Head, _Tail...> >
1175 : tuple_element<__i - 1, tuple<_Tail...> > { };
1176
1177 /**
1178 * Basis case for tuple_element: The first element is the one we're seeking.
1179 */
1180 template<typename _Head, typename... _Tail>
1181 struct tuple_element<0, tuple<_Head, _Tail...> >
1182 {
1183 typedef _Head type;
1184 };
1185
1186 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
1187 template<std::size_t __i, typename _Tp>
1188 using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
1189
1190 template<std::size_t __i, typename _Tp>
1191 struct tuple_element<__i, const _Tp>
1192 {
1193 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
1194 };
1195
1196 template<std::size_t __i, typename _Tp>
1197 struct tuple_element<__i, volatile _Tp>
1198 {
1199 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
1200 };
1201
1202 template<std::size_t __i, typename _Tp>
1203 struct tuple_element<__i, const volatile _Tp>
1204 {
1205 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
1206 };
1207
1208 #if __cplusplus > 201103L
1209 #define __cpp_lib_tuple_element_t 201402
1210
1211 template<std::size_t __i, typename _Tp>
1212 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
1213 #endif
1214
1215 /// Finds the size of a given tuple type.
1216 template<typename _Tp>
1217 struct tuple_size;
1218
1219 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1220 // 2313. tuple_size should always derive from integral_constant<size_t, N>
1221 template<typename _Tp>
1222 struct tuple_size<const _Tp>
1223 : integral_constant<size_t, tuple_size<_Tp>::value> { };
1224
1225 template<typename _Tp>
1226 struct tuple_size<volatile _Tp>
1227 : integral_constant<size_t, tuple_size<_Tp>::value> { };
1228
1229 template<typename _Tp>
1230 struct tuple_size<const volatile _Tp>
1231 : integral_constant<size_t, tuple_size<_Tp>::value> { };
1232
1233 /// class tuple_size
1234 template<typename... _Elements>
1235 struct tuple_size<tuple<_Elements...>>
1236 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
1237
1238 template<std::size_t __i, typename _Head, typename... _Tail>
1239 constexpr _Head&
1240 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1241 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1242
1243 template<std::size_t __i, typename _Head, typename... _Tail>
1244 constexpr const _Head&
1245 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1246 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1247
1248 /// Return a reference to the ith element of a tuple.
1249 template<std::size_t __i, typename... _Elements>
1250 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
1251 get(tuple<_Elements...>& __t) noexcept
1252 { return std::__get_helper<__i>(__t); }
1253
1254 /// Return a const reference to the ith element of a const tuple.
1255 template<std::size_t __i, typename... _Elements>
1256 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
1257 get(const tuple<_Elements...>& __t) noexcept
1258 { return std::__get_helper<__i>(__t); }
1259
1260 /// Return an rvalue reference to the ith element of a tuple rvalue.
1261 template<std::size_t __i, typename... _Elements>
1262 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
1263 get(tuple<_Elements...>&& __t) noexcept
1264 {
1265 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1266 return std::forward<__element_type&&>(std::get<__i>(__t));
1267 }
1268
1269 #if __cplusplus > 201103L
1270
1271 #define __cpp_lib_tuples_by_type 201304
1272
1273 template<typename _Head, size_t __i, typename... _Tail>
1274 constexpr _Head&
1275 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1276 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1277
1278 template<typename _Head, size_t __i, typename... _Tail>
1279 constexpr const _Head&
1280 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1281 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1282
1283 /// Return a reference to the unique element of type _Tp of a tuple.
1284 template <typename _Tp, typename... _Types>
1285 constexpr _Tp&
1286 get(tuple<_Types...>& __t) noexcept
1287 { return std::__get_helper2<_Tp>(__t); }
1288
1289 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
1290 template <typename _Tp, typename... _Types>
1291 constexpr _Tp&&
1292 get(tuple<_Types...>&& __t) noexcept
1293 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
1294
1295 /// Return a const reference to the unique element of type _Tp of a tuple.
1296 template <typename _Tp, typename... _Types>
1297 constexpr const _Tp&
1298 get(const tuple<_Types...>& __t) noexcept
1299 { return std::__get_helper2<_Tp>(__t); }
1300 #endif
1301
1302 // This class performs the comparison operations on tuples
1303 template<typename _Tp, typename _Up, size_t __i, size_t __size>
1304 struct __tuple_compare
1305 {
1306 static constexpr bool
1307 __eq(const _Tp& __t, const _Up& __u)
1308 {
1309 return bool(std::get<__i>(__t) == std::get<__i>(__u))
1310 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
1311 }
1312
1313 static constexpr bool
1314 __less(const _Tp& __t, const _Up& __u)
1315 {
1316 return bool(std::get<__i>(__t) < std::get<__i>(__u))
1317 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
1318 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
1319 }
1320 };
1321
1322 template<typename _Tp, typename _Up, size_t __size>
1323 struct __tuple_compare<_Tp, _Up, __size, __size>
1324 {
1325 static constexpr bool
1326 __eq(const _Tp&, const _Up&) { return true; }
1327
1328 static constexpr bool
1329 __less(const _Tp&, const _Up&) { return false; }
1330 };
1331
1332 template<typename... _TElements, typename... _UElements>
1333 constexpr bool
1334 operator==(const tuple<_TElements...>& __t,
1335 const tuple<_UElements...>& __u)
1336 {
1337 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1338 "tuple objects can only be compared if they have equal sizes.");
1339 using __compare = __tuple_compare<tuple<_TElements...>,
1340 tuple<_UElements...>,
1341 0, sizeof...(_TElements)>;
1342 return __compare::__eq(__t, __u);
1343 }
1344
1345 template<typename... _TElements, typename... _UElements>
1346 constexpr bool
1347 operator<(const tuple<_TElements...>& __t,
1348 const tuple<_UElements...>& __u)
1349 {
1350 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1351 "tuple objects can only be compared if they have equal sizes.");
1352 using __compare = __tuple_compare<tuple<_TElements...>,
1353 tuple<_UElements...>,
1354 0, sizeof...(_TElements)>;
1355 return __compare::__less(__t, __u);
1356 }
1357
1358 template<typename... _TElements, typename... _UElements>
1359 constexpr bool
1360 operator!=(const tuple<_TElements...>& __t,
1361 const tuple<_UElements...>& __u)
1362 { return !(__t == __u); }
1363
1364 template<typename... _TElements, typename... _UElements>
1365 constexpr bool
1366 operator>(const tuple<_TElements...>& __t,
1367 const tuple<_UElements...>& __u)
1368 { return __u < __t; }
1369
1370 template<typename... _TElements, typename... _UElements>
1371 constexpr bool
1372 operator<=(const tuple<_TElements...>& __t,
1373 const tuple<_UElements...>& __u)
1374 { return !(__u < __t); }
1375
1376 template<typename... _TElements, typename... _UElements>
1377 constexpr bool
1378 operator>=(const tuple<_TElements...>& __t,
1379 const tuple<_UElements...>& __u)
1380 { return !(__t < __u); }
1381
1382 // NB: DR 705.
1383 template<typename... _Elements>
1384 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
1385 make_tuple(_Elements&&... __args)
1386 {
1387 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
1388 __result_type;
1389 return __result_type(std::forward<_Elements>(__args)...);
1390 }
1391
1392 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1393 // 2275. Why is forward_as_tuple not constexpr?
1394 template<typename... _Elements>
1395 constexpr tuple<_Elements&&...>
1396 forward_as_tuple(_Elements&&... __args) noexcept
1397 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
1398
1399 template<typename... _Tps>
1400 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
1401 { };
1402
1403 // Internal type trait that allows us to sfinae-protect tuple_cat.
1404 template<typename _Tp>
1405 struct __is_tuple_like
1406 : public __is_tuple_like_impl<typename std::remove_cv
1407 <typename std::remove_reference<_Tp>::type>::type>::type
1408 { };
1409
1410 template<size_t, typename, typename, size_t>
1411 struct __make_tuple_impl;
1412
1413 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
1414 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
1415 : __make_tuple_impl<_Idx + 1,
1416 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
1417 _Tuple, _Nm>
1418 { };
1419
1420 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
1421 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
1422 {
1423 typedef tuple<_Tp...> __type;
1424 };
1425
1426 template<typename _Tuple>
1427 struct __do_make_tuple
1428 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
1429 { };
1430
1431 // Returns the std::tuple equivalent of a tuple-like type.
1432 template<typename _Tuple>
1433 struct __make_tuple
1434 : public __do_make_tuple<typename std::remove_cv
1435 <typename std::remove_reference<_Tuple>::type>::type>
1436 { };
1437
1438 // Combines several std::tuple's into a single one.
1439 template<typename...>
1440 struct __combine_tuples;
1441
1442 template<>
1443 struct __combine_tuples<>
1444 {
1445 typedef tuple<> __type;
1446 };
1447
1448 template<typename... _Ts>
1449 struct __combine_tuples<tuple<_Ts...>>
1450 {
1451 typedef tuple<_Ts...> __type;
1452 };
1453
1454 template<typename... _T1s, typename... _T2s, typename... _Rem>
1455 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
1456 {
1457 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
1458 _Rem...>::__type __type;
1459 };
1460
1461 // Computes the result type of tuple_cat given a set of tuple-like types.
1462 template<typename... _Tpls>
1463 struct __tuple_cat_result
1464 {
1465 typedef typename __combine_tuples
1466 <typename __make_tuple<_Tpls>::__type...>::__type __type;
1467 };
1468
1469 // Helper to determine the index set for the first tuple-like
1470 // type of a given set.
1471 template<typename...>
1472 struct __make_1st_indices;
1473
1474 template<>
1475 struct __make_1st_indices<>
1476 {
1477 typedef std::_Index_tuple<> __type;
1478 };
1479
1480 template<typename _Tp, typename... _Tpls>
1481 struct __make_1st_indices<_Tp, _Tpls...>
1482 {
1483 typedef typename std::_Build_index_tuple<std::tuple_size<
1484 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1485 };
1486
1487 // Performs the actual concatenation by step-wise expanding tuple-like
1488 // objects into the elements, which are finally forwarded into the
1489 // result tuple.
1490 template<typename _Ret, typename _Indices, typename... _Tpls>
1491 struct __tuple_concater;
1492
1493 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1494 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1495 {
1496 template<typename... _Us>
1497 static constexpr _Ret
1498 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1499 {
1500 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1501 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
1502 return __next::_S_do(std::forward<_Tpls>(__tps)...,
1503 std::forward<_Us>(__us)...,
1504 std::get<_Is>(std::forward<_Tp>(__tp))...);
1505 }
1506 };
1507
1508 template<typename _Ret>
1509 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1510 {
1511 template<typename... _Us>
1512 static constexpr _Ret
1513 _S_do(_Us&&... __us)
1514 {
1515 return _Ret(std::forward<_Us>(__us)...);
1516 }
1517 };
1518
1519 /// tuple_cat
1520 template<typename... _Tpls, typename = typename
1521 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1522 constexpr auto
1523 tuple_cat(_Tpls&&... __tpls)
1524 -> typename __tuple_cat_result<_Tpls...>::__type
1525 {
1526 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1527 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1528 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1529 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1530 }
1531
1532 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1533 // 2301. Why is tie not constexpr?
1534 /// tie
1535 template<typename... _Elements>
1536 constexpr tuple<_Elements&...>
1537 tie(_Elements&... __args) noexcept
1538 { return tuple<_Elements&...>(__args...); }
1539
1540 /// swap
1541 template<typename... _Elements>
1542 inline void
1543 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1544 noexcept(noexcept(__x.swap(__y)))
1545 { __x.swap(__y); }
1546
1547 // A class (and instance) which can be used in 'tie' when an element
1548 // of a tuple is not required
1549 struct _Swallow_assign
1550 {
1551 template<class _Tp>
1552 const _Swallow_assign&
1553 operator=(const _Tp&) const
1554 { return *this; }
1555 };
1556
1557 const _Swallow_assign ignore{};
1558
1559 /// Partial specialization for tuples
1560 template<typename... _Types, typename _Alloc>
1561 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1562
1563 // See stl_pair.h...
1564 template<class _T1, class _T2>
1565 template<typename... _Args1, typename... _Args2>
1566 inline
1567 pair<_T1, _T2>::
1568 pair(piecewise_construct_t,
1569 tuple<_Args1...> __first, tuple<_Args2...> __second)
1570 : pair(__first, __second,
1571 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1572 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1573 { }
1574
1575 template<class _T1, class _T2>
1576 template<typename... _Args1, std::size_t... _Indexes1,
1577 typename... _Args2, std::size_t... _Indexes2>
1578 inline
1579 pair<_T1, _T2>::
1580 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1581 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1582 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1583 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1584 { }
1585
1586 /// @}
1587
1588 _GLIBCXX_END_NAMESPACE_VERSION
1589 } // namespace std
1590
1591 #endif // C++11
1592
1593 #endif // _GLIBCXX_TUPLE