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 template<std::size_t _Idx, typename _Head, bool _IsEmptyNotFinal>
54 template<std::size_t _Idx, typename _Head>
55 struct _Head_base<_Idx, _Head, true>
58 constexpr _Head_base()
61 constexpr _Head_base(const _Head& __h)
64 template<typename _UHead, typename = typename
65 enable_if<!is_convertible<_UHead,
66 __uses_alloc_base>::value>::type>
67 constexpr _Head_base(_UHead&& __h)
68 : _Head(std::forward<_UHead>(__h)) { }
70 _Head_base(__uses_alloc0)
73 template<typename _Alloc>
74 _Head_base(__uses_alloc1<_Alloc> __a)
75 : _Head(allocator_arg, *__a._M_a) { }
77 template<typename _Alloc>
78 _Head_base(__uses_alloc2<_Alloc> __a)
79 : _Head(*__a._M_a) { }
81 template<typename _UHead>
82 _Head_base(__uses_alloc0, _UHead&& __uhead)
83 : _Head(std::forward<_UHead>(__uhead)) { }
85 template<typename _Alloc, typename _UHead>
86 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
87 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
89 template<typename _Alloc, typename _UHead>
90 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
91 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
93 static constexpr _Head&
94 _M_head(_Head_base& __b) noexcept { return __b; }
96 static constexpr const _Head&
97 _M_head(const _Head_base& __b) noexcept { return __b; }
100 template<std::size_t _Idx, typename _Head>
101 struct _Head_base<_Idx, _Head, false>
103 constexpr _Head_base()
106 constexpr _Head_base(const _Head& __h)
107 : _M_head_impl(__h) { }
109 template<typename _UHead, typename = typename
110 enable_if<!is_convertible<_UHead,
111 __uses_alloc_base>::value>::type>
112 constexpr _Head_base(_UHead&& __h)
113 : _M_head_impl(std::forward<_UHead>(__h)) { }
115 _Head_base(__uses_alloc0)
118 template<typename _Alloc>
119 _Head_base(__uses_alloc1<_Alloc> __a)
120 : _M_head_impl(allocator_arg, *__a._M_a) { }
122 template<typename _Alloc>
123 _Head_base(__uses_alloc2<_Alloc> __a)
124 : _M_head_impl(*__a._M_a) { }
126 template<typename _UHead>
127 _Head_base(__uses_alloc0, _UHead&& __uhead)
128 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
130 template<typename _Alloc, typename _UHead>
131 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
132 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
135 template<typename _Alloc, typename _UHead>
136 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
137 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
139 static constexpr _Head&
140 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
142 static constexpr const _Head&
143 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
149 * Contains the actual implementation of the @c tuple template, stored
150 * as a recursive inheritance hierarchy from the first element (most
151 * derived class) to the last (least derived class). The @c Idx
152 * parameter gives the 0-based index of the element stored at this
153 * point in the hierarchy; we use it to implement a constant-time
156 template<std::size_t _Idx, typename... _Elements>
160 * Zero-element tuple implementation. This is the basis case for the
161 * inheritance recursion.
163 template<std::size_t _Idx>
164 struct _Tuple_impl<_Idx>
166 template<std::size_t, typename...> friend class _Tuple_impl;
168 _Tuple_impl() = default;
170 template<typename _Alloc>
171 _Tuple_impl(allocator_arg_t, const _Alloc&) { }
173 template<typename _Alloc>
174 _Tuple_impl(allocator_arg_t, const _Alloc&, const _Tuple_impl&) { }
176 template<typename _Alloc>
177 _Tuple_impl(allocator_arg_t, const _Alloc&, _Tuple_impl&&) { }
180 void _M_swap(_Tuple_impl&) noexcept { /* no-op */ }
183 template<typename _Tp>
184 struct __is_empty_non_tuple : is_empty<_Tp> { };
186 // Using EBO for elements that are tuples causes ambiguous base errors.
187 template<typename _El0, typename... _El>
188 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
190 // Use the Empty Base-class Optimization for empty, non-final types.
191 template<typename _Tp>
192 using __empty_not_final
193 = typename conditional<__is_final(_Tp), false_type,
194 __is_empty_non_tuple<_Tp>>::type;
197 * Recursive tuple implementation. Here we store the @c Head element
198 * and derive from a @c Tuple_impl containing the remaining elements
199 * (which contains the @c Tail).
201 template<std::size_t _Idx, typename _Head, typename... _Tail>
202 struct _Tuple_impl<_Idx, _Head, _Tail...>
203 : public _Tuple_impl<_Idx + 1, _Tail...>,
204 private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
206 template<std::size_t, typename...> friend class _Tuple_impl;
208 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
209 typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
211 static constexpr _Head&
212 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
214 static constexpr const _Head&
215 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
217 static constexpr _Inherited&
218 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
220 static constexpr const _Inherited&
221 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
223 constexpr _Tuple_impl()
224 : _Inherited(), _Base() { }
227 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
228 : _Inherited(__tail...), _Base(__head) { }
230 template<typename _UHead, typename... _UTail, typename = typename
231 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
233 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
234 : _Inherited(std::forward<_UTail>(__tail)...),
235 _Base(std::forward<_UHead>(__head)) { }
237 constexpr _Tuple_impl(const _Tuple_impl&) = default;
240 _Tuple_impl(_Tuple_impl&& __in)
241 noexcept(__and_<is_nothrow_move_constructible<_Head>,
242 is_nothrow_move_constructible<_Inherited>>::value)
243 : _Inherited(std::move(_M_tail(__in))),
244 _Base(std::forward<_Head>(_M_head(__in))) { }
246 template<typename... _UElements>
247 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
248 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
249 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
251 template<typename _UHead, typename... _UTails>
252 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
253 : _Inherited(std::move
254 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
255 _Base(std::forward<_UHead>
256 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
258 template<typename _Alloc>
259 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
260 : _Inherited(__tag, __a),
261 _Base(__use_alloc<_Head>(__a)) { }
263 template<typename _Alloc>
264 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
265 const _Head& __head, const _Tail&... __tail)
266 : _Inherited(__tag, __a, __tail...),
267 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
269 template<typename _Alloc, typename _UHead, typename... _UTail,
270 typename = typename enable_if<sizeof...(_Tail)
271 == sizeof...(_UTail)>::type>
272 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
273 _UHead&& __head, _UTail&&... __tail)
274 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
275 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
276 std::forward<_UHead>(__head)) { }
278 template<typename _Alloc>
279 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
280 const _Tuple_impl& __in)
281 : _Inherited(__tag, __a, _M_tail(__in)),
282 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
284 template<typename _Alloc>
285 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
287 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
288 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
289 std::forward<_Head>(_M_head(__in))) { }
291 template<typename _Alloc, typename... _UElements>
292 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
293 const _Tuple_impl<_Idx, _UElements...>& __in)
294 : _Inherited(__tag, __a,
295 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
296 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
297 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
299 template<typename _Alloc, typename _UHead, typename... _UTails>
300 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
301 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
302 : _Inherited(__tag, __a, std::move
303 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
304 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
306 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
309 operator=(const _Tuple_impl& __in)
311 _M_head(*this) = _M_head(__in);
312 _M_tail(*this) = _M_tail(__in);
317 operator=(_Tuple_impl&& __in)
318 noexcept(__and_<is_nothrow_move_assignable<_Head>,
319 is_nothrow_move_assignable<_Inherited>>::value)
321 _M_head(*this) = std::forward<_Head>(_M_head(__in));
322 _M_tail(*this) = std::move(_M_tail(__in));
326 template<typename... _UElements>
328 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
330 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
331 _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
335 template<typename _UHead, typename... _UTails>
337 operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
339 _M_head(*this) = std::forward<_UHead>
340 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
341 _M_tail(*this) = std::move
342 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
348 _M_swap(_Tuple_impl& __in)
349 noexcept(noexcept(swap(std::declval<_Head&>(),
350 std::declval<_Head&>()))
351 && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
354 swap(_M_head(*this), _M_head(__in));
355 _Inherited::_M_swap(_M_tail(__in));
359 /// Primary class template, tuple
360 template<typename... _Elements>
361 class tuple : public _Tuple_impl<0, _Elements...>
363 typedef _Tuple_impl<0, _Elements...> _Inherited;
370 constexpr tuple(const _Elements&... __elements)
371 : _Inherited(__elements...) { }
373 template<typename... _UElements, typename = typename
374 enable_if<__and_<is_convertible<_UElements,
375 _Elements>...>::value>::type>
377 constexpr tuple(_UElements&&... __elements)
378 : _Inherited(std::forward<_UElements>(__elements)...) { }
380 constexpr tuple(const tuple&) = default;
382 constexpr tuple(tuple&&) = default;
384 template<typename... _UElements, typename = typename
385 enable_if<__and_<is_convertible<const _UElements&,
386 _Elements>...>::value>::type>
387 constexpr tuple(const tuple<_UElements...>& __in)
388 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
391 template<typename... _UElements, typename = typename
392 enable_if<__and_<is_convertible<_UElements,
393 _Elements>...>::value>::type>
394 constexpr tuple(tuple<_UElements...>&& __in)
395 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
397 // Allocator-extended constructors.
399 template<typename _Alloc>
400 tuple(allocator_arg_t __tag, const _Alloc& __a)
401 : _Inherited(__tag, __a) { }
403 template<typename _Alloc>
404 tuple(allocator_arg_t __tag, const _Alloc& __a,
405 const _Elements&... __elements)
406 : _Inherited(__tag, __a, __elements...) { }
408 template<typename _Alloc, typename... _UElements, typename = typename
409 enable_if<sizeof...(_UElements)
410 == sizeof...(_Elements)>::type>
411 tuple(allocator_arg_t __tag, const _Alloc& __a,
412 _UElements&&... __elements)
413 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
416 template<typename _Alloc>
417 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
418 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
420 template<typename _Alloc>
421 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
422 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
424 template<typename _Alloc, typename... _UElements, typename = typename
425 enable_if<sizeof...(_UElements)
426 == sizeof...(_Elements)>::type>
427 tuple(allocator_arg_t __tag, const _Alloc& __a,
428 const tuple<_UElements...>& __in)
429 : _Inherited(__tag, __a,
430 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
433 template<typename _Alloc, typename... _UElements, typename = typename
434 enable_if<sizeof...(_UElements)
435 == sizeof...(_Elements)>::type>
436 tuple(allocator_arg_t __tag, const _Alloc& __a,
437 tuple<_UElements...>&& __in)
438 : _Inherited(__tag, __a,
439 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
443 operator=(const tuple& __in)
445 static_cast<_Inherited&>(*this) = __in;
450 operator=(tuple&& __in)
451 noexcept(is_nothrow_move_assignable<_Inherited>::value)
453 static_cast<_Inherited&>(*this) = std::move(__in);
457 template<typename... _UElements, typename = typename
458 enable_if<sizeof...(_UElements)
459 == sizeof...(_Elements)>::type>
461 operator=(const tuple<_UElements...>& __in)
463 static_cast<_Inherited&>(*this) = __in;
467 template<typename... _UElements, typename = typename
468 enable_if<sizeof...(_UElements)
469 == sizeof...(_Elements)>::type>
471 operator=(tuple<_UElements...>&& __in)
473 static_cast<_Inherited&>(*this) = std::move(__in);
479 noexcept(noexcept(__in._M_swap(__in)))
480 { _Inherited::_M_swap(__in); }
483 // Explicit specialization, zero-element tuple.
488 void swap(tuple&) noexcept { /* no-op */ }
491 /// Partial specialization, 2-element tuple.
492 /// Includes construction and assignment from a pair.
493 template<typename _T1, typename _T2>
494 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
496 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
503 constexpr tuple(const _T1& __a1, const _T2& __a2)
504 : _Inherited(__a1, __a2) { }
506 template<typename _U1, typename _U2, typename = typename
507 enable_if<__and_<is_convertible<_U1, _T1>,
508 is_convertible<_U2, _T2>>::value>::type>
510 constexpr tuple(_U1&& __a1, _U2&& __a2)
511 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
513 constexpr tuple(const tuple&) = default;
515 constexpr tuple(tuple&&) = default;
517 template<typename _U1, typename _U2, typename = typename
518 enable_if<__and_<is_convertible<const _U1&, _T1>,
519 is_convertible<const _U2&, _T2>>::value>::type>
520 constexpr tuple(const tuple<_U1, _U2>& __in)
521 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
523 template<typename _U1, typename _U2, typename = typename
524 enable_if<__and_<is_convertible<_U1, _T1>,
525 is_convertible<_U2, _T2>>::value>::type>
526 constexpr tuple(tuple<_U1, _U2>&& __in)
527 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
529 template<typename _U1, typename _U2, typename = typename
530 enable_if<__and_<is_convertible<const _U1&, _T1>,
531 is_convertible<const _U2&, _T2>>::value>::type>
532 constexpr tuple(const pair<_U1, _U2>& __in)
533 : _Inherited(__in.first, __in.second) { }
535 template<typename _U1, typename _U2, typename = typename
536 enable_if<__and_<is_convertible<_U1, _T1>,
537 is_convertible<_U2, _T2>>::value>::type>
538 constexpr tuple(pair<_U1, _U2>&& __in)
539 : _Inherited(std::forward<_U1>(__in.first),
540 std::forward<_U2>(__in.second)) { }
542 // Allocator-extended constructors.
544 template<typename _Alloc>
545 tuple(allocator_arg_t __tag, const _Alloc& __a)
546 : _Inherited(__tag, __a) { }
548 template<typename _Alloc>
549 tuple(allocator_arg_t __tag, const _Alloc& __a,
550 const _T1& __a1, const _T2& __a2)
551 : _Inherited(__tag, __a, __a1, __a2) { }
553 template<typename _Alloc, typename _U1, typename _U2>
554 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
555 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
556 std::forward<_U2>(__a2)) { }
558 template<typename _Alloc>
559 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
560 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
562 template<typename _Alloc>
563 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
564 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
566 template<typename _Alloc, typename _U1, typename _U2>
567 tuple(allocator_arg_t __tag, const _Alloc& __a,
568 const tuple<_U1, _U2>& __in)
569 : _Inherited(__tag, __a,
570 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
573 template<typename _Alloc, typename _U1, typename _U2>
574 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
575 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
578 template<typename _Alloc, typename _U1, typename _U2>
579 tuple(allocator_arg_t __tag, const _Alloc& __a,
580 const pair<_U1, _U2>& __in)
581 : _Inherited(__tag, __a, __in.first, __in.second) { }
583 template<typename _Alloc, typename _U1, typename _U2>
584 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
585 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
586 std::forward<_U2>(__in.second)) { }
589 operator=(const tuple& __in)
591 static_cast<_Inherited&>(*this) = __in;
596 operator=(tuple&& __in)
597 noexcept(is_nothrow_move_assignable<_Inherited>::value)
599 static_cast<_Inherited&>(*this) = std::move(__in);
603 template<typename _U1, typename _U2>
605 operator=(const tuple<_U1, _U2>& __in)
607 static_cast<_Inherited&>(*this) = __in;
611 template<typename _U1, typename _U2>
613 operator=(tuple<_U1, _U2>&& __in)
615 static_cast<_Inherited&>(*this) = std::move(__in);
619 template<typename _U1, typename _U2>
621 operator=(const pair<_U1, _U2>& __in)
623 this->_M_head(*this) = __in.first;
624 this->_M_tail(*this)._M_head(*this) = __in.second;
628 template<typename _U1, typename _U2>
630 operator=(pair<_U1, _U2>&& __in)
632 this->_M_head(*this) = std::forward<_U1>(__in.first);
633 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
639 noexcept(noexcept(__in._M_swap(__in)))
640 { _Inherited::_M_swap(__in); }
644 /// Gives the type of the ith element of a given tuple type.
645 template<std::size_t __i, typename _Tp>
646 struct tuple_element;
649 * Recursive case for tuple_element: strip off the first element in
650 * the tuple and retrieve the (i-1)th element of the remaining tuple.
652 template<std::size_t __i, typename _Head, typename... _Tail>
653 struct tuple_element<__i, tuple<_Head, _Tail...> >
654 : tuple_element<__i - 1, tuple<_Tail...> > { };
657 * Basis case for tuple_element: The first element is the one we're seeking.
659 template<typename _Head, typename... _Tail>
660 struct tuple_element<0, tuple<_Head, _Tail...> >
665 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
666 template<std::size_t __i, typename _Tp>
667 using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
669 template<std::size_t __i, typename _Tp>
670 struct tuple_element<__i, const _Tp>
672 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
675 template<std::size_t __i, typename _Tp>
676 struct tuple_element<__i, volatile _Tp>
678 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
681 template<std::size_t __i, typename _Tp>
682 struct tuple_element<__i, const volatile _Tp>
684 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
687 #if __cplusplus > 201103L
688 template<std::size_t __i, typename _Tp>
689 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
692 /// Finds the size of a given tuple type.
693 template<typename _Tp>
696 template<typename _Tp, typename _Ts = tuple_size<_Tp>>
697 using __cv_tuple_size = integral_constant<
698 typename remove_cv<decltype(_Ts::value)>::type, _Ts::value>;
700 template<typename _Tp>
701 struct tuple_size<const _Tp> : __cv_tuple_size<_Tp> { };
703 template<typename _Tp>
704 struct tuple_size<volatile _Tp> : __cv_tuple_size<_Tp> { };
706 template<typename _Tp>
707 struct tuple_size<const volatile _Tp> : __cv_tuple_size<_Tp> { };
710 template<typename... _Elements>
711 struct tuple_size<tuple<_Elements...>>
712 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
714 template<std::size_t __i, typename _Head, typename... _Tail>
716 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
717 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
719 template<std::size_t __i, typename _Head, typename... _Tail>
720 constexpr const _Head&
721 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
722 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
724 /// Return a reference to the ith element of a tuple.
725 template<std::size_t __i, typename... _Elements>
726 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
727 get(tuple<_Elements...>& __t) noexcept
728 { return std::__get_helper<__i>(__t); }
730 /// Return a const reference to the ith element of a const tuple.
731 template<std::size_t __i, typename... _Elements>
732 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
733 get(const tuple<_Elements...>& __t) noexcept
734 { return std::__get_helper<__i>(__t); }
736 /// Return an rvalue reference to the ith element of a tuple rvalue.
737 template<std::size_t __i, typename... _Elements>
738 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
739 get(tuple<_Elements...>&& __t) noexcept
741 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
742 return std::forward<__element_type&&>(std::get<__i>(__t));
745 #if __cplusplus > 201103L
746 template<typename _Head, size_t __i, typename... _Tail>
748 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
749 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
751 template<typename _Head, size_t __i, typename... _Tail>
752 constexpr const _Head&
753 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
754 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
756 /// Return a reference to the unique element of type _Tp of a tuple.
757 template <typename _Tp, typename... _Types>
759 get(tuple<_Types...>& __t) noexcept
760 { return std::__get_helper2<_Tp>(__t); }
762 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
763 template <typename _Tp, typename... _Types>
765 get(tuple<_Types...>&& __t) noexcept
766 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
768 /// Return a const reference to the unique element of type _Tp of a tuple.
769 template <typename _Tp, typename... _Types>
771 get(const tuple<_Types...>& __t) noexcept
772 { return std::__get_helper2<_Tp>(__t); }
775 // This class performs the comparison operations on tuples
776 template<typename _Tp, typename _Up, size_t __i, size_t __size>
777 struct __tuple_compare
779 static constexpr bool
780 __eq(const _Tp& __t, const _Up& __u)
782 return bool(std::get<__i>(__t) == std::get<__i>(__u))
783 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
786 static constexpr bool
787 __less(const _Tp& __t, const _Up& __u)
789 return bool(std::get<__i>(__t) < std::get<__i>(__u))
790 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
791 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
795 template<typename _Tp, typename _Up, size_t __size>
796 struct __tuple_compare<_Tp, _Up, __size, __size>
798 static constexpr bool
799 __eq(const _Tp&, const _Up&) { return true; }
801 static constexpr bool
802 __less(const _Tp&, const _Up&) { return false; }
805 template<typename... _TElements, typename... _UElements>
807 operator==(const tuple<_TElements...>& __t,
808 const tuple<_UElements...>& __u)
810 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
811 "tuple objects can only be compared if they have equal sizes.");
812 using __compare = __tuple_compare<tuple<_TElements...>,
813 tuple<_UElements...>,
814 0, sizeof...(_TElements)>;
815 return __compare::__eq(__t, __u);
818 template<typename... _TElements, typename... _UElements>
820 operator<(const tuple<_TElements...>& __t,
821 const tuple<_UElements...>& __u)
823 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
824 "tuple objects can only be compared if they have equal sizes.");
825 using __compare = __tuple_compare<tuple<_TElements...>,
826 tuple<_UElements...>,
827 0, sizeof...(_TElements)>;
828 return __compare::__less(__t, __u);
831 template<typename... _TElements, typename... _UElements>
833 operator!=(const tuple<_TElements...>& __t,
834 const tuple<_UElements...>& __u)
835 { return !(__t == __u); }
837 template<typename... _TElements, typename... _UElements>
839 operator>(const tuple<_TElements...>& __t,
840 const tuple<_UElements...>& __u)
841 { return __u < __t; }
843 template<typename... _TElements, typename... _UElements>
845 operator<=(const tuple<_TElements...>& __t,
846 const tuple<_UElements...>& __u)
847 { return !(__u < __t); }
849 template<typename... _TElements, typename... _UElements>
851 operator>=(const tuple<_TElements...>& __t,
852 const tuple<_UElements...>& __u)
853 { return !(__t < __u); }
856 template<typename... _Elements>
857 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
858 make_tuple(_Elements&&... __args)
860 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
862 return __result_type(std::forward<_Elements>(__args)...);
865 template<typename... _Elements>
866 tuple<_Elements&&...>
867 forward_as_tuple(_Elements&&... __args) noexcept
868 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
871 struct __is_tuple_like_impl : false_type
874 template<typename... _Tps>
875 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
878 template<typename _T1, typename _T2>
879 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
882 template<typename _Tp, std::size_t _Nm>
883 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
886 // Internal type trait that allows us to sfinae-protect tuple_cat.
887 template<typename _Tp>
888 struct __is_tuple_like
889 : public __is_tuple_like_impl<typename std::remove_cv
890 <typename std::remove_reference<_Tp>::type>::type>::type
893 template<size_t, typename, typename, size_t>
894 struct __make_tuple_impl;
896 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
897 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
898 : __make_tuple_impl<_Idx + 1,
899 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
903 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
904 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
906 typedef tuple<_Tp...> __type;
909 template<typename _Tuple>
910 struct __do_make_tuple
911 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
914 // Returns the std::tuple equivalent of a tuple-like type.
915 template<typename _Tuple>
917 : public __do_make_tuple<typename std::remove_cv
918 <typename std::remove_reference<_Tuple>::type>::type>
921 // Combines several std::tuple's into a single one.
922 template<typename...>
923 struct __combine_tuples;
926 struct __combine_tuples<>
928 typedef tuple<> __type;
931 template<typename... _Ts>
932 struct __combine_tuples<tuple<_Ts...>>
934 typedef tuple<_Ts...> __type;
937 template<typename... _T1s, typename... _T2s, typename... _Rem>
938 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
940 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
941 _Rem...>::__type __type;
944 // Computes the result type of tuple_cat given a set of tuple-like types.
945 template<typename... _Tpls>
946 struct __tuple_cat_result
948 typedef typename __combine_tuples
949 <typename __make_tuple<_Tpls>::__type...>::__type __type;
952 // Helper to determine the index set for the first tuple-like
953 // type of a given set.
954 template<typename...>
955 struct __make_1st_indices;
958 struct __make_1st_indices<>
960 typedef std::_Index_tuple<> __type;
963 template<typename _Tp, typename... _Tpls>
964 struct __make_1st_indices<_Tp, _Tpls...>
966 typedef typename std::_Build_index_tuple<std::tuple_size<
967 typename std::remove_reference<_Tp>::type>::value>::__type __type;
970 // Performs the actual concatenation by step-wise expanding tuple-like
971 // objects into the elements, which are finally forwarded into the
973 template<typename _Ret, typename _Indices, typename... _Tpls>
974 struct __tuple_concater;
976 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
977 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
979 template<typename... _Us>
980 static constexpr _Ret
981 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
983 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
984 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
985 return __next::_S_do(std::forward<_Tpls>(__tps)...,
986 std::forward<_Us>(__us)...,
987 std::get<_Is>(std::forward<_Tp>(__tp))...);
991 template<typename _Ret>
992 struct __tuple_concater<_Ret, std::_Index_tuple<>>
994 template<typename... _Us>
995 static constexpr _Ret
998 return _Ret(std::forward<_Us>(__us)...);
1003 template<typename... _Tpls, typename = typename
1004 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1006 tuple_cat(_Tpls&&... __tpls)
1007 -> typename __tuple_cat_result<_Tpls...>::__type
1009 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1010 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1011 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1012 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1016 template<typename... _Elements>
1017 inline tuple<_Elements&...>
1018 tie(_Elements&... __args) noexcept
1019 { return tuple<_Elements&...>(__args...); }
1022 template<typename... _Elements>
1024 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1025 noexcept(noexcept(__x.swap(__y)))
1028 // A class (and instance) which can be used in 'tie' when an element
1029 // of a tuple is not required
1030 struct _Swallow_assign
1033 const _Swallow_assign&
1034 operator=(const _Tp&) const
1038 const _Swallow_assign ignore{};
1040 /// Partial specialization for tuples
1041 template<typename... _Types, typename _Alloc>
1042 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1044 // See stl_pair.h...
1045 template<class _T1, class _T2>
1046 template<typename... _Args1, typename... _Args2>
1049 pair(piecewise_construct_t,
1050 tuple<_Args1...> __first, tuple<_Args2...> __second)
1051 : pair(__first, __second,
1052 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1053 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1056 template<class _T1, class _T2>
1057 template<typename... _Args1, std::size_t... _Indexes1,
1058 typename... _Args2, std::size_t... _Indexes2>
1061 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1062 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1063 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1064 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1069 _GLIBCXX_END_NAMESPACE_VERSION
1074 #endif // _GLIBCXX_TUPLE