]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/tuple
Implement N4387, "Improving pair and tuple", and LWG 2367.
[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
491 template<typename... _Elements>
492 struct _TC<false, _Elements...>
493 {
494 template<typename... _UElements>
495 static constexpr bool _ConstructibleTuple()
496 {
497 return false;
498 }
499
500 template<typename... _UElements>
501 static constexpr bool _ImplicitlyConvertibleTuple()
502 {
503 return false;
504 }
505
506 template<typename... _UElements>
507 static constexpr bool _MoveConstructibleTuple()
508 {
509 return false;
510 }
511
512 template<typename... _UElements>
513 static constexpr bool _ImplicitlyMoveConvertibleTuple()
514 {
515 return false;
516 }
517 };
518
519 /// Primary class template, tuple
520 template<typename... _Elements>
521 class tuple : public _Tuple_impl<0, _Elements...>
522 {
523 typedef _Tuple_impl<0, _Elements...> _Inherited;
524
525 // Used for constraining the default constructor so
526 // that it becomes dependent on the constraints.
527 template<typename _Dummy>
528 struct _TC2
529 {
530 static constexpr bool _DefaultConstructibleTuple()
531 {
532 return __and_<is_default_constructible<_Elements>...>::value;
533 }
534 };
535
536 public:
537 template<typename _Dummy = void,
538 typename enable_if<_TC2<_Dummy>::
539 _DefaultConstructibleTuple(),
540 bool>::type = true>
541 constexpr tuple()
542 : _Inherited() { }
543
544 // Shortcut for the cases where constructors taking _Elements...
545 // need to be constrained.
546 template<typename _Dummy> using _TCC =
547 _TC<is_same<_Dummy, void>::value,
548 _Elements...>;
549
550 template<typename _Dummy = void,
551 typename enable_if<
552 _TCC<_Dummy>::template
553 _ConstructibleTuple<_Elements...>()
554 && _TCC<_Dummy>::template
555 _ImplicitlyConvertibleTuple<_Elements...>()
556 && (sizeof...(_Elements) >= 1),
557 bool>::type=true>
558 constexpr tuple(const _Elements&... __elements)
559 : _Inherited(__elements...) { }
560
561 template<typename _Dummy = void,
562 typename enable_if<
563 _TCC<_Dummy>::template
564 _ConstructibleTuple<_Elements...>()
565 && !_TCC<_Dummy>::template
566 _ImplicitlyConvertibleTuple<_Elements...>()
567 && (sizeof...(_Elements) >= 1),
568 bool>::type=false>
569 explicit constexpr tuple(const _Elements&... __elements)
570 : _Inherited(__elements...) { }
571
572 // Shortcut for the cases where constructors taking _UElements...
573 // need to be constrained.
574 template<typename... _UElements> using _TMC =
575 _TC<(sizeof...(_Elements) == sizeof...(_UElements)),
576 _Elements...>;
577
578 template<typename... _UElements, typename
579 enable_if<_TMC<_UElements...>::template
580 _MoveConstructibleTuple<_UElements...>()
581 && _TMC<_UElements...>::template
582 _ImplicitlyMoveConvertibleTuple<_UElements...>()
583 && (sizeof...(_Elements) >= 1),
584 bool>::type=true>
585 constexpr tuple(_UElements&&... __elements)
586 : _Inherited(std::forward<_UElements>(__elements)...) { }
587
588 template<typename... _UElements, typename
589 enable_if<_TMC<_UElements...>::template
590 _MoveConstructibleTuple<_UElements...>()
591 && !_TMC<_UElements...>::template
592 _ImplicitlyMoveConvertibleTuple<_UElements...>()
593 && (sizeof...(_Elements) >= 1),
594 bool>::type=false>
595 explicit constexpr tuple(_UElements&&... __elements)
596 : _Inherited(std::forward<_UElements>(__elements)...) { }
597
598 constexpr tuple(const tuple&) = default;
599
600 constexpr tuple(tuple&&) = default;
601
602 template<typename... _UElements, typename
603 enable_if<_TMC<_UElements...>::template
604 _ConstructibleTuple<_UElements...>()
605 && _TMC<_UElements...>::template
606 _ImplicitlyConvertibleTuple<_UElements...>(),
607 bool>::type=true>
608 constexpr tuple(const tuple<_UElements...>& __in)
609 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
610 { }
611
612 template<typename... _UElements, typename
613 enable_if<_TMC<_UElements...>::template
614 _ConstructibleTuple<_UElements...>()
615 && !_TMC<_UElements...>::template
616 _ImplicitlyConvertibleTuple<_UElements...>(),
617 bool>::type=false>
618 explicit constexpr tuple(const tuple<_UElements...>& __in)
619 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
620 { }
621
622 template<typename... _UElements, typename
623 enable_if<_TMC<_UElements...>::template
624 _MoveConstructibleTuple<_UElements...>()
625 && _TMC<_UElements...>::template
626 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
627 bool>::type=true>
628 constexpr tuple(tuple<_UElements...>&& __in)
629 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
630
631 template<typename... _UElements, typename
632 enable_if<_TMC<_UElements...>::template
633 _MoveConstructibleTuple<_UElements...>()
634 && !_TMC<_UElements...>::template
635 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
636 bool>::type=false>
637 explicit constexpr tuple(tuple<_UElements...>&& __in)
638 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
639
640 // Allocator-extended constructors.
641
642 template<typename _Alloc>
643 tuple(allocator_arg_t __tag, const _Alloc& __a)
644 : _Inherited(__tag, __a) { }
645
646 template<typename _Alloc, typename _Dummy = void,
647 typename enable_if<
648 _TCC<_Dummy>::template
649 _ConstructibleTuple<_Elements...>()
650 && _TCC<_Dummy>::template
651 _ImplicitlyConvertibleTuple<_Elements...>(),
652 bool>::type=true>
653 tuple(allocator_arg_t __tag, const _Alloc& __a,
654 const _Elements&... __elements)
655 : _Inherited(__tag, __a, __elements...) { }
656
657 template<typename _Alloc, typename _Dummy = void,
658 typename enable_if<
659 _TCC<_Dummy>::template
660 _ConstructibleTuple<_Elements...>()
661 && !_TCC<_Dummy>::template
662 _ImplicitlyConvertibleTuple<_Elements...>(),
663 bool>::type=false>
664 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
665 const _Elements&... __elements)
666 : _Inherited(__tag, __a, __elements...) { }
667
668 template<typename _Alloc, typename... _UElements, typename
669 enable_if<_TMC<_UElements...>::template
670 _MoveConstructibleTuple<_UElements...>()
671 && _TMC<_UElements...>::template
672 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
673 bool>::type=true>
674 tuple(allocator_arg_t __tag, const _Alloc& __a,
675 _UElements&&... __elements)
676 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
677 { }
678
679 template<typename _Alloc, typename... _UElements, typename
680 enable_if<_TMC<_UElements...>::template
681 _MoveConstructibleTuple<_UElements...>()
682 && !_TMC<_UElements...>::template
683 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
684 bool>::type=false>
685 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
686 _UElements&&... __elements)
687 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
688 { }
689
690 template<typename _Alloc>
691 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
692 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
693
694 template<typename _Alloc>
695 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
696 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
697
698 template<typename _Alloc, typename... _UElements, typename
699 enable_if<_TMC<_UElements...>::template
700 _ConstructibleTuple<_UElements...>()
701 && _TMC<_UElements...>::template
702 _ImplicitlyConvertibleTuple<_UElements...>(),
703 bool>::type=true>
704 tuple(allocator_arg_t __tag, const _Alloc& __a,
705 const tuple<_UElements...>& __in)
706 : _Inherited(__tag, __a,
707 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
708 { }
709
710 template<typename _Alloc, typename... _UElements, typename
711 enable_if<_TMC<_UElements...>::template
712 _ConstructibleTuple<_UElements...>()
713 && !_TMC<_UElements...>::template
714 _ImplicitlyConvertibleTuple<_UElements...>(),
715 bool>::type=false>
716 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
717 const tuple<_UElements...>& __in)
718 : _Inherited(__tag, __a,
719 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
720 { }
721
722 template<typename _Alloc, typename... _UElements, typename
723 enable_if<_TMC<_UElements...>::template
724 _MoveConstructibleTuple<_UElements...>()
725 && _TMC<_UElements...>::template
726 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
727 bool>::type=true>
728 tuple(allocator_arg_t __tag, const _Alloc& __a,
729 tuple<_UElements...>&& __in)
730 : _Inherited(__tag, __a,
731 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
732 { }
733
734 template<typename _Alloc, typename... _UElements, typename
735 enable_if<_TMC<_UElements...>::template
736 _MoveConstructibleTuple<_UElements...>()
737 && !_TMC<_UElements...>::template
738 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
739 bool>::type=false>
740 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
741 tuple<_UElements...>&& __in)
742 : _Inherited(__tag, __a,
743 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
744 { }
745
746 tuple&
747 operator=(const tuple& __in)
748 {
749 static_cast<_Inherited&>(*this) = __in;
750 return *this;
751 }
752
753 tuple&
754 operator=(tuple&& __in)
755 noexcept(is_nothrow_move_assignable<_Inherited>::value)
756 {
757 static_cast<_Inherited&>(*this) = std::move(__in);
758 return *this;
759 }
760
761 template<typename... _UElements, typename = typename
762 enable_if<sizeof...(_UElements)
763 == sizeof...(_Elements)>::type>
764 tuple&
765 operator=(const tuple<_UElements...>& __in)
766 {
767 static_cast<_Inherited&>(*this) = __in;
768 return *this;
769 }
770
771 template<typename... _UElements, typename = typename
772 enable_if<sizeof...(_UElements)
773 == sizeof...(_Elements)>::type>
774 tuple&
775 operator=(tuple<_UElements...>&& __in)
776 {
777 static_cast<_Inherited&>(*this) = std::move(__in);
778 return *this;
779 }
780
781 void
782 swap(tuple& __in)
783 noexcept(noexcept(__in._M_swap(__in)))
784 { _Inherited::_M_swap(__in); }
785 };
786
787 // Explicit specialization, zero-element tuple.
788 template<>
789 class tuple<>
790 {
791 public:
792 void swap(tuple&) noexcept { /* no-op */ }
793 };
794
795 /// Partial specialization, 2-element tuple.
796 /// Includes construction and assignment from a pair.
797 template<typename _T1, typename _T2>
798 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
799 {
800 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
801
802 public:
803 template <typename _U1 = _T1,
804 typename _U2 = _T2,
805 typename enable_if<__and_<
806 is_default_constructible<_U1>,
807 is_default_constructible<_U2>>
808 ::value, bool>::type = true>
809
810 constexpr tuple()
811 : _Inherited() { }
812
813 // Shortcut for the cases where constructors taking _T1, _T2
814 // need to be constrained.
815 template<typename _Dummy> using _TCC =
816 _TC<is_same<_Dummy, void>::value, _T1, _T2>;
817
818 template<typename _Dummy = void, typename
819 enable_if<_TCC<_Dummy>::template
820 _ConstructibleTuple<_T1, _T2>()
821 && _TCC<_Dummy>::template
822 _ImplicitlyConvertibleTuple<_T1, _T2>(),
823 bool>::type = true>
824 constexpr tuple(const _T1& __a1, const _T2& __a2)
825 : _Inherited(__a1, __a2) { }
826
827 template<typename _Dummy = void, typename
828 enable_if<_TCC<_Dummy>::template
829 _ConstructibleTuple<_T1, _T2>()
830 && !_TCC<_Dummy>::template
831 _ImplicitlyConvertibleTuple<_T1, _T2>(),
832 bool>::type = false>
833 explicit constexpr tuple(const _T1& __a1, const _T2& __a2)
834 : _Inherited(__a1, __a2) { }
835
836 // Shortcut for the cases where constructors taking _U1, _U2
837 // need to be constrained.
838 using _TMC = _TC<true, _T1, _T2>;
839
840 template<typename _U1, typename _U2, typename
841 enable_if<_TMC::template
842 _MoveConstructibleTuple<_U1, _U2>()
843 && _TMC::template
844 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
845 bool>::type = true>
846 constexpr tuple(_U1&& __a1, _U2&& __a2)
847 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
848
849 template<typename _U1, typename _U2, typename
850 enable_if<_TMC::template
851 _MoveConstructibleTuple<_U1, _U2>()
852 && !_TMC::template
853 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
854 bool>::type = false>
855 explicit constexpr tuple(_U1&& __a1, _U2&& __a2)
856 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
857
858 constexpr tuple(const tuple&) = default;
859
860 constexpr tuple(tuple&&) = default;
861
862 template<typename _U1, typename _U2, typename
863 enable_if<_TMC::template
864 _ConstructibleTuple<_U1, _U2>()
865 && _TMC::template
866 _ImplicitlyConvertibleTuple<_U1, _U2>(),
867 bool>::type = true>
868 constexpr tuple(const tuple<_U1, _U2>& __in)
869 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
870
871 template<typename _U1, typename _U2, typename
872 enable_if<_TMC::template
873 _ConstructibleTuple<_U1, _U2>()
874 && !_TMC::template
875 _ImplicitlyConvertibleTuple<_U1, _U2>(),
876 bool>::type = false>
877 explicit constexpr tuple(const tuple<_U1, _U2>& __in)
878 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
879
880 template<typename _U1, typename _U2, typename
881 enable_if<_TMC::template
882 _MoveConstructibleTuple<_U1, _U2>()
883 && _TMC::template
884 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
885 bool>::type = true>
886 constexpr tuple(tuple<_U1, _U2>&& __in)
887 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
888
889 template<typename _U1, typename _U2, typename
890 enable_if<_TMC::template
891 _MoveConstructibleTuple<_U1, _U2>()
892 && !_TMC::template
893 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
894 bool>::type = false>
895 explicit constexpr tuple(tuple<_U1, _U2>&& __in)
896 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
897
898 template<typename _U1, typename _U2, typename
899 enable_if<_TMC::template
900 _ConstructibleTuple<_U1, _U2>()
901 && _TMC::template
902 _ImplicitlyConvertibleTuple<_U1, _U2>(),
903 bool>::type = true>
904 constexpr tuple(const pair<_U1, _U2>& __in)
905 : _Inherited(__in.first, __in.second) { }
906
907 template<typename _U1, typename _U2, typename
908 enable_if<_TMC::template
909 _ConstructibleTuple<_U1, _U2>()
910 && !_TMC::template
911 _ImplicitlyConvertibleTuple<_U1, _U2>(),
912 bool>::type = false>
913 explicit constexpr tuple(const pair<_U1, _U2>& __in)
914 : _Inherited(__in.first, __in.second) { }
915
916 template<typename _U1, typename _U2, typename
917 enable_if<_TMC::template
918 _MoveConstructibleTuple<_U1, _U2>()
919 && _TMC::template
920 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
921 bool>::type = true>
922 constexpr tuple(pair<_U1, _U2>&& __in)
923 : _Inherited(std::forward<_U1>(__in.first),
924 std::forward<_U2>(__in.second)) { }
925
926 template<typename _U1, typename _U2, typename
927 enable_if<_TMC::template
928 _MoveConstructibleTuple<_U1, _U2>()
929 && !_TMC::template
930 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
931 bool>::type = false>
932 explicit constexpr tuple(pair<_U1, _U2>&& __in)
933 : _Inherited(std::forward<_U1>(__in.first),
934 std::forward<_U2>(__in.second)) { }
935
936 // Allocator-extended constructors.
937
938 template<typename _Alloc>
939 tuple(allocator_arg_t __tag, const _Alloc& __a)
940 : _Inherited(__tag, __a) { }
941
942 template<typename _Alloc, typename _Dummy = void,
943 typename enable_if<
944 _TCC<_Dummy>::template
945 _ConstructibleTuple<_T1, _T2>()
946 && _TCC<_Dummy>::template
947 _ImplicitlyConvertibleTuple<_T1, _T2>(),
948 bool>::type=true>
949
950 tuple(allocator_arg_t __tag, const _Alloc& __a,
951 const _T1& __a1, const _T2& __a2)
952 : _Inherited(__tag, __a, __a1, __a2) { }
953
954 template<typename _Alloc, typename _Dummy = void,
955 typename enable_if<
956 _TCC<_Dummy>::template
957 _ConstructibleTuple<_T1, _T2>()
958 && !_TCC<_Dummy>::template
959 _ImplicitlyConvertibleTuple<_T1, _T2>(),
960 bool>::type=false>
961
962 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
963 const _T1& __a1, const _T2& __a2)
964 : _Inherited(__tag, __a, __a1, __a2) { }
965
966 template<typename _Alloc, typename _U1, typename _U2, typename
967 enable_if<_TMC::template
968 _MoveConstructibleTuple<_U1, _U2>()
969 && _TMC::template
970 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
971 bool>::type = true>
972 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
973 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
974 std::forward<_U2>(__a2)) { }
975
976 template<typename _Alloc, typename _U1, typename _U2, typename
977 enable_if<_TMC::template
978 _MoveConstructibleTuple<_U1, _U2>()
979 && !_TMC::template
980 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
981 bool>::type = false>
982 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
983 _U1&& __a1, _U2&& __a2)
984 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
985 std::forward<_U2>(__a2)) { }
986
987 template<typename _Alloc>
988 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
989 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
990
991 template<typename _Alloc>
992 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
993 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
994
995 template<typename _Alloc, typename _U1, typename _U2, typename
996 enable_if<_TMC::template
997 _ConstructibleTuple<_U1, _U2>()
998 && _TMC::template
999 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1000 bool>::type = true>
1001 tuple(allocator_arg_t __tag, const _Alloc& __a,
1002 const tuple<_U1, _U2>& __in)
1003 : _Inherited(__tag, __a,
1004 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1005 { }
1006
1007 template<typename _Alloc, typename _U1, typename _U2, typename
1008 enable_if<_TMC::template
1009 _ConstructibleTuple<_U1, _U2>()
1010 && !_TMC::template
1011 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1012 bool>::type = false>
1013 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1014 const tuple<_U1, _U2>& __in)
1015 : _Inherited(__tag, __a,
1016 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1017 { }
1018
1019 template<typename _Alloc, typename _U1, typename _U2, typename
1020 enable_if<_TMC::template
1021 _MoveConstructibleTuple<_U1, _U2>()
1022 && _TMC::template
1023 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1024 bool>::type = true>
1025 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1026 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1027 { }
1028
1029 template<typename _Alloc, typename _U1, typename _U2, typename
1030 enable_if<_TMC::template
1031 _MoveConstructibleTuple<_U1, _U2>()
1032 && !_TMC::template
1033 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1034 bool>::type = false>
1035 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1036 tuple<_U1, _U2>&& __in)
1037 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1038 { }
1039
1040 template<typename _Alloc, typename _U1, typename _U2, typename
1041 enable_if<_TMC::template
1042 _ConstructibleTuple<_U1, _U2>()
1043 && _TMC::template
1044 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1045 bool>::type = true>
1046 tuple(allocator_arg_t __tag, const _Alloc& __a,
1047 const pair<_U1, _U2>& __in)
1048 : _Inherited(__tag, __a, __in.first, __in.second) { }
1049
1050 template<typename _Alloc, typename _U1, typename _U2, typename
1051 enable_if<_TMC::template
1052 _ConstructibleTuple<_U1, _U2>()
1053 && !_TMC::template
1054 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1055 bool>::type = false>
1056 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1057 const pair<_U1, _U2>& __in)
1058 : _Inherited(__tag, __a, __in.first, __in.second) { }
1059
1060 template<typename _Alloc, typename _U1, typename _U2, typename
1061 enable_if<_TMC::template
1062 _MoveConstructibleTuple<_U1, _U2>()
1063 && _TMC::template
1064 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1065 bool>::type = true>
1066 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
1067 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1068 std::forward<_U2>(__in.second)) { }
1069
1070 template<typename _Alloc, typename _U1, typename _U2, typename
1071 enable_if<_TMC::template
1072 _MoveConstructibleTuple<_U1, _U2>()
1073 && !_TMC::template
1074 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1075 bool>::type = false>
1076 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1077 pair<_U1, _U2>&& __in)
1078 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1079 std::forward<_U2>(__in.second)) { }
1080
1081 tuple&
1082 operator=(const tuple& __in)
1083 {
1084 static_cast<_Inherited&>(*this) = __in;
1085 return *this;
1086 }
1087
1088 tuple&
1089 operator=(tuple&& __in)
1090 noexcept(is_nothrow_move_assignable<_Inherited>::value)
1091 {
1092 static_cast<_Inherited&>(*this) = std::move(__in);
1093 return *this;
1094 }
1095
1096 template<typename _U1, typename _U2>
1097 tuple&
1098 operator=(const tuple<_U1, _U2>& __in)
1099 {
1100 static_cast<_Inherited&>(*this) = __in;
1101 return *this;
1102 }
1103
1104 template<typename _U1, typename _U2>
1105 tuple&
1106 operator=(tuple<_U1, _U2>&& __in)
1107 {
1108 static_cast<_Inherited&>(*this) = std::move(__in);
1109 return *this;
1110 }
1111
1112 template<typename _U1, typename _U2>
1113 tuple&
1114 operator=(const pair<_U1, _U2>& __in)
1115 {
1116 this->_M_head(*this) = __in.first;
1117 this->_M_tail(*this)._M_head(*this) = __in.second;
1118 return *this;
1119 }
1120
1121 template<typename _U1, typename _U2>
1122 tuple&
1123 operator=(pair<_U1, _U2>&& __in)
1124 {
1125 this->_M_head(*this) = std::forward<_U1>(__in.first);
1126 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
1127 return *this;
1128 }
1129
1130 void
1131 swap(tuple& __in)
1132 noexcept(noexcept(__in._M_swap(__in)))
1133 { _Inherited::_M_swap(__in); }
1134 };
1135
1136
1137 /// Gives the type of the ith element of a given tuple type.
1138 template<std::size_t __i, typename _Tp>
1139 struct tuple_element;
1140
1141 /**
1142 * Recursive case for tuple_element: strip off the first element in
1143 * the tuple and retrieve the (i-1)th element of the remaining tuple.
1144 */
1145 template<std::size_t __i, typename _Head, typename... _Tail>
1146 struct tuple_element<__i, tuple<_Head, _Tail...> >
1147 : tuple_element<__i - 1, tuple<_Tail...> > { };
1148
1149 /**
1150 * Basis case for tuple_element: The first element is the one we're seeking.
1151 */
1152 template<typename _Head, typename... _Tail>
1153 struct tuple_element<0, tuple<_Head, _Tail...> >
1154 {
1155 typedef _Head type;
1156 };
1157
1158 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
1159 template<std::size_t __i, typename _Tp>
1160 using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
1161
1162 template<std::size_t __i, typename _Tp>
1163 struct tuple_element<__i, const _Tp>
1164 {
1165 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
1166 };
1167
1168 template<std::size_t __i, typename _Tp>
1169 struct tuple_element<__i, volatile _Tp>
1170 {
1171 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
1172 };
1173
1174 template<std::size_t __i, typename _Tp>
1175 struct tuple_element<__i, const volatile _Tp>
1176 {
1177 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
1178 };
1179
1180 #if __cplusplus > 201103L
1181 #define __cpp_lib_tuple_element_t 201402
1182
1183 template<std::size_t __i, typename _Tp>
1184 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
1185 #endif
1186
1187 /// Finds the size of a given tuple type.
1188 template<typename _Tp>
1189 struct tuple_size;
1190
1191 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1192 // 2313. tuple_size should always derive from integral_constant<size_t, N>
1193 template<typename _Tp>
1194 struct tuple_size<const _Tp>
1195 : integral_constant<size_t, tuple_size<_Tp>::value> { };
1196
1197 template<typename _Tp>
1198 struct tuple_size<volatile _Tp>
1199 : integral_constant<size_t, tuple_size<_Tp>::value> { };
1200
1201 template<typename _Tp>
1202 struct tuple_size<const volatile _Tp>
1203 : integral_constant<size_t, tuple_size<_Tp>::value> { };
1204
1205 /// class tuple_size
1206 template<typename... _Elements>
1207 struct tuple_size<tuple<_Elements...>>
1208 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
1209
1210 template<std::size_t __i, typename _Head, typename... _Tail>
1211 constexpr _Head&
1212 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1213 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1214
1215 template<std::size_t __i, typename _Head, typename... _Tail>
1216 constexpr const _Head&
1217 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1218 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1219
1220 /// Return a reference to the ith element of a tuple.
1221 template<std::size_t __i, typename... _Elements>
1222 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
1223 get(tuple<_Elements...>& __t) noexcept
1224 { return std::__get_helper<__i>(__t); }
1225
1226 /// Return a const reference to the ith element of a const tuple.
1227 template<std::size_t __i, typename... _Elements>
1228 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
1229 get(const tuple<_Elements...>& __t) noexcept
1230 { return std::__get_helper<__i>(__t); }
1231
1232 /// Return an rvalue reference to the ith element of a tuple rvalue.
1233 template<std::size_t __i, typename... _Elements>
1234 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
1235 get(tuple<_Elements...>&& __t) noexcept
1236 {
1237 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1238 return std::forward<__element_type&&>(std::get<__i>(__t));
1239 }
1240
1241 #if __cplusplus > 201103L
1242
1243 #define __cpp_lib_tuples_by_type 201304
1244
1245 template<typename _Head, size_t __i, typename... _Tail>
1246 constexpr _Head&
1247 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1248 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1249
1250 template<typename _Head, size_t __i, typename... _Tail>
1251 constexpr const _Head&
1252 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1253 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1254
1255 /// Return a reference to the unique element of type _Tp of a tuple.
1256 template <typename _Tp, typename... _Types>
1257 constexpr _Tp&
1258 get(tuple<_Types...>& __t) noexcept
1259 { return std::__get_helper2<_Tp>(__t); }
1260
1261 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
1262 template <typename _Tp, typename... _Types>
1263 constexpr _Tp&&
1264 get(tuple<_Types...>&& __t) noexcept
1265 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
1266
1267 /// Return a const reference to the unique element of type _Tp of a tuple.
1268 template <typename _Tp, typename... _Types>
1269 constexpr const _Tp&
1270 get(const tuple<_Types...>& __t) noexcept
1271 { return std::__get_helper2<_Tp>(__t); }
1272 #endif
1273
1274 // This class performs the comparison operations on tuples
1275 template<typename _Tp, typename _Up, size_t __i, size_t __size>
1276 struct __tuple_compare
1277 {
1278 static constexpr bool
1279 __eq(const _Tp& __t, const _Up& __u)
1280 {
1281 return bool(std::get<__i>(__t) == std::get<__i>(__u))
1282 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
1283 }
1284
1285 static constexpr bool
1286 __less(const _Tp& __t, const _Up& __u)
1287 {
1288 return bool(std::get<__i>(__t) < std::get<__i>(__u))
1289 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
1290 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
1291 }
1292 };
1293
1294 template<typename _Tp, typename _Up, size_t __size>
1295 struct __tuple_compare<_Tp, _Up, __size, __size>
1296 {
1297 static constexpr bool
1298 __eq(const _Tp&, const _Up&) { return true; }
1299
1300 static constexpr bool
1301 __less(const _Tp&, const _Up&) { return false; }
1302 };
1303
1304 template<typename... _TElements, typename... _UElements>
1305 constexpr bool
1306 operator==(const tuple<_TElements...>& __t,
1307 const tuple<_UElements...>& __u)
1308 {
1309 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1310 "tuple objects can only be compared if they have equal sizes.");
1311 using __compare = __tuple_compare<tuple<_TElements...>,
1312 tuple<_UElements...>,
1313 0, sizeof...(_TElements)>;
1314 return __compare::__eq(__t, __u);
1315 }
1316
1317 template<typename... _TElements, typename... _UElements>
1318 constexpr bool
1319 operator<(const tuple<_TElements...>& __t,
1320 const tuple<_UElements...>& __u)
1321 {
1322 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1323 "tuple objects can only be compared if they have equal sizes.");
1324 using __compare = __tuple_compare<tuple<_TElements...>,
1325 tuple<_UElements...>,
1326 0, sizeof...(_TElements)>;
1327 return __compare::__less(__t, __u);
1328 }
1329
1330 template<typename... _TElements, typename... _UElements>
1331 constexpr bool
1332 operator!=(const tuple<_TElements...>& __t,
1333 const tuple<_UElements...>& __u)
1334 { return !(__t == __u); }
1335
1336 template<typename... _TElements, typename... _UElements>
1337 constexpr bool
1338 operator>(const tuple<_TElements...>& __t,
1339 const tuple<_UElements...>& __u)
1340 { return __u < __t; }
1341
1342 template<typename... _TElements, typename... _UElements>
1343 constexpr bool
1344 operator<=(const tuple<_TElements...>& __t,
1345 const tuple<_UElements...>& __u)
1346 { return !(__u < __t); }
1347
1348 template<typename... _TElements, typename... _UElements>
1349 constexpr bool
1350 operator>=(const tuple<_TElements...>& __t,
1351 const tuple<_UElements...>& __u)
1352 { return !(__t < __u); }
1353
1354 // NB: DR 705.
1355 template<typename... _Elements>
1356 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
1357 make_tuple(_Elements&&... __args)
1358 {
1359 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
1360 __result_type;
1361 return __result_type(std::forward<_Elements>(__args)...);
1362 }
1363
1364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1365 // 2275. Why is forward_as_tuple not constexpr?
1366 template<typename... _Elements>
1367 constexpr tuple<_Elements&&...>
1368 forward_as_tuple(_Elements&&... __args) noexcept
1369 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
1370
1371 template<typename... _Tps>
1372 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
1373 { };
1374
1375 // Internal type trait that allows us to sfinae-protect tuple_cat.
1376 template<typename _Tp>
1377 struct __is_tuple_like
1378 : public __is_tuple_like_impl<typename std::remove_cv
1379 <typename std::remove_reference<_Tp>::type>::type>::type
1380 { };
1381
1382 template<size_t, typename, typename, size_t>
1383 struct __make_tuple_impl;
1384
1385 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
1386 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
1387 : __make_tuple_impl<_Idx + 1,
1388 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
1389 _Tuple, _Nm>
1390 { };
1391
1392 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
1393 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
1394 {
1395 typedef tuple<_Tp...> __type;
1396 };
1397
1398 template<typename _Tuple>
1399 struct __do_make_tuple
1400 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
1401 { };
1402
1403 // Returns the std::tuple equivalent of a tuple-like type.
1404 template<typename _Tuple>
1405 struct __make_tuple
1406 : public __do_make_tuple<typename std::remove_cv
1407 <typename std::remove_reference<_Tuple>::type>::type>
1408 { };
1409
1410 // Combines several std::tuple's into a single one.
1411 template<typename...>
1412 struct __combine_tuples;
1413
1414 template<>
1415 struct __combine_tuples<>
1416 {
1417 typedef tuple<> __type;
1418 };
1419
1420 template<typename... _Ts>
1421 struct __combine_tuples<tuple<_Ts...>>
1422 {
1423 typedef tuple<_Ts...> __type;
1424 };
1425
1426 template<typename... _T1s, typename... _T2s, typename... _Rem>
1427 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
1428 {
1429 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
1430 _Rem...>::__type __type;
1431 };
1432
1433 // Computes the result type of tuple_cat given a set of tuple-like types.
1434 template<typename... _Tpls>
1435 struct __tuple_cat_result
1436 {
1437 typedef typename __combine_tuples
1438 <typename __make_tuple<_Tpls>::__type...>::__type __type;
1439 };
1440
1441 // Helper to determine the index set for the first tuple-like
1442 // type of a given set.
1443 template<typename...>
1444 struct __make_1st_indices;
1445
1446 template<>
1447 struct __make_1st_indices<>
1448 {
1449 typedef std::_Index_tuple<> __type;
1450 };
1451
1452 template<typename _Tp, typename... _Tpls>
1453 struct __make_1st_indices<_Tp, _Tpls...>
1454 {
1455 typedef typename std::_Build_index_tuple<std::tuple_size<
1456 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1457 };
1458
1459 // Performs the actual concatenation by step-wise expanding tuple-like
1460 // objects into the elements, which are finally forwarded into the
1461 // result tuple.
1462 template<typename _Ret, typename _Indices, typename... _Tpls>
1463 struct __tuple_concater;
1464
1465 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1466 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1467 {
1468 template<typename... _Us>
1469 static constexpr _Ret
1470 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1471 {
1472 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1473 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
1474 return __next::_S_do(std::forward<_Tpls>(__tps)...,
1475 std::forward<_Us>(__us)...,
1476 std::get<_Is>(std::forward<_Tp>(__tp))...);
1477 }
1478 };
1479
1480 template<typename _Ret>
1481 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1482 {
1483 template<typename... _Us>
1484 static constexpr _Ret
1485 _S_do(_Us&&... __us)
1486 {
1487 return _Ret(std::forward<_Us>(__us)...);
1488 }
1489 };
1490
1491 /// tuple_cat
1492 template<typename... _Tpls, typename = typename
1493 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1494 constexpr auto
1495 tuple_cat(_Tpls&&... __tpls)
1496 -> typename __tuple_cat_result<_Tpls...>::__type
1497 {
1498 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1499 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1500 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1501 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1502 }
1503
1504 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1505 // 2301. Why is tie not constexpr?
1506 /// tie
1507 template<typename... _Elements>
1508 constexpr tuple<_Elements&...>
1509 tie(_Elements&... __args) noexcept
1510 { return tuple<_Elements&...>(__args...); }
1511
1512 /// swap
1513 template<typename... _Elements>
1514 inline void
1515 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1516 noexcept(noexcept(__x.swap(__y)))
1517 { __x.swap(__y); }
1518
1519 // A class (and instance) which can be used in 'tie' when an element
1520 // of a tuple is not required
1521 struct _Swallow_assign
1522 {
1523 template<class _Tp>
1524 const _Swallow_assign&
1525 operator=(const _Tp&) const
1526 { return *this; }
1527 };
1528
1529 const _Swallow_assign ignore{};
1530
1531 /// Partial specialization for tuples
1532 template<typename... _Types, typename _Alloc>
1533 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1534
1535 // See stl_pair.h...
1536 template<class _T1, class _T2>
1537 template<typename... _Args1, typename... _Args2>
1538 inline
1539 pair<_T1, _T2>::
1540 pair(piecewise_construct_t,
1541 tuple<_Args1...> __first, tuple<_Args2...> __second)
1542 : pair(__first, __second,
1543 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1544 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1545 { }
1546
1547 template<class _T1, class _T2>
1548 template<typename... _Args1, std::size_t... _Indexes1,
1549 typename... _Args2, std::size_t... _Indexes2>
1550 inline
1551 pair<_T1, _T2>::
1552 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1553 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1554 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1555 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1556 { }
1557
1558 /// @}
1559
1560 _GLIBCXX_END_NAMESPACE_VERSION
1561 } // namespace std
1562
1563 #endif // C++11
1564
1565 #endif // _GLIBCXX_TUPLE