3 // Copyright (C) 2007-2020 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>
41 #include <bits/invoke.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * @addtogroup utilities
52 template<typename... _Elements>
55 template<typename _Tp>
56 struct __is_empty_non_tuple : is_empty<_Tp> { };
58 // Using EBO for elements that are tuples causes ambiguous base errors.
59 template<typename _El0, typename... _El>
60 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
62 // Use the Empty Base-class Optimization for empty, non-final types.
63 template<typename _Tp>
64 using __empty_not_final
65 = typename conditional<__is_final(_Tp), false_type,
66 __is_empty_non_tuple<_Tp>>::type;
68 template<std::size_t _Idx, typename _Head,
69 bool = __empty_not_final<_Head>::value>
72 template<std::size_t _Idx, typename _Head>
73 struct _Head_base<_Idx, _Head, true>
76 constexpr _Head_base()
79 constexpr _Head_base(const _Head& __h)
82 constexpr _Head_base(const _Head_base&) = default;
83 constexpr _Head_base(_Head_base&&) = default;
85 template<typename _UHead>
86 constexpr _Head_base(_UHead&& __h)
87 : _Head(std::forward<_UHead>(__h)) { }
89 _Head_base(allocator_arg_t, __uses_alloc0)
92 template<typename _Alloc>
93 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
94 : _Head(allocator_arg, *__a._M_a) { }
96 template<typename _Alloc>
97 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
98 : _Head(*__a._M_a) { }
100 template<typename _UHead>
101 _Head_base(__uses_alloc0, _UHead&& __uhead)
102 : _Head(std::forward<_UHead>(__uhead)) { }
104 template<typename _Alloc, typename _UHead>
105 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
106 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
108 template<typename _Alloc, typename _UHead>
109 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
110 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
112 static constexpr _Head&
113 _M_head(_Head_base& __b) noexcept { return __b; }
115 static constexpr const _Head&
116 _M_head(const _Head_base& __b) noexcept { return __b; }
119 template<std::size_t _Idx, typename _Head>
120 struct _Head_base<_Idx, _Head, false>
122 constexpr _Head_base()
125 constexpr _Head_base(const _Head& __h)
126 : _M_head_impl(__h) { }
128 constexpr _Head_base(const _Head_base&) = default;
129 constexpr _Head_base(_Head_base&&) = default;
131 template<typename _UHead>
132 constexpr _Head_base(_UHead&& __h)
133 : _M_head_impl(std::forward<_UHead>(__h)) { }
136 _Head_base(allocator_arg_t, __uses_alloc0)
139 template<typename _Alloc>
140 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
141 : _M_head_impl(allocator_arg, *__a._M_a) { }
143 template<typename _Alloc>
144 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
145 : _M_head_impl(*__a._M_a) { }
147 template<typename _UHead>
149 _Head_base(__uses_alloc0, _UHead&& __uhead)
150 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
152 template<typename _Alloc, typename _UHead>
153 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
154 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
157 template<typename _Alloc, typename _UHead>
158 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
159 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
161 static constexpr _Head&
162 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
164 static constexpr const _Head&
165 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
171 * Contains the actual implementation of the @c tuple template, stored
172 * as a recursive inheritance hierarchy from the first element (most
173 * derived class) to the last (least derived class). The @c Idx
174 * parameter gives the 0-based index of the element stored at this
175 * point in the hierarchy; we use it to implement a constant-time
178 template<std::size_t _Idx, typename... _Elements>
182 * Recursive tuple implementation. Here we store the @c Head element
183 * and derive from a @c Tuple_impl containing the remaining elements
184 * (which contains the @c Tail).
186 template<std::size_t _Idx, typename _Head, typename... _Tail>
187 struct _Tuple_impl<_Idx, _Head, _Tail...>
188 : public _Tuple_impl<_Idx + 1, _Tail...>,
189 private _Head_base<_Idx, _Head>
191 template<std::size_t, typename...> friend class _Tuple_impl;
193 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
194 typedef _Head_base<_Idx, _Head> _Base;
196 static constexpr _Head&
197 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
199 static constexpr const _Head&
200 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
202 static constexpr _Inherited&
203 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
205 static constexpr const _Inherited&
206 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
208 constexpr _Tuple_impl()
209 : _Inherited(), _Base() { }
212 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
213 : _Inherited(__tail...), _Base(__head) { }
215 template<typename _UHead, typename... _UTail, typename = typename
216 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
218 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
219 : _Inherited(std::forward<_UTail>(__tail)...),
220 _Base(std::forward<_UHead>(__head)) { }
222 constexpr _Tuple_impl(const _Tuple_impl&) = default;
224 // _GLIBCXX_RESOLVE_LIB_DEFECTS
225 // 2729. Missing SFINAE on std::pair::operator=
226 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
229 _Tuple_impl(_Tuple_impl&& __in)
230 noexcept(__and_<is_nothrow_move_constructible<_Head>,
231 is_nothrow_move_constructible<_Inherited>>::value)
232 : _Inherited(std::move(_M_tail(__in))),
233 _Base(std::forward<_Head>(_M_head(__in))) { }
235 template<typename... _UElements>
236 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
237 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
238 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
240 template<typename _UHead, typename... _UTails>
241 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
242 : _Inherited(std::move
243 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
244 _Base(std::forward<_UHead>
245 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
247 template<typename _Alloc>
249 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
250 : _Inherited(__tag, __a),
251 _Base(__tag, __use_alloc<_Head>(__a)) { }
253 template<typename _Alloc>
254 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
255 const _Head& __head, const _Tail&... __tail)
256 : _Inherited(__tag, __a, __tail...),
257 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
259 template<typename _Alloc, typename _UHead, typename... _UTail,
260 typename = typename enable_if<sizeof...(_Tail)
261 == sizeof...(_UTail)>::type>
263 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
264 _UHead&& __head, _UTail&&... __tail)
265 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
266 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
267 std::forward<_UHead>(__head)) { }
269 template<typename _Alloc>
271 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
272 const _Tuple_impl& __in)
273 : _Inherited(__tag, __a, _M_tail(__in)),
274 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
276 template<typename _Alloc>
278 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
280 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
281 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
282 std::forward<_Head>(_M_head(__in))) { }
284 template<typename _Alloc, typename... _UElements>
286 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
287 const _Tuple_impl<_Idx, _UElements...>& __in)
288 : _Inherited(__tag, __a,
289 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
290 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
291 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
293 template<typename _Alloc, typename _UHead, typename... _UTails>
295 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
296 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
297 : _Inherited(__tag, __a, std::move
298 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
299 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
301 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
303 template<typename... _UElements>
306 _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in)
308 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
309 _M_tail(*this)._M_assign(
310 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
313 template<typename _UHead, typename... _UTails>
316 _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
318 _M_head(*this) = std::forward<_UHead>
319 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
320 _M_tail(*this)._M_assign(
321 std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
327 _M_swap(_Tuple_impl& __in)
330 swap(_M_head(*this), _M_head(__in));
331 _Inherited::_M_swap(_M_tail(__in));
335 // Basis case of inheritance recursion.
336 template<std::size_t _Idx, typename _Head>
337 struct _Tuple_impl<_Idx, _Head>
338 : private _Head_base<_Idx, _Head>
340 template<std::size_t, typename...> friend class _Tuple_impl;
342 typedef _Head_base<_Idx, _Head> _Base;
344 static constexpr _Head&
345 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
347 static constexpr const _Head&
348 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
350 constexpr _Tuple_impl()
354 constexpr _Tuple_impl(const _Head& __head)
357 template<typename _UHead>
359 constexpr _Tuple_impl(_UHead&& __head)
360 : _Base(std::forward<_UHead>(__head)) { }
362 constexpr _Tuple_impl(const _Tuple_impl&) = default;
364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
365 // 2729. Missing SFINAE on std::pair::operator=
366 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
369 _Tuple_impl(_Tuple_impl&& __in)
370 noexcept(is_nothrow_move_constructible<_Head>::value)
371 : _Base(std::forward<_Head>(_M_head(__in))) { }
373 template<typename _UHead>
374 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
375 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
377 template<typename _UHead>
378 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
379 : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
382 template<typename _Alloc>
384 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
385 : _Base(__tag, __use_alloc<_Head>(__a)) { }
387 template<typename _Alloc>
388 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
390 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
392 template<typename _Alloc, typename _UHead>
394 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
396 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
397 std::forward<_UHead>(__head)) { }
399 template<typename _Alloc>
401 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
402 const _Tuple_impl& __in)
403 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
405 template<typename _Alloc>
407 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
409 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
410 std::forward<_Head>(_M_head(__in))) { }
412 template<typename _Alloc, typename _UHead>
414 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
415 const _Tuple_impl<_Idx, _UHead>& __in)
416 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
417 _Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
419 template<typename _Alloc, typename _UHead>
421 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
422 _Tuple_impl<_Idx, _UHead>&& __in)
423 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
424 std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
427 template<typename _UHead>
430 _M_assign(const _Tuple_impl<_Idx, _UHead>& __in)
432 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
435 template<typename _UHead>
438 _M_assign(_Tuple_impl<_Idx, _UHead>&& __in)
441 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
447 _M_swap(_Tuple_impl& __in)
450 swap(_M_head(*this), _M_head(__in));
454 // Concept utility functions, reused in conditionally-explicit
456 template<bool, typename... _Types>
457 struct _TupleConstraints
459 // Constraint for a non-explicit constructor.
460 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
461 // and every Ui is implicitly convertible to Ti.
462 template<typename... _UTypes>
463 static constexpr bool __is_implicitly_constructible()
465 return __and_<is_constructible<_Types, _UTypes>...,
466 is_convertible<_UTypes, _Types>...
470 // Constraint for a non-explicit constructor.
471 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
472 // but not every Ui is implicitly convertible to Ti.
473 template<typename... _UTypes>
474 static constexpr bool __is_explicitly_constructible()
476 return __and_<is_constructible<_Types, _UTypes>...,
477 __not_<__and_<is_convertible<_UTypes, _Types>...>>
481 static constexpr bool __is_implicitly_default_constructible()
483 return __and_<std::__is_implicitly_default_constructible<_Types>...
487 static constexpr bool __is_explicitly_default_constructible()
489 return __and_<is_default_constructible<_Types>...,
491 std::__is_implicitly_default_constructible<_Types>...>
496 // Partial specialization used when a required precondition isn't met,
497 // e.g. when sizeof...(_Types) != sizeof...(_UTypes).
498 template<typename... _Types>
499 struct _TupleConstraints<false, _Types...>
501 template<typename... _UTypes>
502 static constexpr bool __is_implicitly_constructible()
505 template<typename... _UTypes>
506 static constexpr bool __is_explicitly_constructible()
510 /// Primary class template, tuple
511 template<typename... _Elements>
512 class tuple : public _Tuple_impl<0, _Elements...>
514 typedef _Tuple_impl<0, _Elements...> _Inherited;
517 using _TCC = _TupleConstraints<_Cond, _Elements...>;
519 // Constraint for non-explicit default constructor
520 template<bool _Dummy>
521 using _ImplicitDefaultCtor = __enable_if_t<
522 _TCC<_Dummy>::__is_implicitly_default_constructible(),
525 // Constraint for explicit default constructor
526 template<bool _Dummy>
527 using _ExplicitDefaultCtor = __enable_if_t<
528 _TCC<_Dummy>::__is_explicitly_default_constructible(),
531 // Constraint for non-explicit constructors
532 template<bool _Cond, typename... _Args>
533 using _ImplicitCtor = __enable_if_t<
534 _TCC<_Cond>::template __is_implicitly_constructible<_Args...>(),
537 // Constraint for non-explicit constructors
538 template<bool _Cond, typename... _Args>
539 using _ExplicitCtor = __enable_if_t<
540 _TCC<_Cond>::template __is_explicitly_constructible<_Args...>(),
543 template<typename... _UElements>
545 __enable_if_t<sizeof...(_UElements) == sizeof...(_Elements), bool>
547 { return __and_<is_assignable<_Elements&, _UElements>...>::value; }
549 // Condition for noexcept-specifier of an assignment operator.
550 template<typename... _UElements>
551 static constexpr bool __nothrow_assignable()
554 __and_<is_nothrow_assignable<_Elements&, _UElements>...>::value;
557 // Condition for noexcept-specifier of a constructor.
558 template<typename... _UElements>
559 static constexpr bool __nothrow_constructible()
562 __and_<is_nothrow_constructible<_Elements, _UElements>...>::value;
565 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) == 1.
566 template<typename _Up>
567 static constexpr bool __valid_args()
569 return sizeof...(_Elements) == 1
570 && !is_same<tuple, __remove_cvref_t<_Up>>::value;
573 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) > 1.
574 template<typename, typename, typename... _Tail>
575 static constexpr bool __valid_args()
576 { return (sizeof...(_Tail) + 2) == sizeof...(_Elements); }
578 /* Constraint for constructors with a tuple<UTypes...> parameter ensures
579 * that the constructor is only viable when it would not interfere with
580 * tuple(UTypes&&...) or tuple(const tuple&) or tuple(tuple&&).
581 * Such constructors are only viable if:
582 * either sizeof...(Types) != 1,
583 * or (when Types... expands to T and UTypes... expands to U)
584 * is_convertible_v<TUPLE, T>, is_constructible_v<T, TUPLE>,
585 * and is_same_v<T, U> are all false.
587 template<typename _Tuple, typename = tuple,
588 typename = __remove_cvref_t<_Tuple>>
592 // If TUPLE is convertible to the single element in *this,
593 // then TUPLE should match tuple(UTypes&&...) instead.
594 template<typename _Tuple, typename _Tp, typename _Up>
595 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Up>>
596 : __or_<is_convertible<_Tuple, _Tp>, is_constructible<_Tp, _Tuple>>
598 // If TUPLE and *this each have a single element of the same type,
599 // then TUPLE should match a copy/move constructor instead.
600 template<typename _Tuple, typename _Tp>
601 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Tp>>
605 // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
606 // and the single element in Types can be initialized from TUPLE,
607 // or is the same type as tuple_element_t<0, TUPLE>.
608 template<typename _Tuple>
609 static constexpr bool __use_other_ctor()
610 { return _UseOtherCtor<_Tuple>::value; }
613 template<typename _Dummy = void,
614 _ImplicitDefaultCtor<is_void<_Dummy>::value> = true>
617 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
620 template<typename _Dummy = void,
621 _ExplicitDefaultCtor<is_void<_Dummy>::value> = false>
624 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
627 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
628 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
630 tuple(const _Elements&... __elements)
631 noexcept(__nothrow_constructible<const _Elements&...>())
632 : _Inherited(__elements...) { }
634 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
635 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
637 tuple(const _Elements&... __elements)
638 noexcept(__nothrow_constructible<const _Elements&...>())
639 : _Inherited(__elements...) { }
641 template<typename... _UElements,
642 bool _Valid = __valid_args<_UElements...>(),
643 _ImplicitCtor<_Valid, _UElements...> = true>
645 tuple(_UElements&&... __elements)
646 noexcept(__nothrow_constructible<_UElements...>())
647 : _Inherited(std::forward<_UElements>(__elements)...) { }
649 template<typename... _UElements,
650 bool _Valid = __valid_args<_UElements...>(),
651 _ExplicitCtor<_Valid, _UElements...> = false>
653 tuple(_UElements&&... __elements)
654 noexcept(__nothrow_constructible<_UElements...>())
655 : _Inherited(std::forward<_UElements>(__elements)...) { }
657 constexpr tuple(const tuple&) = default;
659 constexpr tuple(tuple&&) = default;
661 template<typename... _UElements,
662 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
663 && !__use_other_ctor<const tuple<_UElements...>&>(),
664 _ImplicitCtor<_Valid, const _UElements&...> = true>
666 tuple(const tuple<_UElements...>& __in)
667 noexcept(__nothrow_constructible<const _UElements&...>())
668 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
671 template<typename... _UElements,
672 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
673 && !__use_other_ctor<const tuple<_UElements...>&>(),
674 _ExplicitCtor<_Valid, const _UElements&...> = false>
676 tuple(const tuple<_UElements...>& __in)
677 noexcept(__nothrow_constructible<const _UElements&...>())
678 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
681 template<typename... _UElements,
682 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
683 && !__use_other_ctor<tuple<_UElements...>&&>(),
684 _ImplicitCtor<_Valid, _UElements...> = true>
686 tuple(tuple<_UElements...>&& __in)
687 noexcept(__nothrow_constructible<_UElements...>())
688 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
690 template<typename... _UElements,
691 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
692 && !__use_other_ctor<tuple<_UElements...>&&>(),
693 _ExplicitCtor<_Valid, _UElements...> = false>
695 tuple(tuple<_UElements...>&& __in)
696 noexcept(__nothrow_constructible<_UElements...>())
697 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
699 // Allocator-extended constructors.
701 template<typename _Alloc,
702 _ImplicitDefaultCtor<is_object<_Alloc>::value> = true>
704 tuple(allocator_arg_t __tag, const _Alloc& __a)
705 : _Inherited(__tag, __a) { }
707 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
708 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
710 tuple(allocator_arg_t __tag, const _Alloc& __a,
711 const _Elements&... __elements)
712 : _Inherited(__tag, __a, __elements...) { }
714 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
715 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
718 tuple(allocator_arg_t __tag, const _Alloc& __a,
719 const _Elements&... __elements)
720 : _Inherited(__tag, __a, __elements...) { }
722 template<typename _Alloc, typename... _UElements,
723 bool _Valid = __valid_args<_UElements...>(),
724 _ImplicitCtor<_Valid, _UElements...> = true>
726 tuple(allocator_arg_t __tag, const _Alloc& __a,
727 _UElements&&... __elements)
728 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
731 template<typename _Alloc, typename... _UElements,
732 bool _Valid = __valid_args<_UElements...>(),
733 _ExplicitCtor<_Valid, _UElements...> = false>
736 tuple(allocator_arg_t __tag, const _Alloc& __a,
737 _UElements&&... __elements)
738 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
741 template<typename _Alloc>
743 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
744 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
746 template<typename _Alloc>
748 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
749 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
751 template<typename _Alloc, typename... _UElements,
752 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
753 && !__use_other_ctor<const tuple<_UElements...>&>(),
754 _ImplicitCtor<_Valid, const _UElements&...> = true>
756 tuple(allocator_arg_t __tag, const _Alloc& __a,
757 const tuple<_UElements...>& __in)
758 : _Inherited(__tag, __a,
759 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
762 template<typename _Alloc, typename... _UElements,
763 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
764 && !__use_other_ctor<const tuple<_UElements...>&>(),
765 _ExplicitCtor<_Valid, const _UElements&...> = false>
768 tuple(allocator_arg_t __tag, const _Alloc& __a,
769 const tuple<_UElements...>& __in)
770 : _Inherited(__tag, __a,
771 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
774 template<typename _Alloc, typename... _UElements,
775 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
776 && !__use_other_ctor<tuple<_UElements...>&&>(),
777 _ImplicitCtor<_Valid, _UElements...> = true>
779 tuple(allocator_arg_t __tag, const _Alloc& __a,
780 tuple<_UElements...>&& __in)
781 : _Inherited(__tag, __a,
782 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
785 template<typename _Alloc, typename... _UElements,
786 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
787 && !__use_other_ctor<tuple<_UElements...>&&>(),
788 _ExplicitCtor<_Valid, _UElements...> = false>
791 tuple(allocator_arg_t __tag, const _Alloc& __a,
792 tuple<_UElements...>&& __in)
793 : _Inherited(__tag, __a,
794 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
801 operator=(typename conditional<__assignable<const _Elements&...>(),
803 const __nonesuch&>::type __in)
804 noexcept(__nothrow_assignable<const _Elements&...>())
806 this->_M_assign(__in);
812 operator=(typename conditional<__assignable<_Elements...>(),
814 __nonesuch&&>::type __in)
815 noexcept(__nothrow_assignable<_Elements...>())
817 this->_M_assign(std::move(__in));
821 template<typename... _UElements>
823 __enable_if_t<__assignable<const _UElements&...>(), tuple&>
824 operator=(const tuple<_UElements...>& __in)
825 noexcept(__nothrow_assignable<const _UElements&...>())
827 this->_M_assign(__in);
831 template<typename... _UElements>
833 __enable_if_t<__assignable<_UElements...>(), tuple&>
834 operator=(tuple<_UElements...>&& __in)
835 noexcept(__nothrow_assignable<_UElements...>())
837 this->_M_assign(std::move(__in));
845 noexcept(__and_<__is_nothrow_swappable<_Elements>...>::value)
846 { _Inherited::_M_swap(__in); }
849 #if __cpp_deduction_guides >= 201606
850 template<typename... _UTypes>
851 tuple(_UTypes...) -> tuple<_UTypes...>;
852 template<typename _T1, typename _T2>
853 tuple(pair<_T1, _T2>) -> tuple<_T1, _T2>;
854 template<typename _Alloc, typename... _UTypes>
855 tuple(allocator_arg_t, _Alloc, _UTypes...) -> tuple<_UTypes...>;
856 template<typename _Alloc, typename _T1, typename _T2>
857 tuple(allocator_arg_t, _Alloc, pair<_T1, _T2>) -> tuple<_T1, _T2>;
858 template<typename _Alloc, typename... _UTypes>
859 tuple(allocator_arg_t, _Alloc, tuple<_UTypes...>) -> tuple<_UTypes...>;
862 // Explicit specialization, zero-element tuple.
867 void swap(tuple&) noexcept { /* no-op */ }
868 // We need the default since we're going to define no-op
869 // allocator constructors.
871 // No-op allocator constructors.
872 template<typename _Alloc>
874 tuple(allocator_arg_t, const _Alloc&) noexcept { }
875 template<typename _Alloc>
877 tuple(allocator_arg_t, const _Alloc&, const tuple&) noexcept { }
880 /// Partial specialization, 2-element tuple.
881 /// Includes construction and assignment from a pair.
882 template<typename _T1, typename _T2>
883 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
885 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
887 // Constraint for non-explicit default constructor
888 template<bool _Dummy, typename _U1, typename _U2>
889 using _ImplicitDefaultCtor = __enable_if_t<
890 _TupleConstraints<_Dummy, _U1, _U2>::
891 __is_implicitly_default_constructible(),
894 // Constraint for explicit default constructor
895 template<bool _Dummy, typename _U1, typename _U2>
896 using _ExplicitDefaultCtor = __enable_if_t<
897 _TupleConstraints<_Dummy, _U1, _U2>::
898 __is_explicitly_default_constructible(),
901 template<bool _Dummy>
902 using _TCC = _TupleConstraints<_Dummy, _T1, _T2>;
904 // Constraint for non-explicit constructors
905 template<bool _Cond, typename _U1, typename _U2>
906 using _ImplicitCtor = __enable_if_t<
907 _TCC<_Cond>::template __is_implicitly_constructible<_U1, _U2>(),
910 // Constraint for non-explicit constructors
911 template<bool _Cond, typename _U1, typename _U2>
912 using _ExplicitCtor = __enable_if_t<
913 _TCC<_Cond>::template __is_explicitly_constructible<_U1, _U2>(),
916 template<typename _U1, typename _U2>
917 static constexpr bool __assignable()
919 return __and_<is_assignable<_T1&, _U1>,
920 is_assignable<_T2&, _U2>>::value;
923 template<typename _U1, typename _U2>
924 static constexpr bool __nothrow_assignable()
926 return __and_<is_nothrow_assignable<_T1&, _U1>,
927 is_nothrow_assignable<_T2&, _U2>>::value;
930 template<typename _U1, typename _U2>
931 static constexpr bool __nothrow_constructible()
933 return __and_<is_nothrow_constructible<_T1, _U1>,
934 is_nothrow_constructible<_T2, _U2>>::value;
937 static constexpr bool __nothrow_default_constructible()
939 return __and_<is_nothrow_default_constructible<_T1>,
940 is_nothrow_default_constructible<_T2>>::value;
943 template<typename _U1>
944 static constexpr bool __is_alloc_arg()
945 { return is_same<__remove_cvref_t<_U1>, allocator_arg_t>::value; }
948 template<bool _Dummy = true,
949 _ImplicitDefaultCtor<_Dummy, _T1, _T2> = true>
952 noexcept(__nothrow_default_constructible())
955 template<bool _Dummy = true,
956 _ExplicitDefaultCtor<_Dummy, _T1, _T2> = false>
959 noexcept(__nothrow_default_constructible())
962 template<bool _Dummy = true,
963 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
965 tuple(const _T1& __a1, const _T2& __a2)
966 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
967 : _Inherited(__a1, __a2) { }
969 template<bool _Dummy = true,
970 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
972 tuple(const _T1& __a1, const _T2& __a2)
973 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
974 : _Inherited(__a1, __a2) { }
976 template<typename _U1, typename _U2,
977 _ImplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = true>
979 tuple(_U1&& __a1, _U2&& __a2)
980 noexcept(__nothrow_constructible<_U1, _U2>())
981 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
983 template<typename _U1, typename _U2,
984 _ExplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = false>
986 tuple(_U1&& __a1, _U2&& __a2)
987 noexcept(__nothrow_constructible<_U1, _U2>())
988 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
990 constexpr tuple(const tuple&) = default;
992 constexpr tuple(tuple&&) = default;
994 template<typename _U1, typename _U2,
995 _ImplicitCtor<true, const _U1&, const _U2&> = true>
997 tuple(const tuple<_U1, _U2>& __in)
998 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
999 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
1001 template<typename _U1, typename _U2,
1002 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1004 tuple(const tuple<_U1, _U2>& __in)
1005 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1006 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
1008 template<typename _U1, typename _U2,
1009 _ImplicitCtor<true, _U1, _U2> = true>
1011 tuple(tuple<_U1, _U2>&& __in)
1012 noexcept(__nothrow_constructible<_U1, _U2>())
1013 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
1015 template<typename _U1, typename _U2,
1016 _ExplicitCtor<true, _U1, _U2> = false>
1018 tuple(tuple<_U1, _U2>&& __in)
1019 noexcept(__nothrow_constructible<_U1, _U2>())
1020 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
1022 template<typename _U1, typename _U2,
1023 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1025 tuple(const pair<_U1, _U2>& __in)
1026 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1027 : _Inherited(__in.first, __in.second) { }
1029 template<typename _U1, typename _U2,
1030 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1032 tuple(const pair<_U1, _U2>& __in)
1033 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1034 : _Inherited(__in.first, __in.second) { }
1036 template<typename _U1, typename _U2,
1037 _ImplicitCtor<true, _U1, _U2> = true>
1039 tuple(pair<_U1, _U2>&& __in)
1040 noexcept(__nothrow_constructible<_U1, _U2>())
1041 : _Inherited(std::forward<_U1>(__in.first),
1042 std::forward<_U2>(__in.second)) { }
1044 template<typename _U1, typename _U2,
1045 _ExplicitCtor<true, _U1, _U2> = false>
1047 tuple(pair<_U1, _U2>&& __in)
1048 noexcept(__nothrow_constructible<_U1, _U2>())
1049 : _Inherited(std::forward<_U1>(__in.first),
1050 std::forward<_U2>(__in.second)) { }
1052 // Allocator-extended constructors.
1054 template<typename _Alloc,
1055 _ImplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = true>
1056 _GLIBCXX20_CONSTEXPR
1057 tuple(allocator_arg_t __tag, const _Alloc& __a)
1058 : _Inherited(__tag, __a) { }
1060 template<typename _Alloc, bool _Dummy = true,
1061 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
1062 _GLIBCXX20_CONSTEXPR
1063 tuple(allocator_arg_t __tag, const _Alloc& __a,
1064 const _T1& __a1, const _T2& __a2)
1065 : _Inherited(__tag, __a, __a1, __a2) { }
1067 template<typename _Alloc, bool _Dummy = true,
1068 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
1070 _GLIBCXX20_CONSTEXPR
1071 tuple(allocator_arg_t __tag, const _Alloc& __a,
1072 const _T1& __a1, const _T2& __a2)
1073 : _Inherited(__tag, __a, __a1, __a2) { }
1075 template<typename _Alloc, typename _U1, typename _U2,
1076 _ImplicitCtor<true, _U1, _U2> = true>
1077 _GLIBCXX20_CONSTEXPR
1078 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
1079 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1080 std::forward<_U2>(__a2)) { }
1082 template<typename _Alloc, typename _U1, typename _U2,
1083 _ExplicitCtor<true, _U1, _U2> = false>
1085 _GLIBCXX20_CONSTEXPR
1086 tuple(allocator_arg_t __tag, const _Alloc& __a,
1087 _U1&& __a1, _U2&& __a2)
1088 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1089 std::forward<_U2>(__a2)) { }
1091 template<typename _Alloc>
1092 _GLIBCXX20_CONSTEXPR
1093 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
1094 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
1096 template<typename _Alloc>
1097 _GLIBCXX20_CONSTEXPR
1098 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
1099 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
1101 template<typename _Alloc, typename _U1, typename _U2,
1102 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1103 _GLIBCXX20_CONSTEXPR
1104 tuple(allocator_arg_t __tag, const _Alloc& __a,
1105 const tuple<_U1, _U2>& __in)
1106 : _Inherited(__tag, __a,
1107 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1110 template<typename _Alloc, typename _U1, typename _U2,
1111 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1113 _GLIBCXX20_CONSTEXPR
1114 tuple(allocator_arg_t __tag, const _Alloc& __a,
1115 const tuple<_U1, _U2>& __in)
1116 : _Inherited(__tag, __a,
1117 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1120 template<typename _Alloc, typename _U1, typename _U2,
1121 _ImplicitCtor<true, _U1, _U2> = true>
1122 _GLIBCXX20_CONSTEXPR
1123 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1124 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1127 template<typename _Alloc, typename _U1, typename _U2,
1128 _ExplicitCtor<true, _U1, _U2> = false>
1130 _GLIBCXX20_CONSTEXPR
1131 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1132 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1135 template<typename _Alloc, typename _U1, typename _U2,
1136 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1137 _GLIBCXX20_CONSTEXPR
1138 tuple(allocator_arg_t __tag, const _Alloc& __a,
1139 const pair<_U1, _U2>& __in)
1140 : _Inherited(__tag, __a, __in.first, __in.second) { }
1142 template<typename _Alloc, typename _U1, typename _U2,
1143 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1145 _GLIBCXX20_CONSTEXPR
1146 tuple(allocator_arg_t __tag, const _Alloc& __a,
1147 const pair<_U1, _U2>& __in)
1148 : _Inherited(__tag, __a, __in.first, __in.second) { }
1150 template<typename _Alloc, typename _U1, typename _U2,
1151 _ImplicitCtor<true, _U1, _U2> = true>
1152 _GLIBCXX20_CONSTEXPR
1153 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
1154 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1155 std::forward<_U2>(__in.second)) { }
1157 template<typename _Alloc, typename _U1, typename _U2,
1158 _ExplicitCtor<true, _U1, _U2> = false>
1160 _GLIBCXX20_CONSTEXPR
1161 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
1162 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1163 std::forward<_U2>(__in.second)) { }
1165 // Tuple assignment.
1167 _GLIBCXX20_CONSTEXPR
1169 operator=(typename conditional<__assignable<const _T1&, const _T2&>(),
1171 const __nonesuch&>::type __in)
1172 noexcept(__nothrow_assignable<const _T1&, const _T2&>())
1174 this->_M_assign(__in);
1178 _GLIBCXX20_CONSTEXPR
1180 operator=(typename conditional<__assignable<_T1, _T2>(),
1182 __nonesuch&&>::type __in)
1183 noexcept(__nothrow_assignable<_T1, _T2>())
1185 this->_M_assign(std::move(__in));
1189 template<typename _U1, typename _U2>
1190 _GLIBCXX20_CONSTEXPR
1191 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
1192 operator=(const tuple<_U1, _U2>& __in)
1193 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1195 this->_M_assign(__in);
1199 template<typename _U1, typename _U2>
1200 _GLIBCXX20_CONSTEXPR
1201 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
1202 operator=(tuple<_U1, _U2>&& __in)
1203 noexcept(__nothrow_assignable<_U1, _U2>())
1205 this->_M_assign(std::move(__in));
1209 template<typename _U1, typename _U2>
1210 _GLIBCXX20_CONSTEXPR
1211 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
1212 operator=(const pair<_U1, _U2>& __in)
1213 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1215 this->_M_head(*this) = __in.first;
1216 this->_M_tail(*this)._M_head(*this) = __in.second;
1220 template<typename _U1, typename _U2>
1221 _GLIBCXX20_CONSTEXPR
1222 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
1223 operator=(pair<_U1, _U2>&& __in)
1224 noexcept(__nothrow_assignable<_U1, _U2>())
1226 this->_M_head(*this) = std::forward<_U1>(__in.first);
1227 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
1231 _GLIBCXX20_CONSTEXPR
1234 noexcept(__and_<__is_nothrow_swappable<_T1>,
1235 __is_nothrow_swappable<_T2>>::value)
1236 { _Inherited::_M_swap(__in); }
1240 /// class tuple_size
1241 template<typename... _Elements>
1242 struct tuple_size<tuple<_Elements...>>
1243 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
1245 #if __cplusplus > 201402L
1246 template <typename _Tp>
1247 inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
1251 * Recursive case for tuple_element: strip off the first element in
1252 * the tuple and retrieve the (i-1)th element of the remaining tuple.
1254 template<std::size_t __i, typename _Head, typename... _Tail>
1255 struct tuple_element<__i, tuple<_Head, _Tail...> >
1256 : tuple_element<__i - 1, tuple<_Tail...> > { };
1259 * Basis case for tuple_element: The first element is the one we're seeking.
1261 template<typename _Head, typename... _Tail>
1262 struct tuple_element<0, tuple<_Head, _Tail...> >
1268 * Error case for tuple_element: invalid index.
1270 template<size_t __i>
1271 struct tuple_element<__i, tuple<>>
1273 static_assert(__i < tuple_size<tuple<>>::value,
1274 "tuple index is in range");
1277 template<std::size_t __i, typename _Head, typename... _Tail>
1279 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1280 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1282 template<std::size_t __i, typename _Head, typename... _Tail>
1283 constexpr const _Head&
1284 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1285 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1287 /// Return a reference to the ith element of a tuple.
1288 template<std::size_t __i, typename... _Elements>
1289 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
1290 get(tuple<_Elements...>& __t) noexcept
1291 { return std::__get_helper<__i>(__t); }
1293 /// Return a const reference to the ith element of a const tuple.
1294 template<std::size_t __i, typename... _Elements>
1295 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
1296 get(const tuple<_Elements...>& __t) noexcept
1297 { return std::__get_helper<__i>(__t); }
1299 /// Return an rvalue reference to the ith element of a tuple rvalue.
1300 template<std::size_t __i, typename... _Elements>
1301 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
1302 get(tuple<_Elements...>&& __t) noexcept
1304 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1305 return std::forward<__element_type&&>(std::get<__i>(__t));
1308 /// Return a const rvalue reference to the ith element of a const tuple rvalue.
1309 template<std::size_t __i, typename... _Elements>
1310 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&&
1311 get(const tuple<_Elements...>&& __t) noexcept
1313 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1314 return std::forward<const __element_type&&>(std::get<__i>(__t));
1317 #if __cplusplus >= 201402L
1319 #define __cpp_lib_tuples_by_type 201304
1321 template<typename _Head, size_t __i, typename... _Tail>
1323 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1324 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1326 template<typename _Head, size_t __i, typename... _Tail>
1327 constexpr const _Head&
1328 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1329 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1331 /// Return a reference to the unique element of type _Tp of a tuple.
1332 template <typename _Tp, typename... _Types>
1334 get(tuple<_Types...>& __t) noexcept
1335 { return std::__get_helper2<_Tp>(__t); }
1337 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
1338 template <typename _Tp, typename... _Types>
1340 get(tuple<_Types...>&& __t) noexcept
1341 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
1343 /// Return a const reference to the unique element of type _Tp of a tuple.
1344 template <typename _Tp, typename... _Types>
1345 constexpr const _Tp&
1346 get(const tuple<_Types...>& __t) noexcept
1347 { return std::__get_helper2<_Tp>(__t); }
1349 /// Return a const reference to the unique element of type _Tp of
1350 /// a const tuple rvalue.
1351 template <typename _Tp, typename... _Types>
1352 constexpr const _Tp&&
1353 get(const tuple<_Types...>&& __t) noexcept
1354 { return std::forward<const _Tp&&>(std::__get_helper2<_Tp>(__t)); }
1357 // This class performs the comparison operations on tuples
1358 template<typename _Tp, typename _Up, size_t __i, size_t __size>
1359 struct __tuple_compare
1361 static constexpr bool
1362 __eq(const _Tp& __t, const _Up& __u)
1364 return bool(std::get<__i>(__t) == std::get<__i>(__u))
1365 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
1368 static constexpr bool
1369 __less(const _Tp& __t, const _Up& __u)
1371 return bool(std::get<__i>(__t) < std::get<__i>(__u))
1372 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
1373 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
1377 template<typename _Tp, typename _Up, size_t __size>
1378 struct __tuple_compare<_Tp, _Up, __size, __size>
1380 static constexpr bool
1381 __eq(const _Tp&, const _Up&) { return true; }
1383 static constexpr bool
1384 __less(const _Tp&, const _Up&) { return false; }
1387 template<typename... _TElements, typename... _UElements>
1389 operator==(const tuple<_TElements...>& __t,
1390 const tuple<_UElements...>& __u)
1392 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1393 "tuple objects can only be compared if they have equal sizes.");
1394 using __compare = __tuple_compare<tuple<_TElements...>,
1395 tuple<_UElements...>,
1396 0, sizeof...(_TElements)>;
1397 return __compare::__eq(__t, __u);
1400 template<typename... _TElements, typename... _UElements>
1402 operator<(const tuple<_TElements...>& __t,
1403 const tuple<_UElements...>& __u)
1405 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1406 "tuple objects can only be compared if they have equal sizes.");
1407 using __compare = __tuple_compare<tuple<_TElements...>,
1408 tuple<_UElements...>,
1409 0, sizeof...(_TElements)>;
1410 return __compare::__less(__t, __u);
1413 template<typename... _TElements, typename... _UElements>
1415 operator!=(const tuple<_TElements...>& __t,
1416 const tuple<_UElements...>& __u)
1417 { return !(__t == __u); }
1419 template<typename... _TElements, typename... _UElements>
1421 operator>(const tuple<_TElements...>& __t,
1422 const tuple<_UElements...>& __u)
1423 { return __u < __t; }
1425 template<typename... _TElements, typename... _UElements>
1427 operator<=(const tuple<_TElements...>& __t,
1428 const tuple<_UElements...>& __u)
1429 { return !(__u < __t); }
1431 template<typename... _TElements, typename... _UElements>
1433 operator>=(const tuple<_TElements...>& __t,
1434 const tuple<_UElements...>& __u)
1435 { return !(__t < __u); }
1438 template<typename... _Elements>
1439 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
1440 make_tuple(_Elements&&... __args)
1442 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
1444 return __result_type(std::forward<_Elements>(__args)...);
1447 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1448 // 2275. Why is forward_as_tuple not constexpr?
1449 /// std::forward_as_tuple
1450 template<typename... _Elements>
1451 constexpr tuple<_Elements&&...>
1452 forward_as_tuple(_Elements&&... __args) noexcept
1453 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
1455 template<size_t, typename, typename, size_t>
1456 struct __make_tuple_impl;
1458 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
1459 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
1460 : __make_tuple_impl<_Idx + 1,
1461 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
1465 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
1466 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
1468 typedef tuple<_Tp...> __type;
1471 template<typename _Tuple>
1472 struct __do_make_tuple
1473 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
1476 // Returns the std::tuple equivalent of a tuple-like type.
1477 template<typename _Tuple>
1479 : public __do_make_tuple<__remove_cvref_t<_Tuple>>
1482 // Combines several std::tuple's into a single one.
1483 template<typename...>
1484 struct __combine_tuples;
1487 struct __combine_tuples<>
1489 typedef tuple<> __type;
1492 template<typename... _Ts>
1493 struct __combine_tuples<tuple<_Ts...>>
1495 typedef tuple<_Ts...> __type;
1498 template<typename... _T1s, typename... _T2s, typename... _Rem>
1499 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
1501 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
1502 _Rem...>::__type __type;
1505 // Computes the result type of tuple_cat given a set of tuple-like types.
1506 template<typename... _Tpls>
1507 struct __tuple_cat_result
1509 typedef typename __combine_tuples
1510 <typename __make_tuple<_Tpls>::__type...>::__type __type;
1513 // Helper to determine the index set for the first tuple-like
1514 // type of a given set.
1515 template<typename...>
1516 struct __make_1st_indices;
1519 struct __make_1st_indices<>
1521 typedef std::_Index_tuple<> __type;
1524 template<typename _Tp, typename... _Tpls>
1525 struct __make_1st_indices<_Tp, _Tpls...>
1527 typedef typename std::_Build_index_tuple<std::tuple_size<
1528 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1531 // Performs the actual concatenation by step-wise expanding tuple-like
1532 // objects into the elements, which are finally forwarded into the
1534 template<typename _Ret, typename _Indices, typename... _Tpls>
1535 struct __tuple_concater;
1537 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1538 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1540 template<typename... _Us>
1541 static constexpr _Ret
1542 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1544 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1545 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
1546 return __next::_S_do(std::forward<_Tpls>(__tps)...,
1547 std::forward<_Us>(__us)...,
1548 std::get<_Is>(std::forward<_Tp>(__tp))...);
1552 template<typename _Ret>
1553 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1555 template<typename... _Us>
1556 static constexpr _Ret
1557 _S_do(_Us&&... __us)
1559 return _Ret(std::forward<_Us>(__us)...);
1564 template<typename... _Tpls, typename = typename
1565 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1567 tuple_cat(_Tpls&&... __tpls)
1568 -> typename __tuple_cat_result<_Tpls...>::__type
1570 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1571 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1572 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1573 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1576 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1577 // 2301. Why is tie not constexpr?
1579 template<typename... _Elements>
1580 constexpr tuple<_Elements&...>
1581 tie(_Elements&... __args) noexcept
1582 { return tuple<_Elements&...>(__args...); }
1585 template<typename... _Elements>
1586 _GLIBCXX20_CONSTEXPR
1588 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1589 // Constrained free swap overload, see p0185r1
1590 typename enable_if<__and_<__is_swappable<_Elements>...>::value
1595 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1596 noexcept(noexcept(__x.swap(__y)))
1599 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1600 template<typename... _Elements>
1601 _GLIBCXX20_CONSTEXPR
1602 typename enable_if<!__and_<__is_swappable<_Elements>...>::value>::type
1603 swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
1606 // A class (and instance) which can be used in 'tie' when an element
1607 // of a tuple is not required.
1608 // _GLIBCXX14_CONSTEXPR
1609 // 2933. PR for LWG 2773 could be clearer
1610 struct _Swallow_assign
1613 _GLIBCXX14_CONSTEXPR const _Swallow_assign&
1614 operator=(const _Tp&) const
1618 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1619 // 2773. Making std::ignore constexpr
1620 _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
1622 /// Partial specialization for tuples
1623 template<typename... _Types, typename _Alloc>
1624 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1626 // See stl_pair.h...
1627 /** "piecewise construction" using a tuple of arguments for each member.
1629 * @param __first Arguments for the first member of the pair.
1630 * @param __second Arguments for the second member of the pair.
1632 * The elements of each tuple will be used as the constructor arguments
1633 * for the data members of the pair.
1635 template<class _T1, class _T2>
1636 template<typename... _Args1, typename... _Args2>
1637 _GLIBCXX20_CONSTEXPR
1640 pair(piecewise_construct_t,
1641 tuple<_Args1...> __first, tuple<_Args2...> __second)
1642 : pair(__first, __second,
1643 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1644 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1647 template<class _T1, class _T2>
1648 template<typename... _Args1, std::size_t... _Indexes1,
1649 typename... _Args2, std::size_t... _Indexes2>
1650 _GLIBCXX20_CONSTEXPR inline
1652 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1653 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1654 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1655 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1658 #if __cplusplus >= 201703L
1660 // Unpack a std::tuple into a type trait and use its value.
1661 // For cv std::tuple<_Up> the result is _Trait<_Tp, cv _Up...>::value.
1662 // For cv std::tuple<_Up>& the result is _Trait<_Tp, cv _Up&...>::value.
1663 // Otherwise the result is false (because we don't know if std::get throws).
1664 template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
1665 inline constexpr bool __unpack_std_tuple = false;
1667 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1668 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>>
1669 = _Trait<_Tp, _Up...>::value;
1671 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1672 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>&>
1673 = _Trait<_Tp, _Up&...>::value;
1675 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1676 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>>
1677 = _Trait<_Tp, const _Up...>::value;
1679 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1680 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>&>
1681 = _Trait<_Tp, const _Up&...>::value;
1683 # define __cpp_lib_apply 201603
1685 template <typename _Fn, typename _Tuple, size_t... _Idx>
1686 constexpr decltype(auto)
1687 __apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
1689 return std::__invoke(std::forward<_Fn>(__f),
1690 std::get<_Idx>(std::forward<_Tuple>(__t))...);
1693 template <typename _Fn, typename _Tuple>
1694 constexpr decltype(auto)
1695 apply(_Fn&& __f, _Tuple&& __t)
1696 noexcept(__unpack_std_tuple<is_nothrow_invocable, _Fn, _Tuple>)
1699 = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
1700 return std::__apply_impl(std::forward<_Fn>(__f),
1701 std::forward<_Tuple>(__t),
1705 #define __cpp_lib_make_from_tuple 201606
1707 template <typename _Tp, typename _Tuple, size_t... _Idx>
1709 __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
1710 { return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...); }
1712 template <typename _Tp, typename _Tuple>
1714 make_from_tuple(_Tuple&& __t)
1715 noexcept(__unpack_std_tuple<is_nothrow_constructible, _Tp, _Tuple>)
1717 return __make_from_tuple_impl<_Tp>(
1718 std::forward<_Tuple>(__t),
1719 make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>{});
1725 _GLIBCXX_END_NAMESPACE_VERSION
1730 #endif // _GLIBCXX_TUPLE