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 constexpr _Head_base(const _Head_base&) = default;
65 constexpr _Head_base(_Head_base&&) = default;
67 template<typename _UHead>
68 constexpr _Head_base(_UHead&& __h)
69 : _Head(std::forward<_UHead>(__h)) { }
71 _Head_base(allocator_arg_t, __uses_alloc0)
74 template<typename _Alloc>
75 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
76 : _Head(allocator_arg, *__a._M_a) { }
78 template<typename _Alloc>
79 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
80 : _Head(*__a._M_a) { }
82 template<typename _UHead>
83 _Head_base(__uses_alloc0, _UHead&& __uhead)
84 : _Head(std::forward<_UHead>(__uhead)) { }
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)) { }
90 template<typename _Alloc, typename _UHead>
91 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
92 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
94 static constexpr _Head&
95 _M_head(_Head_base& __b) noexcept { return __b; }
97 static constexpr const _Head&
98 _M_head(const _Head_base& __b) noexcept { return __b; }
101 template<std::size_t _Idx, typename _Head>
102 struct _Head_base<_Idx, _Head, false>
104 constexpr _Head_base()
107 constexpr _Head_base(const _Head& __h)
108 : _M_head_impl(__h) { }
110 constexpr _Head_base(const _Head_base&) = default;
111 constexpr _Head_base(_Head_base&&) = default;
113 template<typename _UHead>
114 constexpr _Head_base(_UHead&& __h)
115 : _M_head_impl(std::forward<_UHead>(__h)) { }
117 _Head_base(allocator_arg_t, __uses_alloc0)
120 template<typename _Alloc>
121 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
122 : _M_head_impl(allocator_arg, *__a._M_a) { }
124 template<typename _Alloc>
125 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
126 : _M_head_impl(*__a._M_a) { }
128 template<typename _UHead>
129 _Head_base(__uses_alloc0, _UHead&& __uhead)
130 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
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))
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) { }
141 static constexpr _Head&
142 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
144 static constexpr const _Head&
145 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
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
158 template<std::size_t _Idx, typename... _Elements>
162 * Zero-element tuple implementation. This is the basis case for the
163 * inheritance recursion.
165 template<std::size_t _Idx>
166 struct _Tuple_impl<_Idx>
168 template<std::size_t, typename...> friend class _Tuple_impl;
170 _Tuple_impl() = default;
172 template<typename _Alloc>
173 _Tuple_impl(allocator_arg_t, const _Alloc&) { }
175 template<typename _Alloc>
176 _Tuple_impl(allocator_arg_t, const _Alloc&, const _Tuple_impl&) { }
178 template<typename _Alloc>
179 _Tuple_impl(allocator_arg_t, const _Alloc&, _Tuple_impl&&) { }
182 void _M_swap(_Tuple_impl&) noexcept { /* no-op */ }
185 template<typename _Tp>
186 struct __is_empty_non_tuple : is_empty<_Tp> { };
188 // Using EBO for elements that are tuples causes ambiguous base errors.
189 template<typename _El0, typename... _El>
190 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
192 // Use the Empty Base-class Optimization for empty, non-final types.
193 template<typename _Tp>
194 using __empty_not_final
195 = typename conditional<__is_final(_Tp), false_type,
196 __is_empty_non_tuple<_Tp>>::type;
199 * Recursive tuple implementation. Here we store the @c Head element
200 * and derive from a @c Tuple_impl containing the remaining elements
201 * (which contains the @c Tail).
203 template<std::size_t _Idx, typename _Head, typename... _Tail>
204 struct _Tuple_impl<_Idx, _Head, _Tail...>
205 : public _Tuple_impl<_Idx + 1, _Tail...>,
206 private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
208 template<std::size_t, typename...> friend class _Tuple_impl;
210 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
211 typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
213 static constexpr _Head&
214 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
216 static constexpr const _Head&
217 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
219 static constexpr _Inherited&
220 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
222 static constexpr const _Inherited&
223 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
225 constexpr _Tuple_impl()
226 : _Inherited(), _Base() { }
229 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
230 : _Inherited(__tail...), _Base(__head) { }
232 template<typename _UHead, typename... _UTail, typename = typename
233 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
235 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
236 : _Inherited(std::forward<_UTail>(__tail)...),
237 _Base(std::forward<_UHead>(__head)) { }
239 constexpr _Tuple_impl(const _Tuple_impl&) = default;
242 _Tuple_impl(_Tuple_impl&& __in)
243 noexcept(__and_<is_nothrow_move_constructible<_Head>,
244 is_nothrow_move_constructible<_Inherited>>::value)
245 : _Inherited(std::move(_M_tail(__in))),
246 _Base(std::forward<_Head>(_M_head(__in))) { }
248 template<typename... _UElements>
249 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
250 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
251 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
253 template<typename _UHead, typename... _UTails>
254 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
255 : _Inherited(std::move
256 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
257 _Base(std::forward<_UHead>
258 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
260 template<typename _Alloc>
261 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
262 : _Inherited(__tag, __a),
263 _Base(__tag, __use_alloc<_Head>(__a)) { }
265 template<typename _Alloc>
266 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
267 const _Head& __head, const _Tail&... __tail)
268 : _Inherited(__tag, __a, __tail...),
269 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
271 template<typename _Alloc, typename _UHead, typename... _UTail,
272 typename = typename enable_if<sizeof...(_Tail)
273 == sizeof...(_UTail)>::type>
274 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
275 _UHead&& __head, _UTail&&... __tail)
276 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
277 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
278 std::forward<_UHead>(__head)) { }
280 template<typename _Alloc>
281 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
282 const _Tuple_impl& __in)
283 : _Inherited(__tag, __a, _M_tail(__in)),
284 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
286 template<typename _Alloc>
287 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
289 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
290 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
291 std::forward<_Head>(_M_head(__in))) { }
293 template<typename _Alloc, typename... _UElements>
294 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
295 const _Tuple_impl<_Idx, _UElements...>& __in)
296 : _Inherited(__tag, __a,
297 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
298 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
299 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
301 template<typename _Alloc, typename _UHead, typename... _UTails>
302 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
303 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
304 : _Inherited(__tag, __a, std::move
305 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
306 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
308 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
311 operator=(const _Tuple_impl& __in)
313 _M_head(*this) = _M_head(__in);
314 _M_tail(*this) = _M_tail(__in);
319 operator=(_Tuple_impl&& __in)
320 noexcept(__and_<is_nothrow_move_assignable<_Head>,
321 is_nothrow_move_assignable<_Inherited>>::value)
323 _M_head(*this) = std::forward<_Head>(_M_head(__in));
324 _M_tail(*this) = std::move(_M_tail(__in));
328 template<typename... _UElements>
330 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
332 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
333 _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
337 template<typename _UHead, typename... _UTails>
339 operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
341 _M_head(*this) = std::forward<_UHead>
342 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
343 _M_tail(*this) = std::move
344 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
350 _M_swap(_Tuple_impl& __in)
351 noexcept(noexcept(swap(std::declval<_Head&>(),
352 std::declval<_Head&>()))
353 && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
356 swap(_M_head(*this), _M_head(__in));
357 _Inherited::_M_swap(_M_tail(__in));
361 /// Primary class template, tuple
362 template<typename... _Elements>
363 class tuple : public _Tuple_impl<0, _Elements...>
365 typedef _Tuple_impl<0, _Elements...> _Inherited;
372 constexpr tuple(const _Elements&... __elements)
373 : _Inherited(__elements...) { }
375 template<typename... _UElements, typename = typename
376 enable_if<__and_<is_convertible<_UElements,
377 _Elements>...>::value>::type>
379 constexpr tuple(_UElements&&... __elements)
380 : _Inherited(std::forward<_UElements>(__elements)...) { }
382 constexpr tuple(const tuple&) = default;
384 constexpr tuple(tuple&&) = default;
386 template<typename... _UElements, typename = typename
387 enable_if<__and_<is_convertible<const _UElements&,
388 _Elements>...>::value>::type>
389 constexpr tuple(const tuple<_UElements...>& __in)
390 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
393 template<typename... _UElements, typename = typename
394 enable_if<__and_<is_convertible<_UElements,
395 _Elements>...>::value>::type>
396 constexpr tuple(tuple<_UElements...>&& __in)
397 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
399 // Allocator-extended constructors.
401 template<typename _Alloc>
402 tuple(allocator_arg_t __tag, const _Alloc& __a)
403 : _Inherited(__tag, __a) { }
405 template<typename _Alloc>
406 tuple(allocator_arg_t __tag, const _Alloc& __a,
407 const _Elements&... __elements)
408 : _Inherited(__tag, __a, __elements...) { }
410 template<typename _Alloc, typename... _UElements, typename = typename
411 enable_if<sizeof...(_UElements)
412 == sizeof...(_Elements)>::type>
413 tuple(allocator_arg_t __tag, const _Alloc& __a,
414 _UElements&&... __elements)
415 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
418 template<typename _Alloc>
419 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
420 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
422 template<typename _Alloc>
423 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
424 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
426 template<typename _Alloc, typename... _UElements, typename = typename
427 enable_if<sizeof...(_UElements)
428 == sizeof...(_Elements)>::type>
429 tuple(allocator_arg_t __tag, const _Alloc& __a,
430 const tuple<_UElements...>& __in)
431 : _Inherited(__tag, __a,
432 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
435 template<typename _Alloc, typename... _UElements, typename = typename
436 enable_if<sizeof...(_UElements)
437 == sizeof...(_Elements)>::type>
438 tuple(allocator_arg_t __tag, const _Alloc& __a,
439 tuple<_UElements...>&& __in)
440 : _Inherited(__tag, __a,
441 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
445 operator=(const tuple& __in)
447 static_cast<_Inherited&>(*this) = __in;
452 operator=(tuple&& __in)
453 noexcept(is_nothrow_move_assignable<_Inherited>::value)
455 static_cast<_Inherited&>(*this) = std::move(__in);
459 template<typename... _UElements, typename = typename
460 enable_if<sizeof...(_UElements)
461 == sizeof...(_Elements)>::type>
463 operator=(const tuple<_UElements...>& __in)
465 static_cast<_Inherited&>(*this) = __in;
469 template<typename... _UElements, typename = typename
470 enable_if<sizeof...(_UElements)
471 == sizeof...(_Elements)>::type>
473 operator=(tuple<_UElements...>&& __in)
475 static_cast<_Inherited&>(*this) = std::move(__in);
481 noexcept(noexcept(__in._M_swap(__in)))
482 { _Inherited::_M_swap(__in); }
485 // Explicit specialization, zero-element tuple.
490 void swap(tuple&) noexcept { /* no-op */ }
493 /// Partial specialization, 2-element tuple.
494 /// Includes construction and assignment from a pair.
495 template<typename _T1, typename _T2>
496 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
498 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
505 constexpr tuple(const _T1& __a1, const _T2& __a2)
506 : _Inherited(__a1, __a2) { }
508 template<typename _U1, typename _U2, typename = typename
509 enable_if<__and_<is_convertible<_U1, _T1>,
510 is_convertible<_U2, _T2>>::value>::type>
512 constexpr tuple(_U1&& __a1, _U2&& __a2)
513 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
515 constexpr tuple(const tuple&) = default;
517 constexpr tuple(tuple&&) = default;
519 template<typename _U1, typename _U2, typename = typename
520 enable_if<__and_<is_convertible<const _U1&, _T1>,
521 is_convertible<const _U2&, _T2>>::value>::type>
522 constexpr tuple(const tuple<_U1, _U2>& __in)
523 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
525 template<typename _U1, typename _U2, typename = typename
526 enable_if<__and_<is_convertible<_U1, _T1>,
527 is_convertible<_U2, _T2>>::value>::type>
528 constexpr tuple(tuple<_U1, _U2>&& __in)
529 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
531 template<typename _U1, typename _U2, typename = typename
532 enable_if<__and_<is_convertible<const _U1&, _T1>,
533 is_convertible<const _U2&, _T2>>::value>::type>
534 constexpr tuple(const pair<_U1, _U2>& __in)
535 : _Inherited(__in.first, __in.second) { }
537 template<typename _U1, typename _U2, typename = typename
538 enable_if<__and_<is_convertible<_U1, _T1>,
539 is_convertible<_U2, _T2>>::value>::type>
540 constexpr tuple(pair<_U1, _U2>&& __in)
541 : _Inherited(std::forward<_U1>(__in.first),
542 std::forward<_U2>(__in.second)) { }
544 // Allocator-extended constructors.
546 template<typename _Alloc>
547 tuple(allocator_arg_t __tag, const _Alloc& __a)
548 : _Inherited(__tag, __a) { }
550 template<typename _Alloc>
551 tuple(allocator_arg_t __tag, const _Alloc& __a,
552 const _T1& __a1, const _T2& __a2)
553 : _Inherited(__tag, __a, __a1, __a2) { }
555 template<typename _Alloc, typename _U1, typename _U2>
556 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
557 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
558 std::forward<_U2>(__a2)) { }
560 template<typename _Alloc>
561 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
562 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
564 template<typename _Alloc>
565 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
566 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
568 template<typename _Alloc, typename _U1, typename _U2>
569 tuple(allocator_arg_t __tag, const _Alloc& __a,
570 const tuple<_U1, _U2>& __in)
571 : _Inherited(__tag, __a,
572 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
575 template<typename _Alloc, typename _U1, typename _U2>
576 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
577 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
580 template<typename _Alloc, typename _U1, typename _U2>
581 tuple(allocator_arg_t __tag, const _Alloc& __a,
582 const pair<_U1, _U2>& __in)
583 : _Inherited(__tag, __a, __in.first, __in.second) { }
585 template<typename _Alloc, typename _U1, typename _U2>
586 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
587 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
588 std::forward<_U2>(__in.second)) { }
591 operator=(const tuple& __in)
593 static_cast<_Inherited&>(*this) = __in;
598 operator=(tuple&& __in)
599 noexcept(is_nothrow_move_assignable<_Inherited>::value)
601 static_cast<_Inherited&>(*this) = std::move(__in);
605 template<typename _U1, typename _U2>
607 operator=(const tuple<_U1, _U2>& __in)
609 static_cast<_Inherited&>(*this) = __in;
613 template<typename _U1, typename _U2>
615 operator=(tuple<_U1, _U2>&& __in)
617 static_cast<_Inherited&>(*this) = std::move(__in);
621 template<typename _U1, typename _U2>
623 operator=(const pair<_U1, _U2>& __in)
625 this->_M_head(*this) = __in.first;
626 this->_M_tail(*this)._M_head(*this) = __in.second;
630 template<typename _U1, typename _U2>
632 operator=(pair<_U1, _U2>&& __in)
634 this->_M_head(*this) = std::forward<_U1>(__in.first);
635 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
641 noexcept(noexcept(__in._M_swap(__in)))
642 { _Inherited::_M_swap(__in); }
646 /// Gives the type of the ith element of a given tuple type.
647 template<std::size_t __i, typename _Tp>
648 struct tuple_element;
651 * Recursive case for tuple_element: strip off the first element in
652 * the tuple and retrieve the (i-1)th element of the remaining tuple.
654 template<std::size_t __i, typename _Head, typename... _Tail>
655 struct tuple_element<__i, tuple<_Head, _Tail...> >
656 : tuple_element<__i - 1, tuple<_Tail...> > { };
659 * Basis case for tuple_element: The first element is the one we're seeking.
661 template<typename _Head, typename... _Tail>
662 struct tuple_element<0, tuple<_Head, _Tail...> >
667 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
668 template<std::size_t __i, typename _Tp>
669 using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
671 template<std::size_t __i, typename _Tp>
672 struct tuple_element<__i, const _Tp>
674 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
677 template<std::size_t __i, typename _Tp>
678 struct tuple_element<__i, volatile _Tp>
680 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
683 template<std::size_t __i, typename _Tp>
684 struct tuple_element<__i, const volatile _Tp>
686 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
689 #if __cplusplus > 201103L
690 template<std::size_t __i, typename _Tp>
691 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
694 /// Finds the size of a given tuple type.
695 template<typename _Tp>
698 // _GLIBCXX_RESOLVE_LIB_DEFECTS
699 // 2313. tuple_size should always derive from integral_constant<size_t, N>
700 template<typename _Tp>
701 struct tuple_size<const _Tp>
702 : integral_constant<size_t, tuple_size<_Tp>::value> { };
704 template<typename _Tp>
705 struct tuple_size<volatile _Tp>
706 : integral_constant<size_t, tuple_size<_Tp>::value> { };
708 template<typename _Tp>
709 struct tuple_size<const volatile _Tp>
710 : integral_constant<size_t, tuple_size<_Tp>::value> { };
713 template<typename... _Elements>
714 struct tuple_size<tuple<_Elements...>>
715 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
717 template<std::size_t __i, typename _Head, typename... _Tail>
719 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
720 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
722 template<std::size_t __i, typename _Head, typename... _Tail>
723 constexpr const _Head&
724 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
725 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
727 /// Return a reference to the ith element of a tuple.
728 template<std::size_t __i, typename... _Elements>
729 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
730 get(tuple<_Elements...>& __t) noexcept
731 { return std::__get_helper<__i>(__t); }
733 /// Return a const reference to the ith element of a const tuple.
734 template<std::size_t __i, typename... _Elements>
735 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
736 get(const tuple<_Elements...>& __t) noexcept
737 { return std::__get_helper<__i>(__t); }
739 /// Return an rvalue reference to the ith element of a tuple rvalue.
740 template<std::size_t __i, typename... _Elements>
741 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
742 get(tuple<_Elements...>&& __t) noexcept
744 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
745 return std::forward<__element_type&&>(std::get<__i>(__t));
748 #if __cplusplus > 201103L
749 template<typename _Head, size_t __i, typename... _Tail>
751 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
752 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
754 template<typename _Head, size_t __i, typename... _Tail>
755 constexpr const _Head&
756 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
757 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
759 /// Return a reference to the unique element of type _Tp of a tuple.
760 template <typename _Tp, typename... _Types>
762 get(tuple<_Types...>& __t) noexcept
763 { return std::__get_helper2<_Tp>(__t); }
765 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
766 template <typename _Tp, typename... _Types>
768 get(tuple<_Types...>&& __t) noexcept
769 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
771 /// Return a const reference to the unique element of type _Tp of a tuple.
772 template <typename _Tp, typename... _Types>
774 get(const tuple<_Types...>& __t) noexcept
775 { return std::__get_helper2<_Tp>(__t); }
778 // This class performs the comparison operations on tuples
779 template<typename _Tp, typename _Up, size_t __i, size_t __size>
780 struct __tuple_compare
782 static constexpr bool
783 __eq(const _Tp& __t, const _Up& __u)
785 return bool(std::get<__i>(__t) == std::get<__i>(__u))
786 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
789 static constexpr bool
790 __less(const _Tp& __t, const _Up& __u)
792 return bool(std::get<__i>(__t) < std::get<__i>(__u))
793 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
794 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
798 template<typename _Tp, typename _Up, size_t __size>
799 struct __tuple_compare<_Tp, _Up, __size, __size>
801 static constexpr bool
802 __eq(const _Tp&, const _Up&) { return true; }
804 static constexpr bool
805 __less(const _Tp&, const _Up&) { return false; }
808 template<typename... _TElements, typename... _UElements>
810 operator==(const tuple<_TElements...>& __t,
811 const tuple<_UElements...>& __u)
813 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
814 "tuple objects can only be compared if they have equal sizes.");
815 using __compare = __tuple_compare<tuple<_TElements...>,
816 tuple<_UElements...>,
817 0, sizeof...(_TElements)>;
818 return __compare::__eq(__t, __u);
821 template<typename... _TElements, typename... _UElements>
823 operator<(const tuple<_TElements...>& __t,
824 const tuple<_UElements...>& __u)
826 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
827 "tuple objects can only be compared if they have equal sizes.");
828 using __compare = __tuple_compare<tuple<_TElements...>,
829 tuple<_UElements...>,
830 0, sizeof...(_TElements)>;
831 return __compare::__less(__t, __u);
834 template<typename... _TElements, typename... _UElements>
836 operator!=(const tuple<_TElements...>& __t,
837 const tuple<_UElements...>& __u)
838 { return !(__t == __u); }
840 template<typename... _TElements, typename... _UElements>
842 operator>(const tuple<_TElements...>& __t,
843 const tuple<_UElements...>& __u)
844 { return __u < __t; }
846 template<typename... _TElements, typename... _UElements>
848 operator<=(const tuple<_TElements...>& __t,
849 const tuple<_UElements...>& __u)
850 { return !(__u < __t); }
852 template<typename... _TElements, typename... _UElements>
854 operator>=(const tuple<_TElements...>& __t,
855 const tuple<_UElements...>& __u)
856 { return !(__t < __u); }
859 template<typename... _Elements>
860 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
861 make_tuple(_Elements&&... __args)
863 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
865 return __result_type(std::forward<_Elements>(__args)...);
868 template<typename... _Elements>
869 tuple<_Elements&&...>
870 forward_as_tuple(_Elements&&... __args) noexcept
871 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
874 struct __is_tuple_like_impl : false_type
877 template<typename... _Tps>
878 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
881 template<typename _T1, typename _T2>
882 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
885 template<typename _Tp, std::size_t _Nm>
886 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
889 // Internal type trait that allows us to sfinae-protect tuple_cat.
890 template<typename _Tp>
891 struct __is_tuple_like
892 : public __is_tuple_like_impl<typename std::remove_cv
893 <typename std::remove_reference<_Tp>::type>::type>::type
896 template<size_t, typename, typename, size_t>
897 struct __make_tuple_impl;
899 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
900 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
901 : __make_tuple_impl<_Idx + 1,
902 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
906 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
907 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
909 typedef tuple<_Tp...> __type;
912 template<typename _Tuple>
913 struct __do_make_tuple
914 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
917 // Returns the std::tuple equivalent of a tuple-like type.
918 template<typename _Tuple>
920 : public __do_make_tuple<typename std::remove_cv
921 <typename std::remove_reference<_Tuple>::type>::type>
924 // Combines several std::tuple's into a single one.
925 template<typename...>
926 struct __combine_tuples;
929 struct __combine_tuples<>
931 typedef tuple<> __type;
934 template<typename... _Ts>
935 struct __combine_tuples<tuple<_Ts...>>
937 typedef tuple<_Ts...> __type;
940 template<typename... _T1s, typename... _T2s, typename... _Rem>
941 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
943 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
944 _Rem...>::__type __type;
947 // Computes the result type of tuple_cat given a set of tuple-like types.
948 template<typename... _Tpls>
949 struct __tuple_cat_result
951 typedef typename __combine_tuples
952 <typename __make_tuple<_Tpls>::__type...>::__type __type;
955 // Helper to determine the index set for the first tuple-like
956 // type of a given set.
957 template<typename...>
958 struct __make_1st_indices;
961 struct __make_1st_indices<>
963 typedef std::_Index_tuple<> __type;
966 template<typename _Tp, typename... _Tpls>
967 struct __make_1st_indices<_Tp, _Tpls...>
969 typedef typename std::_Build_index_tuple<std::tuple_size<
970 typename std::remove_reference<_Tp>::type>::value>::__type __type;
973 // Performs the actual concatenation by step-wise expanding tuple-like
974 // objects into the elements, which are finally forwarded into the
976 template<typename _Ret, typename _Indices, typename... _Tpls>
977 struct __tuple_concater;
979 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
980 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
982 template<typename... _Us>
983 static constexpr _Ret
984 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
986 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
987 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
988 return __next::_S_do(std::forward<_Tpls>(__tps)...,
989 std::forward<_Us>(__us)...,
990 std::get<_Is>(std::forward<_Tp>(__tp))...);
994 template<typename _Ret>
995 struct __tuple_concater<_Ret, std::_Index_tuple<>>
997 template<typename... _Us>
998 static constexpr _Ret
1001 return _Ret(std::forward<_Us>(__us)...);
1006 template<typename... _Tpls, typename = typename
1007 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1009 tuple_cat(_Tpls&&... __tpls)
1010 -> typename __tuple_cat_result<_Tpls...>::__type
1012 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1013 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1014 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1015 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1019 template<typename... _Elements>
1020 inline tuple<_Elements&...>
1021 tie(_Elements&... __args) noexcept
1022 { return tuple<_Elements&...>(__args...); }
1025 template<typename... _Elements>
1027 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1028 noexcept(noexcept(__x.swap(__y)))
1031 // A class (and instance) which can be used in 'tie' when an element
1032 // of a tuple is not required
1033 struct _Swallow_assign
1036 const _Swallow_assign&
1037 operator=(const _Tp&) const
1041 const _Swallow_assign ignore{};
1043 /// Partial specialization for tuples
1044 template<typename... _Types, typename _Alloc>
1045 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1047 // See stl_pair.h...
1048 template<class _T1, class _T2>
1049 template<typename... _Args1, typename... _Args2>
1052 pair(piecewise_construct_t,
1053 tuple<_Args1...> __first, tuple<_Args2...> __second)
1054 : pair(__first, __second,
1055 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1056 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1059 template<class _T1, class _T2>
1060 template<typename... _Args1, std::size_t... _Indexes1,
1061 typename... _Args2, std::size_t... _Indexes2>
1064 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1065 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1066 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1067 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1072 _GLIBCXX_END_NAMESPACE_VERSION
1077 #endif // _GLIBCXX_TUPLE