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>
42 #if __cplusplus > 201703L
44 # define __cpp_lib_constexpr_tuple 201811L
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 * @addtogroup utilities
56 template<typename... _Elements>
59 template<typename _Tp>
60 struct __is_empty_non_tuple : is_empty<_Tp> { };
62 // Using EBO for elements that are tuples causes ambiguous base errors.
63 template<typename _El0, typename... _El>
64 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
66 // Use the Empty Base-class Optimization for empty, non-final types.
67 template<typename _Tp>
68 using __empty_not_final
69 = typename conditional<__is_final(_Tp), false_type,
70 __is_empty_non_tuple<_Tp>>::type;
72 template<size_t _Idx, typename _Head,
73 bool = __empty_not_final<_Head>::value>
76 #if __has_cpp_attribute(no_unique_address)
77 template<size_t _Idx, typename _Head>
78 struct _Head_base<_Idx, _Head, true>
80 constexpr _Head_base()
83 constexpr _Head_base(const _Head& __h)
84 : _M_head_impl(__h) { }
86 constexpr _Head_base(const _Head_base&) = default;
87 constexpr _Head_base(_Head_base&&) = default;
89 template<typename _UHead>
90 constexpr _Head_base(_UHead&& __h)
91 : _M_head_impl(std::forward<_UHead>(__h)) { }
94 _Head_base(allocator_arg_t, __uses_alloc0)
97 template<typename _Alloc>
98 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
99 : _M_head_impl(allocator_arg, *__a._M_a) { }
101 template<typename _Alloc>
102 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
103 : _M_head_impl(*__a._M_a) { }
105 template<typename _UHead>
107 _Head_base(__uses_alloc0, _UHead&& __uhead)
108 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
110 template<typename _Alloc, typename _UHead>
111 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
112 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
115 template<typename _Alloc, typename _UHead>
116 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
117 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
119 static constexpr _Head&
120 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
122 static constexpr const _Head&
123 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
125 [[no_unique_address]] _Head _M_head_impl;
128 template<size_t _Idx, typename _Head>
129 struct _Head_base<_Idx, _Head, true>
132 constexpr _Head_base()
135 constexpr _Head_base(const _Head& __h)
138 constexpr _Head_base(const _Head_base&) = default;
139 constexpr _Head_base(_Head_base&&) = default;
141 template<typename _UHead>
142 constexpr _Head_base(_UHead&& __h)
143 : _Head(std::forward<_UHead>(__h)) { }
145 _Head_base(allocator_arg_t, __uses_alloc0)
148 template<typename _Alloc>
149 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
150 : _Head(allocator_arg, *__a._M_a) { }
152 template<typename _Alloc>
153 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
154 : _Head(*__a._M_a) { }
156 template<typename _UHead>
157 _Head_base(__uses_alloc0, _UHead&& __uhead)
158 : _Head(std::forward<_UHead>(__uhead)) { }
160 template<typename _Alloc, typename _UHead>
161 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
162 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
164 template<typename _Alloc, typename _UHead>
165 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
166 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
168 static constexpr _Head&
169 _M_head(_Head_base& __b) noexcept { return __b; }
171 static constexpr const _Head&
172 _M_head(const _Head_base& __b) noexcept { return __b; }
176 template<size_t _Idx, typename _Head>
177 struct _Head_base<_Idx, _Head, false>
179 constexpr _Head_base()
182 constexpr _Head_base(const _Head& __h)
183 : _M_head_impl(__h) { }
185 constexpr _Head_base(const _Head_base&) = default;
186 constexpr _Head_base(_Head_base&&) = default;
188 template<typename _UHead>
189 constexpr _Head_base(_UHead&& __h)
190 : _M_head_impl(std::forward<_UHead>(__h)) { }
193 _Head_base(allocator_arg_t, __uses_alloc0)
196 template<typename _Alloc>
197 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
198 : _M_head_impl(allocator_arg, *__a._M_a) { }
200 template<typename _Alloc>
201 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
202 : _M_head_impl(*__a._M_a) { }
204 template<typename _UHead>
206 _Head_base(__uses_alloc0, _UHead&& __uhead)
207 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
209 template<typename _Alloc, typename _UHead>
210 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
211 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
214 template<typename _Alloc, typename _UHead>
215 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
216 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
218 static constexpr _Head&
219 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
221 static constexpr const _Head&
222 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
228 * Contains the actual implementation of the @c tuple template, stored
229 * as a recursive inheritance hierarchy from the first element (most
230 * derived class) to the last (least derived class). The @c Idx
231 * parameter gives the 0-based index of the element stored at this
232 * point in the hierarchy; we use it to implement a constant-time
235 template<size_t _Idx, typename... _Elements>
239 * Recursive tuple implementation. Here we store the @c Head element
240 * and derive from a @c Tuple_impl containing the remaining elements
241 * (which contains the @c Tail).
243 template<size_t _Idx, typename _Head, typename... _Tail>
244 struct _Tuple_impl<_Idx, _Head, _Tail...>
245 : public _Tuple_impl<_Idx + 1, _Tail...>,
246 private _Head_base<_Idx, _Head>
248 template<size_t, typename...> friend struct _Tuple_impl;
250 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
251 typedef _Head_base<_Idx, _Head> _Base;
253 static constexpr _Head&
254 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
256 static constexpr const _Head&
257 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
259 static constexpr _Inherited&
260 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
262 static constexpr const _Inherited&
263 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
265 constexpr _Tuple_impl()
266 : _Inherited(), _Base() { }
269 _Tuple_impl(const _Head& __head, const _Tail&... __tail)
270 : _Inherited(__tail...), _Base(__head)
273 template<typename _UHead, typename... _UTail,
274 typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
276 _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
277 : _Inherited(std::forward<_UTail>(__tail)...),
278 _Base(std::forward<_UHead>(__head))
281 constexpr _Tuple_impl(const _Tuple_impl&) = default;
283 // _GLIBCXX_RESOLVE_LIB_DEFECTS
284 // 2729. Missing SFINAE on std::pair::operator=
285 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
288 _Tuple_impl(_Tuple_impl&& __in)
289 noexcept(__and_<is_nothrow_move_constructible<_Head>,
290 is_nothrow_move_constructible<_Inherited>>::value)
291 : _Inherited(std::move(_M_tail(__in))),
292 _Base(std::forward<_Head>(_M_head(__in)))
295 template<typename... _UElements>
297 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
298 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
299 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in))
302 template<typename _UHead, typename... _UTails>
304 _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
305 : _Inherited(std::move
306 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
307 _Base(std::forward<_UHead>
308 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
311 template<typename _Alloc>
313 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
314 : _Inherited(__tag, __a),
315 _Base(__tag, __use_alloc<_Head>(__a))
318 template<typename _Alloc>
319 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
320 const _Head& __head, const _Tail&... __tail)
321 : _Inherited(__tag, __a, __tail...),
322 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head)
325 template<typename _Alloc, typename _UHead, typename... _UTail,
326 typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
328 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
329 _UHead&& __head, _UTail&&... __tail)
330 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
331 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
332 std::forward<_UHead>(__head))
335 template<typename _Alloc>
337 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
338 const _Tuple_impl& __in)
339 : _Inherited(__tag, __a, _M_tail(__in)),
340 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in))
343 template<typename _Alloc>
345 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
347 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
348 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
349 std::forward<_Head>(_M_head(__in)))
352 template<typename _Alloc, typename _UHead, typename... _UTails>
354 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
355 const _Tuple_impl<_Idx, _UHead, _UTails...>& __in)
356 : _Inherited(__tag, __a,
357 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)),
358 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
359 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))
362 template<typename _Alloc, typename _UHead, typename... _UTails>
364 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
365 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
366 : _Inherited(__tag, __a, std::move
367 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
368 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
370 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
373 template<typename... _UElements>
376 _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in)
378 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
379 _M_tail(*this)._M_assign(
380 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
383 template<typename _UHead, typename... _UTails>
386 _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
388 _M_head(*this) = std::forward<_UHead>
389 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
390 _M_tail(*this)._M_assign(
391 std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
397 _M_swap(_Tuple_impl& __in)
400 swap(_M_head(*this), _M_head(__in));
401 _Inherited::_M_swap(_M_tail(__in));
405 // Basis case of inheritance recursion.
406 template<size_t _Idx, typename _Head>
407 struct _Tuple_impl<_Idx, _Head>
408 : private _Head_base<_Idx, _Head>
410 template<size_t, typename...> friend struct _Tuple_impl;
412 typedef _Head_base<_Idx, _Head> _Base;
414 static constexpr _Head&
415 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
417 static constexpr const _Head&
418 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
425 _Tuple_impl(const _Head& __head)
429 template<typename _UHead>
431 _Tuple_impl(_UHead&& __head)
432 : _Base(std::forward<_UHead>(__head))
435 constexpr _Tuple_impl(const _Tuple_impl&) = default;
437 // _GLIBCXX_RESOLVE_LIB_DEFECTS
438 // 2729. Missing SFINAE on std::pair::operator=
439 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
442 _Tuple_impl(_Tuple_impl&& __in)
443 noexcept(is_nothrow_move_constructible<_Head>::value)
444 : _Base(std::forward<_Head>(_M_head(__in)))
447 template<typename _UHead>
449 _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
450 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in))
453 template<typename _UHead>
455 _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
456 : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
459 template<typename _Alloc>
461 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
462 : _Base(__tag, __use_alloc<_Head>(__a))
465 template<typename _Alloc>
466 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
468 : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), __head)
471 template<typename _Alloc, typename _UHead>
473 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
475 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
476 std::forward<_UHead>(__head))
479 template<typename _Alloc>
481 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
482 const _Tuple_impl& __in)
483 : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), _M_head(__in))
486 template<typename _Alloc>
488 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
490 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
491 std::forward<_Head>(_M_head(__in)))
494 template<typename _Alloc, typename _UHead>
496 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
497 const _Tuple_impl<_Idx, _UHead>& __in)
498 : _Base(__use_alloc<_Head, _Alloc, const _UHead&>(__a),
499 _Tuple_impl<_Idx, _UHead>::_M_head(__in))
502 template<typename _Alloc, typename _UHead>
504 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
505 _Tuple_impl<_Idx, _UHead>&& __in)
506 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
507 std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
510 template<typename _UHead>
513 _M_assign(const _Tuple_impl<_Idx, _UHead>& __in)
515 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
518 template<typename _UHead>
521 _M_assign(_Tuple_impl<_Idx, _UHead>&& __in)
524 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
530 _M_swap(_Tuple_impl& __in)
533 swap(_M_head(*this), _M_head(__in));
537 // Concept utility functions, reused in conditionally-explicit
539 template<bool, typename... _Types>
540 struct _TupleConstraints
542 template<typename _Tp, typename _Up> // Workaround for PR 96592
543 using is_constructible
544 = __bool_constant<__is_constructible(_Tp, _Up)>;
546 // Constraint for a non-explicit constructor.
547 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
548 // and every Ui is implicitly convertible to Ti.
549 template<typename... _UTypes>
550 static constexpr bool __is_implicitly_constructible()
552 return __and_<is_constructible<_Types, _UTypes>...,
553 is_convertible<_UTypes, _Types>...
557 // Constraint for a non-explicit constructor.
558 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
559 // but not every Ui is implicitly convertible to Ti.
560 template<typename... _UTypes>
561 static constexpr bool __is_explicitly_constructible()
563 return __and_<is_constructible<_Types, _UTypes>...,
564 __not_<__and_<is_convertible<_UTypes, _Types>...>>
568 static constexpr bool __is_implicitly_default_constructible()
570 return __and_<std::__is_implicitly_default_constructible<_Types>...
574 static constexpr bool __is_explicitly_default_constructible()
576 return __and_<is_default_constructible<_Types>...,
578 std::__is_implicitly_default_constructible<_Types>...>
583 // Partial specialization used when a required precondition isn't met,
584 // e.g. when sizeof...(_Types) != sizeof...(_UTypes).
585 template<typename... _Types>
586 struct _TupleConstraints<false, _Types...>
588 template<typename... _UTypes>
589 static constexpr bool __is_implicitly_constructible()
592 template<typename... _UTypes>
593 static constexpr bool __is_explicitly_constructible()
597 /// Primary class template, tuple
598 template<typename... _Elements>
599 class tuple : public _Tuple_impl<0, _Elements...>
601 typedef _Tuple_impl<0, _Elements...> _Inherited;
604 using _TCC = _TupleConstraints<_Cond, _Elements...>;
606 // Constraint for non-explicit default constructor
607 template<bool _Dummy>
608 using _ImplicitDefaultCtor = __enable_if_t<
609 _TCC<_Dummy>::__is_implicitly_default_constructible(),
612 // Constraint for explicit default constructor
613 template<bool _Dummy>
614 using _ExplicitDefaultCtor = __enable_if_t<
615 _TCC<_Dummy>::__is_explicitly_default_constructible(),
618 // Constraint for non-explicit constructors
619 template<bool _Cond, typename... _Args>
620 using _ImplicitCtor = __enable_if_t<
621 _TCC<_Cond>::template __is_implicitly_constructible<_Args...>(),
624 // Constraint for non-explicit constructors
625 template<bool _Cond, typename... _Args>
626 using _ExplicitCtor = __enable_if_t<
627 _TCC<_Cond>::template __is_explicitly_constructible<_Args...>(),
630 template<typename... _UElements>
632 __enable_if_t<sizeof...(_UElements) == sizeof...(_Elements), bool>
634 { return __and_<is_assignable<_Elements&, _UElements>...>::value; }
636 // Condition for noexcept-specifier of an assignment operator.
637 template<typename... _UElements>
638 static constexpr bool __nothrow_assignable()
641 __and_<is_nothrow_assignable<_Elements&, _UElements>...>::value;
644 // Condition for noexcept-specifier of a constructor.
645 template<typename... _UElements>
646 static constexpr bool __nothrow_constructible()
649 __and_<is_nothrow_constructible<_Elements, _UElements>...>::value;
652 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) == 1.
653 template<typename _Up>
654 static constexpr bool __valid_args()
656 return sizeof...(_Elements) == 1
657 && !is_same<tuple, __remove_cvref_t<_Up>>::value;
660 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) > 1.
661 template<typename, typename, typename... _Tail>
662 static constexpr bool __valid_args()
663 { return (sizeof...(_Tail) + 2) == sizeof...(_Elements); }
665 /* Constraint for constructors with a tuple<UTypes...> parameter ensures
666 * that the constructor is only viable when it would not interfere with
667 * tuple(UTypes&&...) or tuple(const tuple&) or tuple(tuple&&).
668 * Such constructors are only viable if:
669 * either sizeof...(Types) != 1,
670 * or (when Types... expands to T and UTypes... expands to U)
671 * is_convertible_v<TUPLE, T>, is_constructible_v<T, TUPLE>,
672 * and is_same_v<T, U> are all false.
674 template<typename _Tuple, typename = tuple,
675 typename = __remove_cvref_t<_Tuple>>
679 // If TUPLE is convertible to the single element in *this,
680 // then TUPLE should match tuple(UTypes&&...) instead.
681 template<typename _Tuple, typename _Tp, typename _Up>
682 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Up>>
683 : __or_<is_convertible<_Tuple, _Tp>, is_constructible<_Tp, _Tuple>>
685 // If TUPLE and *this each have a single element of the same type,
686 // then TUPLE should match a copy/move constructor instead.
687 template<typename _Tuple, typename _Tp>
688 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Tp>>
692 // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
693 // and the single element in Types can be initialized from TUPLE,
694 // or is the same type as tuple_element_t<0, TUPLE>.
695 template<typename _Tuple>
696 static constexpr bool __use_other_ctor()
697 { return _UseOtherCtor<_Tuple>::value; }
700 template<typename _Dummy = void,
701 _ImplicitDefaultCtor<is_void<_Dummy>::value> = true>
704 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
707 template<typename _Dummy = void,
708 _ExplicitDefaultCtor<is_void<_Dummy>::value> = false>
711 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
714 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
715 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
717 tuple(const _Elements&... __elements)
718 noexcept(__nothrow_constructible<const _Elements&...>())
719 : _Inherited(__elements...) { }
721 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
722 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
724 tuple(const _Elements&... __elements)
725 noexcept(__nothrow_constructible<const _Elements&...>())
726 : _Inherited(__elements...) { }
728 template<typename... _UElements,
729 bool _Valid = __valid_args<_UElements...>(),
730 _ImplicitCtor<_Valid, _UElements...> = true>
732 tuple(_UElements&&... __elements)
733 noexcept(__nothrow_constructible<_UElements...>())
734 : _Inherited(std::forward<_UElements>(__elements)...) { }
736 template<typename... _UElements,
737 bool _Valid = __valid_args<_UElements...>(),
738 _ExplicitCtor<_Valid, _UElements...> = false>
740 tuple(_UElements&&... __elements)
741 noexcept(__nothrow_constructible<_UElements...>())
742 : _Inherited(std::forward<_UElements>(__elements)...) { }
744 constexpr tuple(const tuple&) = default;
746 constexpr tuple(tuple&&) = default;
748 template<typename... _UElements,
749 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
750 && !__use_other_ctor<const tuple<_UElements...>&>(),
751 _ImplicitCtor<_Valid, const _UElements&...> = true>
753 tuple(const tuple<_UElements...>& __in)
754 noexcept(__nothrow_constructible<const _UElements&...>())
755 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
758 template<typename... _UElements,
759 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
760 && !__use_other_ctor<const tuple<_UElements...>&>(),
761 _ExplicitCtor<_Valid, const _UElements&...> = false>
763 tuple(const tuple<_UElements...>& __in)
764 noexcept(__nothrow_constructible<const _UElements&...>())
765 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
768 template<typename... _UElements,
769 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
770 && !__use_other_ctor<tuple<_UElements...>&&>(),
771 _ImplicitCtor<_Valid, _UElements...> = true>
773 tuple(tuple<_UElements...>&& __in)
774 noexcept(__nothrow_constructible<_UElements...>())
775 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
777 template<typename... _UElements,
778 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
779 && !__use_other_ctor<tuple<_UElements...>&&>(),
780 _ExplicitCtor<_Valid, _UElements...> = false>
782 tuple(tuple<_UElements...>&& __in)
783 noexcept(__nothrow_constructible<_UElements...>())
784 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
786 // Allocator-extended constructors.
788 template<typename _Alloc,
789 _ImplicitDefaultCtor<is_object<_Alloc>::value> = true>
791 tuple(allocator_arg_t __tag, const _Alloc& __a)
792 : _Inherited(__tag, __a) { }
794 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
795 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
797 tuple(allocator_arg_t __tag, const _Alloc& __a,
798 const _Elements&... __elements)
799 : _Inherited(__tag, __a, __elements...) { }
801 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
802 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
805 tuple(allocator_arg_t __tag, const _Alloc& __a,
806 const _Elements&... __elements)
807 : _Inherited(__tag, __a, __elements...) { }
809 template<typename _Alloc, typename... _UElements,
810 bool _Valid = __valid_args<_UElements...>(),
811 _ImplicitCtor<_Valid, _UElements...> = true>
813 tuple(allocator_arg_t __tag, const _Alloc& __a,
814 _UElements&&... __elements)
815 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
818 template<typename _Alloc, typename... _UElements,
819 bool _Valid = __valid_args<_UElements...>(),
820 _ExplicitCtor<_Valid, _UElements...> = false>
823 tuple(allocator_arg_t __tag, const _Alloc& __a,
824 _UElements&&... __elements)
825 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
828 template<typename _Alloc>
830 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
831 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
833 template<typename _Alloc>
835 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
836 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
838 template<typename _Alloc, typename... _UElements,
839 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
840 && !__use_other_ctor<const tuple<_UElements...>&>(),
841 _ImplicitCtor<_Valid, const _UElements&...> = true>
843 tuple(allocator_arg_t __tag, const _Alloc& __a,
844 const tuple<_UElements...>& __in)
845 : _Inherited(__tag, __a,
846 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
849 template<typename _Alloc, typename... _UElements,
850 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
851 && !__use_other_ctor<const tuple<_UElements...>&>(),
852 _ExplicitCtor<_Valid, const _UElements&...> = false>
855 tuple(allocator_arg_t __tag, const _Alloc& __a,
856 const tuple<_UElements...>& __in)
857 : _Inherited(__tag, __a,
858 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
861 template<typename _Alloc, typename... _UElements,
862 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
863 && !__use_other_ctor<tuple<_UElements...>&&>(),
864 _ImplicitCtor<_Valid, _UElements...> = true>
866 tuple(allocator_arg_t __tag, const _Alloc& __a,
867 tuple<_UElements...>&& __in)
868 : _Inherited(__tag, __a,
869 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
872 template<typename _Alloc, typename... _UElements,
873 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
874 && !__use_other_ctor<tuple<_UElements...>&&>(),
875 _ExplicitCtor<_Valid, _UElements...> = false>
878 tuple(allocator_arg_t __tag, const _Alloc& __a,
879 tuple<_UElements...>&& __in)
880 : _Inherited(__tag, __a,
881 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
888 operator=(typename conditional<__assignable<const _Elements&...>(),
890 const __nonesuch&>::type __in)
891 noexcept(__nothrow_assignable<const _Elements&...>())
893 this->_M_assign(__in);
899 operator=(typename conditional<__assignable<_Elements...>(),
901 __nonesuch&&>::type __in)
902 noexcept(__nothrow_assignable<_Elements...>())
904 this->_M_assign(std::move(__in));
908 template<typename... _UElements>
910 __enable_if_t<__assignable<const _UElements&...>(), tuple&>
911 operator=(const tuple<_UElements...>& __in)
912 noexcept(__nothrow_assignable<const _UElements&...>())
914 this->_M_assign(__in);
918 template<typename... _UElements>
920 __enable_if_t<__assignable<_UElements...>(), tuple&>
921 operator=(tuple<_UElements...>&& __in)
922 noexcept(__nothrow_assignable<_UElements...>())
924 this->_M_assign(std::move(__in));
932 noexcept(__and_<__is_nothrow_swappable<_Elements>...>::value)
933 { _Inherited::_M_swap(__in); }
936 #if __cpp_deduction_guides >= 201606
937 template<typename... _UTypes>
938 tuple(_UTypes...) -> tuple<_UTypes...>;
939 template<typename _T1, typename _T2>
940 tuple(pair<_T1, _T2>) -> tuple<_T1, _T2>;
941 template<typename _Alloc, typename... _UTypes>
942 tuple(allocator_arg_t, _Alloc, _UTypes...) -> tuple<_UTypes...>;
943 template<typename _Alloc, typename _T1, typename _T2>
944 tuple(allocator_arg_t, _Alloc, pair<_T1, _T2>) -> tuple<_T1, _T2>;
945 template<typename _Alloc, typename... _UTypes>
946 tuple(allocator_arg_t, _Alloc, tuple<_UTypes...>) -> tuple<_UTypes...>;
949 // Explicit specialization, zero-element tuple.
954 void swap(tuple&) noexcept { /* no-op */ }
955 // We need the default since we're going to define no-op
956 // allocator constructors.
958 // No-op allocator constructors.
959 template<typename _Alloc>
961 tuple(allocator_arg_t, const _Alloc&) noexcept { }
962 template<typename _Alloc>
964 tuple(allocator_arg_t, const _Alloc&, const tuple&) noexcept { }
967 /// Partial specialization, 2-element tuple.
968 /// Includes construction and assignment from a pair.
969 template<typename _T1, typename _T2>
970 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
972 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
974 // Constraint for non-explicit default constructor
975 template<bool _Dummy, typename _U1, typename _U2>
976 using _ImplicitDefaultCtor = __enable_if_t<
977 _TupleConstraints<_Dummy, _U1, _U2>::
978 __is_implicitly_default_constructible(),
981 // Constraint for explicit default constructor
982 template<bool _Dummy, typename _U1, typename _U2>
983 using _ExplicitDefaultCtor = __enable_if_t<
984 _TupleConstraints<_Dummy, _U1, _U2>::
985 __is_explicitly_default_constructible(),
988 template<bool _Dummy>
989 using _TCC = _TupleConstraints<_Dummy, _T1, _T2>;
991 // Constraint for non-explicit constructors
992 template<bool _Cond, typename _U1, typename _U2>
993 using _ImplicitCtor = __enable_if_t<
994 _TCC<_Cond>::template __is_implicitly_constructible<_U1, _U2>(),
997 // Constraint for non-explicit constructors
998 template<bool _Cond, typename _U1, typename _U2>
999 using _ExplicitCtor = __enable_if_t<
1000 _TCC<_Cond>::template __is_explicitly_constructible<_U1, _U2>(),
1003 template<typename _U1, typename _U2>
1004 static constexpr bool __assignable()
1006 return __and_<is_assignable<_T1&, _U1>,
1007 is_assignable<_T2&, _U2>>::value;
1010 template<typename _U1, typename _U2>
1011 static constexpr bool __nothrow_assignable()
1013 return __and_<is_nothrow_assignable<_T1&, _U1>,
1014 is_nothrow_assignable<_T2&, _U2>>::value;
1017 template<typename _U1, typename _U2>
1018 static constexpr bool __nothrow_constructible()
1020 return __and_<is_nothrow_constructible<_T1, _U1>,
1021 is_nothrow_constructible<_T2, _U2>>::value;
1024 static constexpr bool __nothrow_default_constructible()
1026 return __and_<is_nothrow_default_constructible<_T1>,
1027 is_nothrow_default_constructible<_T2>>::value;
1030 template<typename _U1>
1031 static constexpr bool __is_alloc_arg()
1032 { return is_same<__remove_cvref_t<_U1>, allocator_arg_t>::value; }
1035 template<bool _Dummy = true,
1036 _ImplicitDefaultCtor<_Dummy, _T1, _T2> = true>
1039 noexcept(__nothrow_default_constructible())
1042 template<bool _Dummy = true,
1043 _ExplicitDefaultCtor<_Dummy, _T1, _T2> = false>
1046 noexcept(__nothrow_default_constructible())
1049 template<bool _Dummy = true,
1050 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
1052 tuple(const _T1& __a1, const _T2& __a2)
1053 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
1054 : _Inherited(__a1, __a2) { }
1056 template<bool _Dummy = true,
1057 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
1059 tuple(const _T1& __a1, const _T2& __a2)
1060 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
1061 : _Inherited(__a1, __a2) { }
1063 template<typename _U1, typename _U2,
1064 _ImplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = true>
1066 tuple(_U1&& __a1, _U2&& __a2)
1067 noexcept(__nothrow_constructible<_U1, _U2>())
1068 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
1070 template<typename _U1, typename _U2,
1071 _ExplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = false>
1073 tuple(_U1&& __a1, _U2&& __a2)
1074 noexcept(__nothrow_constructible<_U1, _U2>())
1075 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
1077 constexpr tuple(const tuple&) = default;
1079 constexpr tuple(tuple&&) = default;
1081 template<typename _U1, typename _U2,
1082 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1084 tuple(const tuple<_U1, _U2>& __in)
1085 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1086 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
1088 template<typename _U1, typename _U2,
1089 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1091 tuple(const tuple<_U1, _U2>& __in)
1092 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1093 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
1095 template<typename _U1, typename _U2,
1096 _ImplicitCtor<true, _U1, _U2> = true>
1098 tuple(tuple<_U1, _U2>&& __in)
1099 noexcept(__nothrow_constructible<_U1, _U2>())
1100 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
1102 template<typename _U1, typename _U2,
1103 _ExplicitCtor<true, _U1, _U2> = false>
1105 tuple(tuple<_U1, _U2>&& __in)
1106 noexcept(__nothrow_constructible<_U1, _U2>())
1107 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
1109 template<typename _U1, typename _U2,
1110 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1112 tuple(const pair<_U1, _U2>& __in)
1113 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1114 : _Inherited(__in.first, __in.second) { }
1116 template<typename _U1, typename _U2,
1117 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1119 tuple(const pair<_U1, _U2>& __in)
1120 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1121 : _Inherited(__in.first, __in.second) { }
1123 template<typename _U1, typename _U2,
1124 _ImplicitCtor<true, _U1, _U2> = true>
1126 tuple(pair<_U1, _U2>&& __in)
1127 noexcept(__nothrow_constructible<_U1, _U2>())
1128 : _Inherited(std::forward<_U1>(__in.first),
1129 std::forward<_U2>(__in.second)) { }
1131 template<typename _U1, typename _U2,
1132 _ExplicitCtor<true, _U1, _U2> = false>
1134 tuple(pair<_U1, _U2>&& __in)
1135 noexcept(__nothrow_constructible<_U1, _U2>())
1136 : _Inherited(std::forward<_U1>(__in.first),
1137 std::forward<_U2>(__in.second)) { }
1139 // Allocator-extended constructors.
1141 template<typename _Alloc,
1142 _ImplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = true>
1143 _GLIBCXX20_CONSTEXPR
1144 tuple(allocator_arg_t __tag, const _Alloc& __a)
1145 : _Inherited(__tag, __a) { }
1147 template<typename _Alloc, bool _Dummy = true,
1148 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
1149 _GLIBCXX20_CONSTEXPR
1150 tuple(allocator_arg_t __tag, const _Alloc& __a,
1151 const _T1& __a1, const _T2& __a2)
1152 : _Inherited(__tag, __a, __a1, __a2) { }
1154 template<typename _Alloc, bool _Dummy = true,
1155 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
1157 _GLIBCXX20_CONSTEXPR
1158 tuple(allocator_arg_t __tag, const _Alloc& __a,
1159 const _T1& __a1, const _T2& __a2)
1160 : _Inherited(__tag, __a, __a1, __a2) { }
1162 template<typename _Alloc, typename _U1, typename _U2,
1163 _ImplicitCtor<true, _U1, _U2> = true>
1164 _GLIBCXX20_CONSTEXPR
1165 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
1166 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1167 std::forward<_U2>(__a2)) { }
1169 template<typename _Alloc, typename _U1, typename _U2,
1170 _ExplicitCtor<true, _U1, _U2> = false>
1172 _GLIBCXX20_CONSTEXPR
1173 tuple(allocator_arg_t __tag, const _Alloc& __a,
1174 _U1&& __a1, _U2&& __a2)
1175 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1176 std::forward<_U2>(__a2)) { }
1178 template<typename _Alloc>
1179 _GLIBCXX20_CONSTEXPR
1180 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
1181 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
1183 template<typename _Alloc>
1184 _GLIBCXX20_CONSTEXPR
1185 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
1186 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
1188 template<typename _Alloc, typename _U1, typename _U2,
1189 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1190 _GLIBCXX20_CONSTEXPR
1191 tuple(allocator_arg_t __tag, const _Alloc& __a,
1192 const tuple<_U1, _U2>& __in)
1193 : _Inherited(__tag, __a,
1194 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1197 template<typename _Alloc, typename _U1, typename _U2,
1198 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1200 _GLIBCXX20_CONSTEXPR
1201 tuple(allocator_arg_t __tag, const _Alloc& __a,
1202 const tuple<_U1, _U2>& __in)
1203 : _Inherited(__tag, __a,
1204 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1207 template<typename _Alloc, typename _U1, typename _U2,
1208 _ImplicitCtor<true, _U1, _U2> = true>
1209 _GLIBCXX20_CONSTEXPR
1210 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1211 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1214 template<typename _Alloc, typename _U1, typename _U2,
1215 _ExplicitCtor<true, _U1, _U2> = false>
1217 _GLIBCXX20_CONSTEXPR
1218 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1219 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1222 template<typename _Alloc, typename _U1, typename _U2,
1223 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1224 _GLIBCXX20_CONSTEXPR
1225 tuple(allocator_arg_t __tag, const _Alloc& __a,
1226 const pair<_U1, _U2>& __in)
1227 : _Inherited(__tag, __a, __in.first, __in.second) { }
1229 template<typename _Alloc, typename _U1, typename _U2,
1230 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1232 _GLIBCXX20_CONSTEXPR
1233 tuple(allocator_arg_t __tag, const _Alloc& __a,
1234 const pair<_U1, _U2>& __in)
1235 : _Inherited(__tag, __a, __in.first, __in.second) { }
1237 template<typename _Alloc, typename _U1, typename _U2,
1238 _ImplicitCtor<true, _U1, _U2> = true>
1239 _GLIBCXX20_CONSTEXPR
1240 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
1241 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1242 std::forward<_U2>(__in.second)) { }
1244 template<typename _Alloc, typename _U1, typename _U2,
1245 _ExplicitCtor<true, _U1, _U2> = false>
1247 _GLIBCXX20_CONSTEXPR
1248 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
1249 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1250 std::forward<_U2>(__in.second)) { }
1252 // Tuple assignment.
1254 _GLIBCXX20_CONSTEXPR
1256 operator=(typename conditional<__assignable<const _T1&, const _T2&>(),
1258 const __nonesuch&>::type __in)
1259 noexcept(__nothrow_assignable<const _T1&, const _T2&>())
1261 this->_M_assign(__in);
1265 _GLIBCXX20_CONSTEXPR
1267 operator=(typename conditional<__assignable<_T1, _T2>(),
1269 __nonesuch&&>::type __in)
1270 noexcept(__nothrow_assignable<_T1, _T2>())
1272 this->_M_assign(std::move(__in));
1276 template<typename _U1, typename _U2>
1277 _GLIBCXX20_CONSTEXPR
1278 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
1279 operator=(const tuple<_U1, _U2>& __in)
1280 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1282 this->_M_assign(__in);
1286 template<typename _U1, typename _U2>
1287 _GLIBCXX20_CONSTEXPR
1288 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
1289 operator=(tuple<_U1, _U2>&& __in)
1290 noexcept(__nothrow_assignable<_U1, _U2>())
1292 this->_M_assign(std::move(__in));
1296 template<typename _U1, typename _U2>
1297 _GLIBCXX20_CONSTEXPR
1298 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
1299 operator=(const pair<_U1, _U2>& __in)
1300 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1302 this->_M_head(*this) = __in.first;
1303 this->_M_tail(*this)._M_head(*this) = __in.second;
1307 template<typename _U1, typename _U2>
1308 _GLIBCXX20_CONSTEXPR
1309 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
1310 operator=(pair<_U1, _U2>&& __in)
1311 noexcept(__nothrow_assignable<_U1, _U2>())
1313 this->_M_head(*this) = std::forward<_U1>(__in.first);
1314 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
1318 _GLIBCXX20_CONSTEXPR
1321 noexcept(__and_<__is_nothrow_swappable<_T1>,
1322 __is_nothrow_swappable<_T2>>::value)
1323 { _Inherited::_M_swap(__in); }
1327 /// class tuple_size
1328 template<typename... _Elements>
1329 struct tuple_size<tuple<_Elements...>>
1330 : public integral_constant<size_t, sizeof...(_Elements)> { };
1332 #if __cplusplus > 201402L
1333 template <typename _Tp>
1334 inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
1338 * Recursive case for tuple_element: strip off the first element in
1339 * the tuple and retrieve the (i-1)th element of the remaining tuple.
1341 template<size_t __i, typename _Head, typename... _Tail>
1342 struct tuple_element<__i, tuple<_Head, _Tail...> >
1343 : tuple_element<__i - 1, tuple<_Tail...> > { };
1346 * Basis case for tuple_element: The first element is the one we're seeking.
1348 template<typename _Head, typename... _Tail>
1349 struct tuple_element<0, tuple<_Head, _Tail...> >
1355 * Error case for tuple_element: invalid index.
1357 template<size_t __i>
1358 struct tuple_element<__i, tuple<>>
1360 static_assert(__i < tuple_size<tuple<>>::value,
1361 "tuple index is in range");
1364 template<size_t __i, typename _Head, typename... _Tail>
1366 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1367 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1369 template<size_t __i, typename _Head, typename... _Tail>
1370 constexpr const _Head&
1371 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1372 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1374 /// Return a reference to the ith element of a tuple.
1375 template<size_t __i, typename... _Elements>
1376 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
1377 get(tuple<_Elements...>& __t) noexcept
1378 { return std::__get_helper<__i>(__t); }
1380 /// Return a const reference to the ith element of a const tuple.
1381 template<size_t __i, typename... _Elements>
1382 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
1383 get(const tuple<_Elements...>& __t) noexcept
1384 { return std::__get_helper<__i>(__t); }
1386 /// Return an rvalue reference to the ith element of a tuple rvalue.
1387 template<size_t __i, typename... _Elements>
1388 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
1389 get(tuple<_Elements...>&& __t) noexcept
1391 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1392 return std::forward<__element_type&&>(std::get<__i>(__t));
1395 /// Return a const rvalue reference to the ith element of a const tuple rvalue.
1396 template<size_t __i, typename... _Elements>
1397 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&&
1398 get(const tuple<_Elements...>&& __t) noexcept
1400 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1401 return std::forward<const __element_type&&>(std::get<__i>(__t));
1404 #if __cplusplus >= 201402L
1406 #define __cpp_lib_tuples_by_type 201304
1408 template<typename _Head, size_t __i, typename... _Tail>
1410 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1411 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1413 template<typename _Head, size_t __i, typename... _Tail>
1414 constexpr const _Head&
1415 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1416 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1418 /// Return a reference to the unique element of type _Tp of a tuple.
1419 template <typename _Tp, typename... _Types>
1421 get(tuple<_Types...>& __t) noexcept
1422 { return std::__get_helper2<_Tp>(__t); }
1424 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
1425 template <typename _Tp, typename... _Types>
1427 get(tuple<_Types...>&& __t) noexcept
1428 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
1430 /// Return a const reference to the unique element of type _Tp of a tuple.
1431 template <typename _Tp, typename... _Types>
1432 constexpr const _Tp&
1433 get(const tuple<_Types...>& __t) noexcept
1434 { return std::__get_helper2<_Tp>(__t); }
1436 /// Return a const reference to the unique element of type _Tp of
1437 /// a const tuple rvalue.
1438 template <typename _Tp, typename... _Types>
1439 constexpr const _Tp&&
1440 get(const tuple<_Types...>&& __t) noexcept
1441 { return std::forward<const _Tp&&>(std::__get_helper2<_Tp>(__t)); }
1444 // This class performs the comparison operations on tuples
1445 template<typename _Tp, typename _Up, size_t __i, size_t __size>
1446 struct __tuple_compare
1448 static constexpr bool
1449 __eq(const _Tp& __t, const _Up& __u)
1451 return bool(std::get<__i>(__t) == std::get<__i>(__u))
1452 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
1455 static constexpr bool
1456 __less(const _Tp& __t, const _Up& __u)
1458 return bool(std::get<__i>(__t) < std::get<__i>(__u))
1459 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
1460 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
1464 template<typename _Tp, typename _Up, size_t __size>
1465 struct __tuple_compare<_Tp, _Up, __size, __size>
1467 static constexpr bool
1468 __eq(const _Tp&, const _Up&) { return true; }
1470 static constexpr bool
1471 __less(const _Tp&, const _Up&) { return false; }
1474 template<typename... _TElements, typename... _UElements>
1476 operator==(const tuple<_TElements...>& __t,
1477 const tuple<_UElements...>& __u)
1479 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1480 "tuple objects can only be compared if they have equal sizes.");
1481 using __compare = __tuple_compare<tuple<_TElements...>,
1482 tuple<_UElements...>,
1483 0, sizeof...(_TElements)>;
1484 return __compare::__eq(__t, __u);
1487 #if __cpp_lib_three_way_comparison
1488 template<typename _Cat, typename _Tp, typename _Up>
1490 __tuple_cmp(const _Tp&, const _Up&, index_sequence<>)
1491 { return _Cat::equivalent; }
1493 template<typename _Cat, typename _Tp, typename _Up,
1494 size_t _Idx0, size_t... _Idxs>
1496 __tuple_cmp(const _Tp& __t, const _Up& __u,
1497 index_sequence<_Idx0, _Idxs...>)
1500 = __detail::__synth3way(std::get<_Idx0>(__t), std::get<_Idx0>(__u));
1503 return std::__tuple_cmp<_Cat>(__t, __u, index_sequence<_Idxs...>());
1506 template<typename... _Tps, typename... _Ups>
1508 common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>
1509 operator<=>(const tuple<_Tps...>& __t, const tuple<_Ups...>& __u)
1512 = common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>;
1513 return std::__tuple_cmp<_Cat>(__t, __u, index_sequence_for<_Tps...>());
1516 template<typename... _TElements, typename... _UElements>
1518 operator<(const tuple<_TElements...>& __t,
1519 const tuple<_UElements...>& __u)
1521 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1522 "tuple objects can only be compared if they have equal sizes.");
1523 using __compare = __tuple_compare<tuple<_TElements...>,
1524 tuple<_UElements...>,
1525 0, sizeof...(_TElements)>;
1526 return __compare::__less(__t, __u);
1529 template<typename... _TElements, typename... _UElements>
1531 operator!=(const tuple<_TElements...>& __t,
1532 const tuple<_UElements...>& __u)
1533 { return !(__t == __u); }
1535 template<typename... _TElements, typename... _UElements>
1537 operator>(const tuple<_TElements...>& __t,
1538 const tuple<_UElements...>& __u)
1539 { return __u < __t; }
1541 template<typename... _TElements, typename... _UElements>
1543 operator<=(const tuple<_TElements...>& __t,
1544 const tuple<_UElements...>& __u)
1545 { return !(__u < __t); }
1547 template<typename... _TElements, typename... _UElements>
1549 operator>=(const tuple<_TElements...>& __t,
1550 const tuple<_UElements...>& __u)
1551 { return !(__t < __u); }
1552 #endif // three_way_comparison
1555 template<typename... _Elements>
1556 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
1557 make_tuple(_Elements&&... __args)
1559 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
1561 return __result_type(std::forward<_Elements>(__args)...);
1564 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1565 // 2275. Why is forward_as_tuple not constexpr?
1566 /// std::forward_as_tuple
1567 template<typename... _Elements>
1568 constexpr tuple<_Elements&&...>
1569 forward_as_tuple(_Elements&&... __args) noexcept
1570 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
1572 template<size_t, typename, typename, size_t>
1573 struct __make_tuple_impl;
1575 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
1576 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
1577 : __make_tuple_impl<_Idx + 1,
1578 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
1582 template<size_t _Nm, typename _Tuple, typename... _Tp>
1583 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
1585 typedef tuple<_Tp...> __type;
1588 template<typename _Tuple>
1589 struct __do_make_tuple
1590 : __make_tuple_impl<0, tuple<>, _Tuple, tuple_size<_Tuple>::value>
1593 // Returns the std::tuple equivalent of a tuple-like type.
1594 template<typename _Tuple>
1596 : public __do_make_tuple<__remove_cvref_t<_Tuple>>
1599 // Combines several std::tuple's into a single one.
1600 template<typename...>
1601 struct __combine_tuples;
1604 struct __combine_tuples<>
1606 typedef tuple<> __type;
1609 template<typename... _Ts>
1610 struct __combine_tuples<tuple<_Ts...>>
1612 typedef tuple<_Ts...> __type;
1615 template<typename... _T1s, typename... _T2s, typename... _Rem>
1616 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
1618 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
1619 _Rem...>::__type __type;
1622 // Computes the result type of tuple_cat given a set of tuple-like types.
1623 template<typename... _Tpls>
1624 struct __tuple_cat_result
1626 typedef typename __combine_tuples
1627 <typename __make_tuple<_Tpls>::__type...>::__type __type;
1630 // Helper to determine the index set for the first tuple-like
1631 // type of a given set.
1632 template<typename...>
1633 struct __make_1st_indices;
1636 struct __make_1st_indices<>
1638 typedef _Index_tuple<> __type;
1641 template<typename _Tp, typename... _Tpls>
1642 struct __make_1st_indices<_Tp, _Tpls...>
1644 typedef typename _Build_index_tuple<tuple_size<
1645 typename remove_reference<_Tp>::type>::value>::__type __type;
1648 // Performs the actual concatenation by step-wise expanding tuple-like
1649 // objects into the elements, which are finally forwarded into the
1651 template<typename _Ret, typename _Indices, typename... _Tpls>
1652 struct __tuple_concater;
1654 template<typename _Ret, size_t... _Is, typename _Tp, typename... _Tpls>
1655 struct __tuple_concater<_Ret, _Index_tuple<_Is...>, _Tp, _Tpls...>
1657 template<typename... _Us>
1658 static constexpr _Ret
1659 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1661 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1662 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
1663 return __next::_S_do(std::forward<_Tpls>(__tps)...,
1664 std::forward<_Us>(__us)...,
1665 std::get<_Is>(std::forward<_Tp>(__tp))...);
1669 template<typename _Ret>
1670 struct __tuple_concater<_Ret, _Index_tuple<>>
1672 template<typename... _Us>
1673 static constexpr _Ret
1674 _S_do(_Us&&... __us)
1676 return _Ret(std::forward<_Us>(__us)...);
1681 template<typename... _Tpls, typename = typename
1682 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1684 tuple_cat(_Tpls&&... __tpls)
1685 -> typename __tuple_cat_result<_Tpls...>::__type
1687 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1688 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1689 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1690 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1693 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1694 // 2301. Why is tie not constexpr?
1696 template<typename... _Elements>
1697 constexpr tuple<_Elements&...>
1698 tie(_Elements&... __args) noexcept
1699 { return tuple<_Elements&...>(__args...); }
1702 template<typename... _Elements>
1703 _GLIBCXX20_CONSTEXPR
1705 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1706 // Constrained free swap overload, see p0185r1
1707 typename enable_if<__and_<__is_swappable<_Elements>...>::value
1712 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1713 noexcept(noexcept(__x.swap(__y)))
1716 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1717 template<typename... _Elements>
1718 _GLIBCXX20_CONSTEXPR
1719 typename enable_if<!__and_<__is_swappable<_Elements>...>::value>::type
1720 swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
1723 // A class (and instance) which can be used in 'tie' when an element
1724 // of a tuple is not required.
1725 // _GLIBCXX14_CONSTEXPR
1726 // 2933. PR for LWG 2773 could be clearer
1727 struct _Swallow_assign
1730 _GLIBCXX14_CONSTEXPR const _Swallow_assign&
1731 operator=(const _Tp&) const
1735 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1736 // 2773. Making std::ignore constexpr
1737 _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
1739 /// Partial specialization for tuples
1740 template<typename... _Types, typename _Alloc>
1741 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1743 // See stl_pair.h...
1744 /** "piecewise construction" using a tuple of arguments for each member.
1746 * @param __first Arguments for the first member of the pair.
1747 * @param __second Arguments for the second member of the pair.
1749 * The elements of each tuple will be used as the constructor arguments
1750 * for the data members of the pair.
1752 template<class _T1, class _T2>
1753 template<typename... _Args1, typename... _Args2>
1754 _GLIBCXX20_CONSTEXPR
1757 pair(piecewise_construct_t,
1758 tuple<_Args1...> __first, tuple<_Args2...> __second)
1759 : pair(__first, __second,
1760 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1761 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1764 template<class _T1, class _T2>
1765 template<typename... _Args1, size_t... _Indexes1,
1766 typename... _Args2, size_t... _Indexes2>
1767 _GLIBCXX20_CONSTEXPR inline
1769 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1770 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1771 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1772 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1775 #if __cplusplus >= 201703L
1777 // Unpack a std::tuple into a type trait and use its value.
1778 // For cv std::tuple<_Up> the result is _Trait<_Tp, cv _Up...>::value.
1779 // For cv std::tuple<_Up>& the result is _Trait<_Tp, cv _Up&...>::value.
1780 // Otherwise the result is false (because we don't know if std::get throws).
1781 template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
1782 inline constexpr bool __unpack_std_tuple = false;
1784 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1785 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>>
1786 = _Trait<_Tp, _Up...>::value;
1788 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1789 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>&>
1790 = _Trait<_Tp, _Up&...>::value;
1792 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1793 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>>
1794 = _Trait<_Tp, const _Up...>::value;
1796 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1797 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>&>
1798 = _Trait<_Tp, const _Up&...>::value;
1800 # define __cpp_lib_apply 201603
1802 template <typename _Fn, typename _Tuple, size_t... _Idx>
1803 constexpr decltype(auto)
1804 __apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
1806 return std::__invoke(std::forward<_Fn>(__f),
1807 std::get<_Idx>(std::forward<_Tuple>(__t))...);
1810 template <typename _Fn, typename _Tuple>
1811 constexpr decltype(auto)
1812 apply(_Fn&& __f, _Tuple&& __t)
1813 noexcept(__unpack_std_tuple<is_nothrow_invocable, _Fn, _Tuple>)
1816 = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
1817 return std::__apply_impl(std::forward<_Fn>(__f),
1818 std::forward<_Tuple>(__t),
1822 #define __cpp_lib_make_from_tuple 201606
1824 template <typename _Tp, typename _Tuple, size_t... _Idx>
1826 __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
1827 { return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...); }
1829 template <typename _Tp, typename _Tuple>
1831 make_from_tuple(_Tuple&& __t)
1832 noexcept(__unpack_std_tuple<is_nothrow_constructible, _Tp, _Tuple>)
1834 return __make_from_tuple_impl<_Tp>(
1835 std::forward<_Tuple>(__t),
1836 make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>{});
1842 _GLIBCXX_END_NAMESPACE_VERSION
1847 #endif // _GLIBCXX_TUPLE