3 // Copyright (C) 2007-2014 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/tuple
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_TUPLE
30 #define _GLIBCXX_TUPLE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
40 #include <bits/uses_allocator.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 * @addtogroup utilities
51 // Adds a const reference to a non-reference type.
52 template<typename _Tp>
54 { typedef const _Tp& type; };
56 template<typename _Tp>
57 struct __add_c_ref<_Tp&>
58 { typedef _Tp& type; };
60 // Adds a reference to a non-reference type.
61 template<typename _Tp>
63 { typedef _Tp& type; };
65 template<typename _Tp>
66 struct __add_ref<_Tp&>
67 { typedef _Tp& type; };
69 // Adds an rvalue reference to a non-reference type.
70 template<typename _Tp>
72 { typedef _Tp&& type; };
74 template<typename _Tp>
75 struct __add_r_ref<_Tp&>
76 { typedef _Tp& type; };
78 template<std::size_t _Idx, typename _Head, bool _IsEmptyNotFinal>
81 template<std::size_t _Idx, typename _Head>
82 struct _Head_base<_Idx, _Head, true>
85 constexpr _Head_base()
88 constexpr _Head_base(const _Head& __h)
91 constexpr _Head_base(const _Head_base&) = default;
92 constexpr _Head_base(_Head_base&&) = default;
94 template<typename _UHead>
95 constexpr _Head_base(_UHead&& __h)
96 : _Head(std::forward<_UHead>(__h)) { }
98 _Head_base(allocator_arg_t, __uses_alloc0)
101 template<typename _Alloc>
102 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
103 : _Head(allocator_arg, *__a._M_a) { }
105 template<typename _Alloc>
106 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
107 : _Head(*__a._M_a) { }
109 template<typename _UHead>
110 _Head_base(__uses_alloc0, _UHead&& __uhead)
111 : _Head(std::forward<_UHead>(__uhead)) { }
113 template<typename _Alloc, typename _UHead>
114 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
115 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
117 template<typename _Alloc, typename _UHead>
118 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
119 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
121 static constexpr _Head&
122 _M_head(_Head_base& __b) noexcept { return __b; }
124 static constexpr const _Head&
125 _M_head(const _Head_base& __b) noexcept { return __b; }
128 template<std::size_t _Idx, typename _Head>
129 struct _Head_base<_Idx, _Head, false>
131 constexpr _Head_base()
134 constexpr _Head_base(const _Head& __h)
135 : _M_head_impl(__h) { }
137 constexpr _Head_base(const _Head_base&) = default;
138 constexpr _Head_base(_Head_base&&) = default;
140 template<typename _UHead>
141 constexpr _Head_base(_UHead&& __h)
142 : _M_head_impl(std::forward<_UHead>(__h)) { }
144 _Head_base(allocator_arg_t, __uses_alloc0)
147 template<typename _Alloc>
148 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
149 : _M_head_impl(allocator_arg, *__a._M_a) { }
151 template<typename _Alloc>
152 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
153 : _M_head_impl(*__a._M_a) { }
155 template<typename _UHead>
156 _Head_base(__uses_alloc0, _UHead&& __uhead)
157 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
159 template<typename _Alloc, typename _UHead>
160 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
161 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
164 template<typename _Alloc, typename _UHead>
165 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
166 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
168 static constexpr _Head&
169 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
171 static constexpr const _Head&
172 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
178 * Contains the actual implementation of the @c tuple template, stored
179 * as a recursive inheritance hierarchy from the first element (most
180 * derived class) to the last (least derived class). The @c Idx
181 * parameter gives the 0-based index of the element stored at this
182 * point in the hierarchy; we use it to implement a constant-time
185 template<std::size_t _Idx, typename... _Elements>
189 * Zero-element tuple implementation. This is the basis case for the
190 * inheritance recursion.
192 template<std::size_t _Idx>
193 struct _Tuple_impl<_Idx>
195 template<std::size_t, typename...> friend class _Tuple_impl;
197 _Tuple_impl() = default;
199 template<typename _Alloc>
200 _Tuple_impl(allocator_arg_t, const _Alloc&) { }
202 template<typename _Alloc>
203 _Tuple_impl(allocator_arg_t, const _Alloc&, const _Tuple_impl&) { }
205 template<typename _Alloc>
206 _Tuple_impl(allocator_arg_t, const _Alloc&, _Tuple_impl&&) { }
209 void _M_swap(_Tuple_impl&) noexcept { /* no-op */ }
212 template<typename _Tp>
213 struct __is_empty_non_tuple : is_empty<_Tp> { };
215 // Using EBO for elements that are tuples causes ambiguous base errors.
216 template<typename _El0, typename... _El>
217 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
219 // Use the Empty Base-class Optimization for empty, non-final types.
220 template<typename _Tp>
221 using __empty_not_final
222 = typename conditional<__is_final(_Tp), false_type,
223 __is_empty_non_tuple<_Tp>>::type;
226 * Recursive tuple implementation. Here we store the @c Head element
227 * and derive from a @c Tuple_impl containing the remaining elements
228 * (which contains the @c Tail).
230 template<std::size_t _Idx, typename _Head, typename... _Tail>
231 struct _Tuple_impl<_Idx, _Head, _Tail...>
232 : public _Tuple_impl<_Idx + 1, _Tail...>,
233 private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
235 template<std::size_t, typename...> friend class _Tuple_impl;
237 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
238 typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
240 static constexpr _Head&
241 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
243 static constexpr const _Head&
244 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
246 static constexpr _Inherited&
247 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
249 static constexpr const _Inherited&
250 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
252 constexpr _Tuple_impl()
253 : _Inherited(), _Base() { }
256 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
257 : _Inherited(__tail...), _Base(__head) { }
259 template<typename _UHead, typename... _UTail, typename = typename
260 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
262 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
263 : _Inherited(std::forward<_UTail>(__tail)...),
264 _Base(std::forward<_UHead>(__head)) { }
266 constexpr _Tuple_impl(const _Tuple_impl&) = default;
269 _Tuple_impl(_Tuple_impl&& __in)
270 noexcept(__and_<is_nothrow_move_constructible<_Head>,
271 is_nothrow_move_constructible<_Inherited>>::value)
272 : _Inherited(std::move(_M_tail(__in))),
273 _Base(std::forward<_Head>(_M_head(__in))) { }
275 template<typename... _UElements>
276 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
277 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
278 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
280 template<typename _UHead, typename... _UTails>
281 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
282 : _Inherited(std::move
283 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
284 _Base(std::forward<_UHead>
285 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
287 template<typename _Alloc>
288 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
289 : _Inherited(__tag, __a),
290 _Base(__tag, __use_alloc<_Head>(__a)) { }
292 template<typename _Alloc>
293 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
294 const _Head& __head, const _Tail&... __tail)
295 : _Inherited(__tag, __a, __tail...),
296 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
298 template<typename _Alloc, typename _UHead, typename... _UTail,
299 typename = typename enable_if<sizeof...(_Tail)
300 == sizeof...(_UTail)>::type>
301 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
302 _UHead&& __head, _UTail&&... __tail)
303 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
304 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
305 std::forward<_UHead>(__head)) { }
307 template<typename _Alloc>
308 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
309 const _Tuple_impl& __in)
310 : _Inherited(__tag, __a, _M_tail(__in)),
311 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
313 template<typename _Alloc>
314 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
316 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
317 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
318 std::forward<_Head>(_M_head(__in))) { }
320 template<typename _Alloc, typename... _UElements>
321 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
322 const _Tuple_impl<_Idx, _UElements...>& __in)
323 : _Inherited(__tag, __a,
324 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
325 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
326 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
328 template<typename _Alloc, typename _UHead, typename... _UTails>
329 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
330 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
331 : _Inherited(__tag, __a, std::move
332 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
333 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
335 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
338 operator=(const _Tuple_impl& __in)
340 _M_head(*this) = _M_head(__in);
341 _M_tail(*this) = _M_tail(__in);
346 operator=(_Tuple_impl&& __in)
347 noexcept(__and_<is_nothrow_move_assignable<_Head>,
348 is_nothrow_move_assignable<_Inherited>>::value)
350 _M_head(*this) = std::forward<_Head>(_M_head(__in));
351 _M_tail(*this) = std::move(_M_tail(__in));
355 template<typename... _UElements>
357 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
359 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
360 _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
364 template<typename _UHead, typename... _UTails>
366 operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
368 _M_head(*this) = std::forward<_UHead>
369 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
370 _M_tail(*this) = std::move
371 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
377 _M_swap(_Tuple_impl& __in)
378 noexcept(noexcept(swap(std::declval<_Head&>(),
379 std::declval<_Head&>()))
380 && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
383 swap(_M_head(*this), _M_head(__in));
384 _Inherited::_M_swap(_M_tail(__in));
388 /// Primary class template, tuple
389 template<typename... _Elements>
390 class tuple : public _Tuple_impl<0, _Elements...>
392 typedef _Tuple_impl<0, _Elements...> _Inherited;
399 constexpr tuple(const _Elements&... __elements)
400 : _Inherited(__elements...) { }
402 template<typename... _UElements, typename = typename
403 enable_if<__and_<is_convertible<_UElements,
404 _Elements>...>::value>::type>
406 constexpr tuple(_UElements&&... __elements)
407 : _Inherited(std::forward<_UElements>(__elements)...) { }
409 constexpr tuple(const tuple&) = default;
411 constexpr tuple(tuple&&) = default;
413 template<typename... _UElements, typename = typename
414 enable_if<__and_<is_convertible<const _UElements&,
415 _Elements>...>::value>::type>
416 constexpr tuple(const tuple<_UElements...>& __in)
417 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
420 template<typename... _UElements, typename = typename
421 enable_if<__and_<is_convertible<_UElements,
422 _Elements>...>::value>::type>
423 constexpr tuple(tuple<_UElements...>&& __in)
424 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
426 // Allocator-extended constructors.
428 template<typename _Alloc>
429 tuple(allocator_arg_t __tag, const _Alloc& __a)
430 : _Inherited(__tag, __a) { }
432 template<typename _Alloc>
433 tuple(allocator_arg_t __tag, const _Alloc& __a,
434 const _Elements&... __elements)
435 : _Inherited(__tag, __a, __elements...) { }
437 template<typename _Alloc, typename... _UElements, typename = typename
438 enable_if<sizeof...(_UElements)
439 == sizeof...(_Elements)>::type>
440 tuple(allocator_arg_t __tag, const _Alloc& __a,
441 _UElements&&... __elements)
442 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
445 template<typename _Alloc>
446 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
447 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
449 template<typename _Alloc>
450 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
451 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
453 template<typename _Alloc, typename... _UElements, typename = typename
454 enable_if<sizeof...(_UElements)
455 == sizeof...(_Elements)>::type>
456 tuple(allocator_arg_t __tag, const _Alloc& __a,
457 const tuple<_UElements...>& __in)
458 : _Inherited(__tag, __a,
459 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
462 template<typename _Alloc, typename... _UElements, typename = typename
463 enable_if<sizeof...(_UElements)
464 == sizeof...(_Elements)>::type>
465 tuple(allocator_arg_t __tag, const _Alloc& __a,
466 tuple<_UElements...>&& __in)
467 : _Inherited(__tag, __a,
468 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
472 operator=(const tuple& __in)
474 static_cast<_Inherited&>(*this) = __in;
479 operator=(tuple&& __in)
480 noexcept(is_nothrow_move_assignable<_Inherited>::value)
482 static_cast<_Inherited&>(*this) = std::move(__in);
486 template<typename... _UElements, typename = typename
487 enable_if<sizeof...(_UElements)
488 == sizeof...(_Elements)>::type>
490 operator=(const tuple<_UElements...>& __in)
492 static_cast<_Inherited&>(*this) = __in;
496 template<typename... _UElements, typename = typename
497 enable_if<sizeof...(_UElements)
498 == sizeof...(_Elements)>::type>
500 operator=(tuple<_UElements...>&& __in)
502 static_cast<_Inherited&>(*this) = std::move(__in);
508 noexcept(noexcept(__in._M_swap(__in)))
509 { _Inherited::_M_swap(__in); }
512 // Explicit specialization, zero-element tuple.
517 void swap(tuple&) noexcept { /* no-op */ }
520 /// Partial specialization, 2-element tuple.
521 /// Includes construction and assignment from a pair.
522 template<typename _T1, typename _T2>
523 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
525 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
532 constexpr tuple(const _T1& __a1, const _T2& __a2)
533 : _Inherited(__a1, __a2) { }
535 template<typename _U1, typename _U2, typename = typename
536 enable_if<__and_<is_convertible<_U1, _T1>,
537 is_convertible<_U2, _T2>>::value>::type>
539 constexpr tuple(_U1&& __a1, _U2&& __a2)
540 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
542 constexpr tuple(const tuple&) = default;
544 constexpr tuple(tuple&&) = default;
546 template<typename _U1, typename _U2, typename = typename
547 enable_if<__and_<is_convertible<const _U1&, _T1>,
548 is_convertible<const _U2&, _T2>>::value>::type>
549 constexpr tuple(const tuple<_U1, _U2>& __in)
550 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
552 template<typename _U1, typename _U2, typename = typename
553 enable_if<__and_<is_convertible<_U1, _T1>,
554 is_convertible<_U2, _T2>>::value>::type>
555 constexpr tuple(tuple<_U1, _U2>&& __in)
556 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
558 template<typename _U1, typename _U2, typename = typename
559 enable_if<__and_<is_convertible<const _U1&, _T1>,
560 is_convertible<const _U2&, _T2>>::value>::type>
561 constexpr tuple(const pair<_U1, _U2>& __in)
562 : _Inherited(__in.first, __in.second) { }
564 template<typename _U1, typename _U2, typename = typename
565 enable_if<__and_<is_convertible<_U1, _T1>,
566 is_convertible<_U2, _T2>>::value>::type>
567 constexpr tuple(pair<_U1, _U2>&& __in)
568 : _Inherited(std::forward<_U1>(__in.first),
569 std::forward<_U2>(__in.second)) { }
571 // Allocator-extended constructors.
573 template<typename _Alloc>
574 tuple(allocator_arg_t __tag, const _Alloc& __a)
575 : _Inherited(__tag, __a) { }
577 template<typename _Alloc>
578 tuple(allocator_arg_t __tag, const _Alloc& __a,
579 const _T1& __a1, const _T2& __a2)
580 : _Inherited(__tag, __a, __a1, __a2) { }
582 template<typename _Alloc, typename _U1, typename _U2>
583 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
584 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
585 std::forward<_U2>(__a2)) { }
587 template<typename _Alloc>
588 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
589 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
591 template<typename _Alloc>
592 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
593 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
595 template<typename _Alloc, typename _U1, typename _U2>
596 tuple(allocator_arg_t __tag, const _Alloc& __a,
597 const tuple<_U1, _U2>& __in)
598 : _Inherited(__tag, __a,
599 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
602 template<typename _Alloc, typename _U1, typename _U2>
603 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
604 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
607 template<typename _Alloc, typename _U1, typename _U2>
608 tuple(allocator_arg_t __tag, const _Alloc& __a,
609 const pair<_U1, _U2>& __in)
610 : _Inherited(__tag, __a, __in.first, __in.second) { }
612 template<typename _Alloc, typename _U1, typename _U2>
613 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
614 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
615 std::forward<_U2>(__in.second)) { }
618 operator=(const tuple& __in)
620 static_cast<_Inherited&>(*this) = __in;
625 operator=(tuple&& __in)
626 noexcept(is_nothrow_move_assignable<_Inherited>::value)
628 static_cast<_Inherited&>(*this) = std::move(__in);
632 template<typename _U1, typename _U2>
634 operator=(const tuple<_U1, _U2>& __in)
636 static_cast<_Inherited&>(*this) = __in;
640 template<typename _U1, typename _U2>
642 operator=(tuple<_U1, _U2>&& __in)
644 static_cast<_Inherited&>(*this) = std::move(__in);
648 template<typename _U1, typename _U2>
650 operator=(const pair<_U1, _U2>& __in)
652 this->_M_head(*this) = __in.first;
653 this->_M_tail(*this)._M_head(*this) = __in.second;
657 template<typename _U1, typename _U2>
659 operator=(pair<_U1, _U2>&& __in)
661 this->_M_head(*this) = std::forward<_U1>(__in.first);
662 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
668 noexcept(noexcept(__in._M_swap(__in)))
669 { _Inherited::_M_swap(__in); }
673 /// Gives the type of the ith element of a given tuple type.
674 template<std::size_t __i, typename _Tp>
675 struct tuple_element;
678 * Recursive case for tuple_element: strip off the first element in
679 * the tuple and retrieve the (i-1)th element of the remaining tuple.
681 template<std::size_t __i, typename _Head, typename... _Tail>
682 struct tuple_element<__i, tuple<_Head, _Tail...> >
683 : tuple_element<__i - 1, tuple<_Tail...> > { };
686 * Basis case for tuple_element: The first element is the one we're seeking.
688 template<typename _Head, typename... _Tail>
689 struct tuple_element<0, tuple<_Head, _Tail...> >
694 template<std::size_t __i, typename _Tp>
695 struct tuple_element<__i, const _Tp>
698 add_const<typename tuple_element<__i, _Tp>::type>::type type;
701 template<std::size_t __i, typename _Tp>
702 struct tuple_element<__i, volatile _Tp>
705 add_volatile<typename tuple_element<__i, _Tp>::type>::type type;
708 template<std::size_t __i, typename _Tp>
709 struct tuple_element<__i, const volatile _Tp>
712 add_cv<typename tuple_element<__i, _Tp>::type>::type type;
715 #if __cplusplus > 201103L
716 template<std::size_t __i, typename _Tp>
717 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
720 /// Finds the size of a given tuple type.
721 template<typename _Tp>
724 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725 // 2313. tuple_size should always derive from integral_constant<size_t, N>
726 template<typename _Tp>
727 struct tuple_size<const _Tp>
728 : public integral_constant<size_t, tuple_size<_Tp>::value> { };
730 template<typename _Tp>
731 struct tuple_size<volatile _Tp>
732 : public integral_constant<size_t, tuple_size<_Tp>::value> { };
734 template<typename _Tp>
735 struct tuple_size<const volatile _Tp>
736 : public integral_constant<size_t, tuple_size<_Tp>::value> { };
739 template<typename... _Elements>
740 struct tuple_size<tuple<_Elements...>>
741 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
743 template<std::size_t __i, typename _Head, typename... _Tail>
744 constexpr typename __add_ref<_Head>::type
745 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
746 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
748 template<std::size_t __i, typename _Head, typename... _Tail>
749 constexpr typename __add_c_ref<_Head>::type
750 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
751 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
753 /// Return a reference to the ith element of a tuple.
754 template<std::size_t __i, typename... _Elements>
755 constexpr typename __add_ref<
756 typename tuple_element<__i, tuple<_Elements...>>::type
758 get(tuple<_Elements...>& __t) noexcept
759 { return std::__get_helper<__i>(__t); }
761 /// Return a const reference to the ith element of a const tuple.
762 template<std::size_t __i, typename... _Elements>
763 constexpr typename __add_c_ref<
764 typename tuple_element<__i, tuple<_Elements...>>::type
766 get(const tuple<_Elements...>& __t) noexcept
767 { return std::__get_helper<__i>(__t); }
769 /// Return an rvalue reference to the ith element of a tuple rvalue.
770 template<std::size_t __i, typename... _Elements>
771 constexpr typename __add_r_ref<
772 typename tuple_element<__i, tuple<_Elements...>>::type
774 get(tuple<_Elements...>&& __t) noexcept
775 { return std::forward<typename tuple_element<__i,
776 tuple<_Elements...>>::type&&>(get<__i>(__t)); }
778 #if __cplusplus > 201103L
780 #define __cpp_lib_tuples_by_type 201304
782 template<typename _Head, size_t __i, typename... _Tail>
783 constexpr typename __add_ref<_Head>::type
784 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
785 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
787 template<typename _Head, size_t __i, typename... _Tail>
788 constexpr typename __add_c_ref<_Head>::type
789 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
790 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
792 /// Return a reference to the unique element of type _Tp of a tuple.
793 template <typename _Tp, typename... _Types>
795 get(tuple<_Types...>& __t) noexcept
796 { return std::__get_helper2<_Tp>(__t); }
798 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
799 template <typename _Tp, typename... _Types>
801 get(tuple<_Types...>&& __t) noexcept
802 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
804 /// Return a const reference to the unique element of type _Tp of a tuple.
805 template <typename _Tp, typename... _Types>
807 get(const tuple<_Types...>& __t) noexcept
808 { return std::__get_helper2<_Tp>(__t); }
812 // This class helps construct the various comparison operations on tuples
813 template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
814 typename _Tp, typename _Up>
815 struct __tuple_compare;
817 template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
818 struct __tuple_compare<0, __i, __j, _Tp, _Up>
820 static constexpr bool
821 __eq(const _Tp& __t, const _Up& __u)
823 return (get<__i>(__t) == get<__i>(__u) &&
824 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
827 static constexpr bool
828 __less(const _Tp& __t, const _Up& __u)
830 return ((get<__i>(__t) < get<__i>(__u))
831 || !(get<__i>(__u) < get<__i>(__t)) &&
832 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
836 template<std::size_t __i, typename _Tp, typename _Up>
837 struct __tuple_compare<0, __i, __i, _Tp, _Up>
839 static constexpr bool
840 __eq(const _Tp&, const _Up&) { return true; }
842 static constexpr bool
843 __less(const _Tp&, const _Up&) { return false; }
846 template<typename... _TElements, typename... _UElements>
848 operator==(const tuple<_TElements...>& __t,
849 const tuple<_UElements...>& __u)
851 typedef tuple<_TElements...> _Tp;
852 typedef tuple<_UElements...> _Up;
853 return bool(__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
854 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
857 template<typename... _TElements, typename... _UElements>
859 operator<(const tuple<_TElements...>& __t,
860 const tuple<_UElements...>& __u)
862 typedef tuple<_TElements...> _Tp;
863 typedef tuple<_UElements...> _Up;
864 return bool(__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
865 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
868 template<typename... _TElements, typename... _UElements>
870 operator!=(const tuple<_TElements...>& __t,
871 const tuple<_UElements...>& __u)
872 { return !(__t == __u); }
874 template<typename... _TElements, typename... _UElements>
876 operator>(const tuple<_TElements...>& __t,
877 const tuple<_UElements...>& __u)
878 { return __u < __t; }
880 template<typename... _TElements, typename... _UElements>
882 operator<=(const tuple<_TElements...>& __t,
883 const tuple<_UElements...>& __u)
884 { return !(__u < __t); }
886 template<typename... _TElements, typename... _UElements>
888 operator>=(const tuple<_TElements...>& __t,
889 const tuple<_UElements...>& __u)
890 { return !(__t < __u); }
893 template<typename... _Elements>
894 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
895 make_tuple(_Elements&&... __args)
897 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
899 return __result_type(std::forward<_Elements>(__args)...);
902 template<typename... _Elements>
903 tuple<_Elements&&...>
904 forward_as_tuple(_Elements&&... __args) noexcept
905 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
908 struct __is_tuple_like_impl : false_type
911 template<typename... _Tps>
912 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
915 template<typename _T1, typename _T2>
916 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
919 template<typename _Tp, std::size_t _Nm>
920 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
923 // Internal type trait that allows us to sfinae-protect tuple_cat.
924 template<typename _Tp>
925 struct __is_tuple_like
926 : public __is_tuple_like_impl<typename std::remove_cv
927 <typename std::remove_reference<_Tp>::type>::type>::type
930 template<std::size_t, typename, typename, std::size_t>
931 struct __make_tuple_impl;
933 template<std::size_t _Idx, typename _Tuple, typename... _Tp,
935 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
937 typedef typename __make_tuple_impl<_Idx + 1, tuple<_Tp...,
938 typename std::tuple_element<_Idx, _Tuple>::type>, _Tuple, _Nm>::__type
942 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
943 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
945 typedef tuple<_Tp...> __type;
948 template<typename _Tuple>
949 struct __do_make_tuple
950 : public __make_tuple_impl<0, tuple<>, _Tuple,
951 std::tuple_size<_Tuple>::value>
954 // Returns the std::tuple equivalent of a tuple-like type.
955 template<typename _Tuple>
957 : public __do_make_tuple<typename std::remove_cv
958 <typename std::remove_reference<_Tuple>::type>::type>
961 // Combines several std::tuple's into a single one.
962 template<typename...>
963 struct __combine_tuples;
966 struct __combine_tuples<>
968 typedef tuple<> __type;
971 template<typename... _Ts>
972 struct __combine_tuples<tuple<_Ts...>>
974 typedef tuple<_Ts...> __type;
977 template<typename... _T1s, typename... _T2s, typename... _Rem>
978 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
980 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
981 _Rem...>::__type __type;
984 // Computes the result type of tuple_cat given a set of tuple-like types.
985 template<typename... _Tpls>
986 struct __tuple_cat_result
988 typedef typename __combine_tuples
989 <typename __make_tuple<_Tpls>::__type...>::__type __type;
992 // Helper to determine the index set for the first tuple-like
993 // type of a given set.
994 template<typename...>
995 struct __make_1st_indices;
998 struct __make_1st_indices<>
1000 typedef std::_Index_tuple<> __type;
1003 template<typename _Tp, typename... _Tpls>
1004 struct __make_1st_indices<_Tp, _Tpls...>
1006 typedef typename std::_Build_index_tuple<std::tuple_size<
1007 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1010 // Performs the actual concatenation by step-wise expanding tuple-like
1011 // objects into the elements, which are finally forwarded into the
1013 template<typename _Ret, typename _Indices, typename... _Tpls>
1014 struct __tuple_concater;
1016 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1017 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1019 template<typename... _Us>
1020 static constexpr _Ret
1021 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1023 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1024 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
1025 return __next::_S_do(std::forward<_Tpls>(__tps)...,
1026 std::forward<_Us>(__us)...,
1027 std::get<_Is>(std::forward<_Tp>(__tp))...);
1031 template<typename _Ret>
1032 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1034 template<typename... _Us>
1035 static constexpr _Ret
1036 _S_do(_Us&&... __us)
1038 return _Ret(std::forward<_Us>(__us)...);
1043 template<typename... _Tpls, typename = typename
1044 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1046 tuple_cat(_Tpls&&... __tpls)
1047 -> typename __tuple_cat_result<_Tpls...>::__type
1049 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1050 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1051 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1052 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1056 template<typename... _Elements>
1057 inline tuple<_Elements&...>
1058 tie(_Elements&... __args) noexcept
1059 { return tuple<_Elements&...>(__args...); }
1062 template<typename... _Elements>
1064 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1065 noexcept(noexcept(__x.swap(__y)))
1068 // A class (and instance) which can be used in 'tie' when an element
1069 // of a tuple is not required
1070 struct _Swallow_assign
1073 const _Swallow_assign&
1074 operator=(const _Tp&) const
1078 const _Swallow_assign ignore{};
1080 /// Partial specialization for tuples
1081 template<typename... _Types, typename _Alloc>
1082 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1084 // See stl_pair.h...
1085 template<class _T1, class _T2>
1086 template<typename... _Args1, typename... _Args2>
1089 pair(piecewise_construct_t,
1090 tuple<_Args1...> __first, tuple<_Args2...> __second)
1091 : pair(__first, __second,
1092 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1093 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1096 template<class _T1, class _T2>
1097 template<typename... _Args1, std::size_t... _Indexes1,
1098 typename... _Args2, std::size_t... _Indexes2>
1101 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1102 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1103 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1104 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1109 _GLIBCXX_END_NAMESPACE_VERSION
1114 #endif // _GLIBCXX_TUPLE