3// Copyright (C) 2007-2025 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.
30#define _GLIBCXX_TUPLE 1
33#pragma GCC system_header
36#if __cplusplus < 201103L
37# include <bits/c++0x_warning.h>
40#include <bits/stl_pair.h> // for std::pair
41#include <bits/uses_allocator.h> // for std::allocator_arg_t
42#include <bits/utility.h> // for std::tuple_size etc.
43#include <bits/invoke.h> // for std::__invoke
44#if __cplusplus > 201703L
46# include <bits/ranges_util.h> // for std::ranges::subrange
49#define __glibcxx_want_constexpr_tuple
50#define __glibcxx_want_tuple_element_t
51#define __glibcxx_want_tuples_by_type
52#define __glibcxx_want_apply
53#define __glibcxx_want_make_from_tuple
54#define __glibcxx_want_ranges_zip
55#define __glibcxx_want_tuple_like
56#define __glibcxx_want_constrained_equality
57#include <bits/version.h>
59namespace std _GLIBCXX_VISIBILITY(default)
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
64 * @addtogroup utilities
68 template<typename... _Elements>
71 /// @cond undocumented
72 template<typename _Tp>
73 struct __is_empty_non_tuple : is_empty<_Tp> { };
75 // Using EBO for elements that are tuples causes ambiguous base errors.
76 template<typename _El0, typename... _El>
77 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
79 // Use the Empty Base-class Optimization for empty, non-final types.
80 template<typename _Tp>
81 using __empty_not_final
82 = __conditional_t<__is_final(_Tp), false_type,
83 __is_empty_non_tuple<_Tp>>;
85 template<size_t _Idx, typename _Head,
86 bool = __empty_not_final<_Head>::value>
89#if __has_cpp_attribute(__no_unique_address__)
90 template<size_t _Idx, typename _Head>
91 struct _Head_base<_Idx, _Head, true>
93 constexpr _Head_base()
96 constexpr _Head_base(const _Head& __h)
97 : _M_head_impl(__h) { }
99 constexpr _Head_base(const _Head_base&) = default;
100 constexpr _Head_base(_Head_base&&) = default;
102 template<typename _UHead>
103 constexpr _Head_base(_UHead&& __h)
104 : _M_head_impl(std::forward<_UHead>(__h)) { }
107 _Head_base(allocator_arg_t, __uses_alloc0)
110 template<typename _Alloc>
112 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
113 : _M_head_impl(allocator_arg, *__a._M_a) { }
115 template<typename _Alloc>
117 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
118 : _M_head_impl(*__a._M_a) { }
120 template<typename _UHead>
122 _Head_base(__uses_alloc0, _UHead&& __uhead)
123 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
125 template<typename _Alloc, typename _UHead>
127 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
128 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
131 template<typename _Alloc, typename _UHead>
133 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
134 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
136 static constexpr _Head&
137 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
139 static constexpr const _Head&
140 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
142 [[__no_unique_address__]] _Head _M_head_impl;
145 template<size_t _Idx, typename _Head>
146 struct _Head_base<_Idx, _Head, true>
149 constexpr _Head_base()
152 constexpr _Head_base(const _Head& __h)
155 constexpr _Head_base(const _Head_base&) = default;
156 constexpr _Head_base(_Head_base&&) = default;
158 template<typename _UHead>
159 constexpr _Head_base(_UHead&& __h)
160 : _Head(std::forward<_UHead>(__h)) { }
163 _Head_base(allocator_arg_t, __uses_alloc0)
166 template<typename _Alloc>
168 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
169 : _Head(allocator_arg, *__a._M_a) { }
171 template<typename _Alloc>
173 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
174 : _Head(*__a._M_a) { }
176 template<typename _UHead>
178 _Head_base(__uses_alloc0, _UHead&& __uhead)
179 : _Head(std::forward<_UHead>(__uhead)) { }
181 template<typename _Alloc, typename _UHead>
183 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
184 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
186 template<typename _Alloc, typename _UHead>
188 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
189 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
191 static constexpr _Head&
192 _M_head(_Head_base& __b) noexcept { return __b; }
194 static constexpr const _Head&
195 _M_head(const _Head_base& __b) noexcept { return __b; }
199 template<size_t _Idx, typename _Head>
200 struct _Head_base<_Idx, _Head, false>
202 constexpr _Head_base()
205 constexpr _Head_base(const _Head& __h)
206 : _M_head_impl(__h) { }
208 constexpr _Head_base(const _Head_base&) = default;
209 constexpr _Head_base(_Head_base&&) = default;
211 template<typename _UHead>
212 constexpr _Head_base(_UHead&& __h)
213 : _M_head_impl(std::forward<_UHead>(__h)) { }
216 _Head_base(allocator_arg_t, __uses_alloc0)
219 template<typename _Alloc>
221 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
222 : _M_head_impl(allocator_arg, *__a._M_a) { }
224 template<typename _Alloc>
226 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
227 : _M_head_impl(*__a._M_a) { }
229 template<typename _UHead>
231 _Head_base(__uses_alloc0, _UHead&& __uhead)
232 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
234 template<typename _Alloc, typename _UHead>
236 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
237 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
240 template<typename _Alloc, typename _UHead>
242 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
243 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
245 static constexpr _Head&
246 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
248 static constexpr const _Head&
249 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
254#if __cpp_lib_tuple_like // >= C++23
255 struct __tuple_like_tag_t { explicit __tuple_like_tag_t() = default; };
257 // This forward declaration is used by the operator<=> overload for
259 template<typename _Cat, typename _Tp, typename _Up, typename _IndexSeq>
261 __tuple_cmp(const _Tp& __t, const _Up& __u, _IndexSeq);
265 * Contains the actual implementation of the @c tuple template, stored
266 * as a recursive inheritance hierarchy from the first element (most
267 * derived class) to the last (least derived class). The @c Idx
268 * parameter gives the 0-based index of the element stored at this
269 * point in the hierarchy; we use it to implement a constant-time
272 template<size_t _Idx, typename... _Elements>
276 * Recursive tuple implementation. Here we store the @c Head element
277 * and derive from a @c Tuple_impl containing the remaining elements
278 * (which contains the @c Tail).
280 template<size_t _Idx, typename _Head, typename... _Tail>
281 struct _Tuple_impl<_Idx, _Head, _Tail...>
282 : public _Tuple_impl<_Idx + 1, _Tail...>,
283 private _Head_base<_Idx, _Head>
285 template<size_t, typename...> friend struct _Tuple_impl;
287 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
288 typedef _Head_base<_Idx, _Head> _Base;
290 static constexpr _Head&
291 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
293 static constexpr const _Head&
294 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
296 static constexpr _Inherited&
297 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
299 static constexpr const _Inherited&
300 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
302 constexpr _Tuple_impl()
303 : _Inherited(), _Base() { }
306 _Tuple_impl(const _Head& __head, const _Tail&... __tail)
307 : _Inherited(__tail...), _Base(__head)
310 template<typename _UHead, typename... _UTail,
311 typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
313 _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
314 : _Inherited(std::forward<_UTail>(__tail)...),
315 _Base(std::forward<_UHead>(__head))
318 constexpr _Tuple_impl(const _Tuple_impl&) = default;
320 // _GLIBCXX_RESOLVE_LIB_DEFECTS
321 // 2729. Missing SFINAE on std::pair::operator=
322 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
324 _Tuple_impl(_Tuple_impl&&) = default;
326 template<typename... _UElements>
328 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
329 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
330 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in))
333 template<typename _UHead, typename... _UTails>
335 _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
336 : _Inherited(std::move
337 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
338 _Base(std::forward<_UHead>
339 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
342#if __cpp_lib_ranges_zip // >= C++23
343 template<typename... _UElements>
345 _Tuple_impl(_Tuple_impl<_Idx, _UElements...>& __in)
346 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
347 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in))
350 template<typename _UHead, typename... _UTails>
352 _Tuple_impl(const _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
353 : _Inherited(std::move
354 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
355 _Base(std::forward<const _UHead>
356 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
360#if __cpp_lib_tuple_like // >= C++23
361 template<typename _UTuple, size_t... _Is>
363 _Tuple_impl(__tuple_like_tag_t, _UTuple&& __u, index_sequence<_Is...>)
364 : _Tuple_impl(std::get<_Is>(std::forward<_UTuple>(__u))...)
368 template<typename _Alloc>
370 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
371 : _Inherited(__tag, __a),
372 _Base(__tag, __use_alloc<_Head>(__a))
375 template<typename _Alloc>
377 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
378 const _Head& __head, const _Tail&... __tail)
379 : _Inherited(__tag, __a, __tail...),
380 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head)
383 template<typename _Alloc, typename _UHead, typename... _UTail,
384 typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
386 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
387 _UHead&& __head, _UTail&&... __tail)
388 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
389 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
390 std::forward<_UHead>(__head))
393 template<typename _Alloc>
395 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
396 const _Tuple_impl& __in)
397 : _Inherited(__tag, __a, _M_tail(__in)),
398 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in))
401 template<typename _Alloc>
403 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
405 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
406 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
407 std::forward<_Head>(_M_head(__in)))
410 template<typename _Alloc, typename _UHead, typename... _UTails>
412 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
413 const _Tuple_impl<_Idx, _UHead, _UTails...>& __in)
414 : _Inherited(__tag, __a,
415 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)),
416 _Base(__use_alloc<_Head, _Alloc, const _UHead&>(__a),
417 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))
420 template<typename _Alloc, typename _UHead, typename... _UTails>
422 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
423 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
424 : _Inherited(__tag, __a, std::move
425 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
426 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
428 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
431#if __cpp_lib_ranges_zip // >= C++23
432 template<typename _Alloc, typename _UHead, typename... _UTails>
434 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
435 _Tuple_impl<_Idx, _UHead, _UTails...>& __in)
436 : _Inherited(__tag, __a,
437 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)),
438 _Base(__use_alloc<_Head, _Alloc, _UHead&>(__a),
439 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))
442 template<typename _Alloc, typename _UHead, typename... _UTails>
444 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
445 const _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
446 : _Inherited(__tag, __a, std::move
447 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
448 _Base(__use_alloc<_Head, _Alloc, const _UHead>(__a),
449 std::forward<const _UHead>
450 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
454#if __cpp_lib_tuple_like // >= C++23
455 template<typename _Alloc, typename _UTuple, size_t... _Is>
457 _Tuple_impl(__tuple_like_tag_t, allocator_arg_t __tag, const _Alloc& __a,
458 _UTuple&& __u, index_sequence<_Is...>)
459 : _Tuple_impl(__tag, __a, std::get<_Is>(std::forward<_UTuple>(__u))...)
463 template<typename... _UElements>
466 _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in)
468 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
469 _M_tail(*this)._M_assign(
470 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
473 template<typename _UHead, typename... _UTails>
476 _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
478 _M_head(*this) = std::forward<_UHead>
479 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
480 _M_tail(*this)._M_assign(
481 std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
484#if __cpp_lib_ranges_zip // >= C++23
485 template<typename... _UElements>
487 _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in) const
489 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
490 _M_tail(*this)._M_assign(
491 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
494 template<typename _UHead, typename... _UTails>
496 _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in) const
498 _M_head(*this) = std::forward<_UHead>
499 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
500 _M_tail(*this)._M_assign(
501 std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
505#if __cpp_lib_tuple_like // >= C++23
506 template<typename _UTuple>
508 _M_assign(__tuple_like_tag_t __tag, _UTuple&& __u)
510 _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u));
511 _M_tail(*this)._M_assign(__tag, std::forward<_UTuple>(__u));
514 template<typename _UTuple>
516 _M_assign(__tuple_like_tag_t __tag, _UTuple&& __u) const
518 _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u));
519 _M_tail(*this)._M_assign(__tag, std::forward<_UTuple>(__u));
526 _M_swap(_Tuple_impl& __in)
529 swap(_M_head(*this), _M_head(__in));
530 _Inherited::_M_swap(_M_tail(__in));
533#if __cpp_lib_ranges_zip // >= C++23
535 _M_swap(const _Tuple_impl& __in) const
538 swap(_M_head(*this), _M_head(__in));
539 _Inherited::_M_swap(_M_tail(__in));
544 // Basis case of inheritance recursion.
545 template<size_t _Idx, typename _Head>
546 struct _Tuple_impl<_Idx, _Head>
547 : private _Head_base<_Idx, _Head>
549 template<size_t, typename...> friend struct _Tuple_impl;
551 typedef _Head_base<_Idx, _Head> _Base;
553 static constexpr _Head&
554 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
556 static constexpr const _Head&
557 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
564 _Tuple_impl(const _Head& __head)
568 template<typename _UHead>
570 _Tuple_impl(_UHead&& __head)
571 : _Base(std::forward<_UHead>(__head))
574 constexpr _Tuple_impl(const _Tuple_impl&) = default;
576 // _GLIBCXX_RESOLVE_LIB_DEFECTS
577 // 2729. Missing SFINAE on std::pair::operator=
578 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
580#if _GLIBCXX_INLINE_VERSION
581 _Tuple_impl(_Tuple_impl&&) = default;
584 _Tuple_impl(_Tuple_impl&& __in)
585 noexcept(is_nothrow_move_constructible<_Head>::value)
586 : _Base(static_cast<_Base&&>(__in))
590 template<typename _UHead>
592 _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
593 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in))
596 template<typename _UHead>
598 _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
599 : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
602#if __cpp_lib_ranges_zip // >= C++23
603 template<typename _UHead>
605 _Tuple_impl(_Tuple_impl<_Idx, _UHead>& __in)
606 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in))
609 template<typename _UHead>
611 _Tuple_impl(const _Tuple_impl<_Idx, _UHead>&& __in)
612 : _Base(std::forward<const _UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
616#if __cpp_lib_tuple_like // >= C++23
617 template<typename _UTuple>
619 _Tuple_impl(__tuple_like_tag_t, _UTuple&& __u, index_sequence<0>)
620 : _Tuple_impl(std::get<0>(std::forward<_UTuple>(__u)))
624 template<typename _Alloc>
626 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
627 : _Base(__tag, __use_alloc<_Head>(__a))
630 template<typename _Alloc>
632 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
634 : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), __head)
637 template<typename _Alloc, typename _UHead>
639 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
641 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
642 std::forward<_UHead>(__head))
645 template<typename _Alloc>
647 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
648 const _Tuple_impl& __in)
649 : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), _M_head(__in))
652 template<typename _Alloc>
654 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
656 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
657 std::forward<_Head>(_M_head(__in)))
660 template<typename _Alloc, typename _UHead>
662 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
663 const _Tuple_impl<_Idx, _UHead>& __in)
664 : _Base(__use_alloc<_Head, _Alloc, const _UHead&>(__a),
665 _Tuple_impl<_Idx, _UHead>::_M_head(__in))
668 template<typename _Alloc, typename _UHead>
670 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
671 _Tuple_impl<_Idx, _UHead>&& __in)
672 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
673 std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
676#if __cpp_lib_ranges_zip // >= C++23
677 template<typename _Alloc, typename _UHead>
679 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
680 _Tuple_impl<_Idx, _UHead>& __in)
681 : _Base(__use_alloc<_Head, _Alloc, _UHead&>(__a),
682 _Tuple_impl<_Idx, _UHead>::_M_head(__in))
685 template<typename _Alloc, typename _UHead>
687 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
688 const _Tuple_impl<_Idx, _UHead>&& __in)
689 : _Base(__use_alloc<_Head, _Alloc, const _UHead>(__a),
690 std::forward<const _UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
694#if __cpp_lib_tuple_like // >= C++23
695 template<typename _Alloc, typename _UTuple>
697 _Tuple_impl(__tuple_like_tag_t, allocator_arg_t __tag, const _Alloc& __a,
698 _UTuple&& __u, index_sequence<0>)
699 : _Tuple_impl(__tag, __a, std::get<0>(std::forward<_UTuple>(__u)))
703 template<typename _UHead>
706 _M_assign(const _Tuple_impl<_Idx, _UHead>& __in)
708 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
711 template<typename _UHead>
714 _M_assign(_Tuple_impl<_Idx, _UHead>&& __in)
717 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
720#if __cpp_lib_ranges_zip // >= C++23
721 template<typename _UHead>
723 _M_assign(const _Tuple_impl<_Idx, _UHead>& __in) const
725 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
728 template<typename _UHead>
730 _M_assign(_Tuple_impl<_Idx, _UHead>&& __in) const
733 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
737#if __cpp_lib_tuple_like // >= C++23
738 template<typename _UTuple>
740 _M_assign(__tuple_like_tag_t, _UTuple&& __u)
741 { _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u)); }
743 template<typename _UTuple>
745 _M_assign(__tuple_like_tag_t, _UTuple&& __u) const
746 { _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u)); }
752 _M_swap(_Tuple_impl& __in)
755 swap(_M_head(*this), _M_head(__in));
758#if __cpp_lib_ranges_zip // >= C++23
760 _M_swap(const _Tuple_impl& __in) const
763 swap(_M_head(*this), _M_head(__in));
768 // Concept utility functions, reused in conditionally-explicit
770 template<bool, typename... _Types>
771 struct _TupleConstraints
773 template<typename... _UTypes>
774 using __constructible = __and_<is_constructible<_Types, _UTypes>...>;
776 template<typename... _UTypes>
777 using __convertible = __and_<is_convertible<_UTypes, _Types>...>;
779 // Constraint for a non-explicit constructor.
780 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
781 // and every Ui is implicitly convertible to Ti.
782 template<typename... _UTypes>
783 static constexpr bool __is_implicitly_constructible()
785 return __and_<__constructible<_UTypes...>,
786 __convertible<_UTypes...>
790 // Constraint for a non-explicit constructor.
791 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
792 // but not every Ui is implicitly convertible to Ti.
793 template<typename... _UTypes>
794 static constexpr bool __is_explicitly_constructible()
796 return __and_<__constructible<_UTypes...>,
797 __not_<__convertible<_UTypes...>>
801 static constexpr bool __is_implicitly_default_constructible()
803 return __and_<std::__is_implicitly_default_constructible<_Types>...
807 static constexpr bool __is_explicitly_default_constructible()
809 return __and_<is_default_constructible<_Types>...,
811 std::__is_implicitly_default_constructible<_Types>...>
816 // Partial specialization used when a required precondition isn't met,
817 // e.g. when sizeof...(_Types) != sizeof...(_UTypes).
818 template<typename... _Types>
819 struct _TupleConstraints<false, _Types...>
821 template<typename... _UTypes>
822 static constexpr bool __is_implicitly_constructible()
825 template<typename... _UTypes>
826 static constexpr bool __is_explicitly_constructible()
831 /// Primary class template, tuple
832 template<typename... _Elements>
833 class tuple : public _Tuple_impl<0, _Elements...>
835 using _Inherited = _Tuple_impl<0, _Elements...>;
837#if __cpp_concepts && __cpp_consteval && __cpp_conditional_explicit // >= C++20
838 template<typename... _UTypes>
839 static consteval bool
842 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
843 return __and_v<is_constructible<_Elements, _UTypes>...>;
848 template<typename... _UTypes>
849 static consteval bool
850 __nothrow_constructible()
852 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
853 return __and_v<is_nothrow_constructible<_Elements, _UTypes>...>;
858 template<typename... _UTypes>
859 static consteval bool
862 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
863 return __and_v<is_convertible<_UTypes, _Elements>...>;
868 // _GLIBCXX_RESOLVE_LIB_DEFECTS
869 // 3121. tuple constructor constraints for UTypes&&... overloads
870 template<typename... _UTypes>
871 static consteval bool
872 __disambiguating_constraint()
874 if constexpr (sizeof...(_Elements) != sizeof...(_UTypes))
876 else if constexpr (sizeof...(_Elements) == 1)
878 using _U0 = typename _Nth_type<0, _UTypes...>::type;
879 return !is_same_v<remove_cvref_t<_U0>, tuple>;
881 else if constexpr (sizeof...(_Elements) < 4)
883 using _U0 = typename _Nth_type<0, _UTypes...>::type;
884 if constexpr (!is_same_v<remove_cvref_t<_U0>, allocator_arg_t>)
888 using _T0 = typename _Nth_type<0, _Elements...>::type;
889 return is_same_v<remove_cvref_t<_T0>, allocator_arg_t>;
895 // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
896 // and the single element in Types can be initialized from TUPLE,
897 // or is the same type as tuple_element_t<0, TUPLE>.
898 template<typename _Tuple>
899 static consteval bool
902 if constexpr (sizeof...(_Elements) != 1)
904 else if constexpr (is_same_v<remove_cvref_t<_Tuple>, tuple>)
905 return true; // Should use a copy/move constructor instead.
908 using _Tp = typename _Nth_type<0, _Elements...>::type;
909 if constexpr (is_convertible_v<_Tuple, _Tp>)
911 else if constexpr (is_constructible_v<_Tp, _Tuple>)
917 template<typename... _Up>
918 static consteval bool
921#if __has_builtin(__reference_constructs_from_temporary)
922 return (__reference_constructs_from_temporary(_Elements, _Up&&)
929#if __cpp_lib_tuple_like // >= C++23
930 // _GLIBCXX_RESOLVE_LIB_DEFECTS
931 // 4045. tuple can create dangling references from tuple-like
932 template<typename _UTuple>
933 static consteval bool
934 __dangles_from_tuple_like()
936 return []<size_t... _Is>(index_sequence<_Is...>) {
937 return __dangles<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
938 }(index_sequence_for<_Elements...>{});
941 template<typename _UTuple>
942 static consteval bool
943 __constructible_from_tuple_like()
945 return []<size_t... _Is>(index_sequence<_Is...>) {
946 return __constructible<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
947 }(index_sequence_for<_Elements...>{});
950 template<typename _UTuple>
951 static consteval bool
952 __convertible_from_tuple_like()
954 return []<size_t... _Is>(index_sequence<_Is...>) {
955 return __convertible<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
956 }(index_sequence_for<_Elements...>{});
962 explicit(!(__is_implicitly_default_constructible_v<_Elements> && ...))
964 noexcept((is_nothrow_default_constructible_v<_Elements> && ...))
965 requires (is_default_constructible_v<_Elements> && ...)
969 // Defined as a template to work around PR libstdc++/116440.
970 template<typename = void>
971 constexpr explicit(!__convertible<const _Elements&...>())
972 tuple(const _Elements&... __elements)
973 noexcept(__nothrow_constructible<const _Elements&...>())
974 requires (__constructible<const _Elements&...>())
975 : _Inherited(__elements...)
978 template<typename... _UTypes>
979 requires (__disambiguating_constraint<_UTypes...>())
980 && (__constructible<_UTypes...>())
981 && (!__dangles<_UTypes...>())
982 constexpr explicit(!__convertible<_UTypes...>())
983 tuple(_UTypes&&... __u)
984 noexcept(__nothrow_constructible<_UTypes...>())
985 : _Inherited(std::forward<_UTypes>(__u)...)
988 template<typename... _UTypes>
989 requires (__disambiguating_constraint<_UTypes...>())
990 && (__constructible<_UTypes...>())
991 && (__dangles<_UTypes...>())
992 tuple(_UTypes&&...) = delete;
994 constexpr tuple(const tuple&) = default;
996 constexpr tuple(tuple&&) = default;
998 template<typename... _UTypes>
999 requires (__constructible<const _UTypes&...>())
1000 && (!__use_other_ctor<const tuple<_UTypes...>&>())
1001 && (!__dangles<const _UTypes&...>())
1002 constexpr explicit(!__convertible<const _UTypes&...>())
1003 tuple(const tuple<_UTypes...>& __u)
1004 noexcept(__nothrow_constructible<const _UTypes&...>())
1005 : _Inherited(static_cast<const _Tuple_impl<0, _UTypes...>&>(__u))
1008 template<typename... _UTypes>
1009 requires (__constructible<const _UTypes&...>())
1010 && (!__use_other_ctor<const tuple<_UTypes...>&>())
1011 && (__dangles<const _UTypes&...>())
1012 tuple(const tuple<_UTypes...>&) = delete;
1014 template<typename... _UTypes>
1015 requires (__constructible<_UTypes...>())
1016 && (!__use_other_ctor<tuple<_UTypes...>>())
1017 && (!__dangles<_UTypes...>())
1018 constexpr explicit(!__convertible<_UTypes...>())
1019 tuple(tuple<_UTypes...>&& __u)
1020 noexcept(__nothrow_constructible<_UTypes...>())
1021 : _Inherited(static_cast<_Tuple_impl<0, _UTypes...>&&>(__u))
1024 template<typename... _UTypes>
1025 requires (__constructible<_UTypes...>())
1026 && (!__use_other_ctor<tuple<_UTypes...>>())
1027 && (__dangles<_UTypes...>())
1028 tuple(tuple<_UTypes...>&&) = delete;
1030#if __cpp_lib_ranges_zip // >= C++23
1031 template<typename... _UTypes>
1032 requires (__constructible<_UTypes&...>())
1033 && (!__use_other_ctor<tuple<_UTypes...>&>())
1034 && (!__dangles<_UTypes&...>())
1035 constexpr explicit(!__convertible<_UTypes&...>())
1036 tuple(tuple<_UTypes...>& __u)
1037 noexcept(__nothrow_constructible<_UTypes&...>())
1038 : _Inherited(static_cast<_Tuple_impl<0, _UTypes...>&>(__u))
1041 template<typename... _UTypes>
1042 requires (__constructible<_UTypes&...>())
1043 && (!__use_other_ctor<tuple<_UTypes...>&>())
1044 && (__dangles<_UTypes&...>())
1045 tuple(tuple<_UTypes...>&) = delete;
1047 template<typename... _UTypes>
1048 requires (__constructible<const _UTypes...>())
1049 && (!__use_other_ctor<const tuple<_UTypes...>>())
1050 && (!__dangles<const _UTypes...>())
1051 constexpr explicit(!__convertible<const _UTypes...>())
1052 tuple(const tuple<_UTypes...>&& __u)
1053 noexcept(__nothrow_constructible<const _UTypes...>())
1054 : _Inherited(static_cast<const _Tuple_impl<0, _UTypes...>&&>(__u))
1057 template<typename... _UTypes>
1058 requires (__constructible<const _UTypes...>())
1059 && (!__use_other_ctor<const tuple<_UTypes...>>())
1060 && (__dangles<const _UTypes...>())
1061 tuple(const tuple<_UTypes...>&&) = delete;
1064 template<typename _U1, typename _U2>
1065 requires (sizeof...(_Elements) == 2)
1066 && (__constructible<const _U1&, const _U2&>())
1067 && (!__dangles<const _U1&, const _U2&>())
1068 constexpr explicit(!__convertible<const _U1&, const _U2&>())
1069 tuple(const pair<_U1, _U2>& __u)
1070 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1071 : _Inherited(__u.first, __u.second)
1074 template<typename _U1, typename _U2>
1075 requires (sizeof...(_Elements) == 2)
1076 && (__constructible<const _U1&, const _U2&>())
1077 && (__dangles<const _U1&, const _U2&>())
1078 tuple(const pair<_U1, _U2>&) = delete;
1080 template<typename _U1, typename _U2>
1081 requires (sizeof...(_Elements) == 2)
1082 && (__constructible<_U1, _U2>())
1083 && (!__dangles<_U1, _U2>())
1084 constexpr explicit(!__convertible<_U1, _U2>())
1085 tuple(pair<_U1, _U2>&& __u)
1086 noexcept(__nothrow_constructible<_U1, _U2>())
1087 : _Inherited(std::forward<_U1>(__u.first),
1088 std::forward<_U2>(__u.second))
1091 template<typename _U1, typename _U2>
1092 requires (sizeof...(_Elements) == 2)
1093 && (__constructible<_U1, _U2>())
1094 && (__dangles<_U1, _U2>())
1095 tuple(pair<_U1, _U2>&&) = delete;
1097#if __cpp_lib_ranges_zip // >= C++23
1098 template<typename _U1, typename _U2>
1099 requires (sizeof...(_Elements) == 2)
1100 && (__constructible<_U1&, _U2&>())
1101 && (!__dangles<_U1&, _U2&>())
1102 constexpr explicit(!__convertible<_U1&, _U2&>())
1103 tuple(pair<_U1, _U2>& __u)
1104 noexcept(__nothrow_constructible<_U1&, _U2&>())
1105 : _Inherited(__u.first, __u.second)
1108 template<typename _U1, typename _U2>
1109 requires (sizeof...(_Elements) == 2)
1110 && (__constructible<_U1&, _U2&>())
1111 && (__dangles<_U1&, _U2&>())
1112 tuple(pair<_U1, _U2>&) = delete;
1114 template<typename _U1, typename _U2>
1115 requires (sizeof...(_Elements) == 2)
1116 && (__constructible<const _U1, const _U2>())
1117 && (!__dangles<const _U1, const _U2>())
1118 constexpr explicit(!__convertible<const _U1, const _U2>())
1119 tuple(const pair<_U1, _U2>&& __u)
1120 noexcept(__nothrow_constructible<const _U1, const _U2>())
1121 : _Inherited(std::forward<const _U1>(__u.first),
1122 std::forward<const _U2>(__u.second))
1125 template<typename _U1, typename _U2>
1126 requires (sizeof...(_Elements) == 2)
1127 && (__constructible<const _U1, const _U2>())
1128 && (__dangles<const _U1, const _U2>())
1129 tuple(const pair<_U1, _U2>&&) = delete;
1132#if __cpp_lib_tuple_like // >= C++23
1133 template<__eligible_tuple_like<tuple> _UTuple>
1134 requires (__constructible_from_tuple_like<_UTuple>())
1135 && (!__use_other_ctor<_UTuple>())
1136 && (!__dangles_from_tuple_like<_UTuple>())
1137 constexpr explicit(!__convertible_from_tuple_like<_UTuple>())
1138 tuple(_UTuple&& __u)
1139 : _Inherited(__tuple_like_tag_t{},
1140 std::forward<_UTuple>(__u),
1141 index_sequence_for<_Elements...>{})
1144 template<__eligible_tuple_like<tuple> _UTuple>
1145 requires (__constructible_from_tuple_like<_UTuple>())
1146 && (!__use_other_ctor<_UTuple>())
1147 && (__dangles_from_tuple_like<_UTuple>())
1148 tuple(_UTuple&&) = delete;
1151 // Allocator-extended constructors.
1153 template<typename _Alloc>
1155 explicit(!(__is_implicitly_default_constructible_v<_Elements> && ...))
1156 tuple(allocator_arg_t __tag, const _Alloc& __a)
1157 requires (is_default_constructible_v<_Elements> && ...)
1158 : _Inherited(__tag, __a)
1161 template<typename _Alloc>
1162 constexpr explicit(!__convertible<const _Elements&...>())
1163 tuple(allocator_arg_t __tag, const _Alloc& __a,
1164 const _Elements&... __elements)
1165 requires (__constructible<const _Elements&...>())
1166 : _Inherited(__tag, __a, __elements...)
1169 template<typename _Alloc, typename... _UTypes>
1170 requires (__disambiguating_constraint<_UTypes...>())
1171 && (__constructible<_UTypes...>())
1172 && (!__dangles<_UTypes...>())
1173 constexpr explicit(!__convertible<_UTypes...>())
1174 tuple(allocator_arg_t __tag, const _Alloc& __a, _UTypes&&... __u)
1175 : _Inherited(__tag, __a, std::forward<_UTypes>(__u)...)
1178 template<typename _Alloc, typename... _UTypes>
1179 requires (__disambiguating_constraint<_UTypes...>())
1180 && (__constructible<_UTypes...>())
1181 && (__dangles<_UTypes...>())
1182 tuple(allocator_arg_t, const _Alloc&, _UTypes&&...) = delete;
1184 template<typename _Alloc>
1186 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __u)
1187 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__u))
1190 template<typename _Alloc>
1191 requires (__constructible<_Elements...>())
1193 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __u)
1194 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__u))
1197 template<typename _Alloc, typename... _UTypes>
1198 requires (__constructible<const _UTypes&...>())
1199 && (!__use_other_ctor<const tuple<_UTypes...>&>())
1200 && (!__dangles<const _UTypes&...>())
1201 constexpr explicit(!__convertible<const _UTypes&...>())
1202 tuple(allocator_arg_t __tag, const _Alloc& __a,
1203 const tuple<_UTypes...>& __u)
1204 : _Inherited(__tag, __a,
1205 static_cast<const _Tuple_impl<0, _UTypes...>&>(__u))
1208 template<typename _Alloc, typename... _UTypes>
1209 requires (__constructible<const _UTypes&...>())
1210 && (!__use_other_ctor<const tuple<_UTypes...>&>())
1211 && (__dangles<const _UTypes&...>())
1212 tuple(allocator_arg_t, const _Alloc&, const tuple<_UTypes...>&) = delete;
1214 template<typename _Alloc, typename... _UTypes>
1215 requires (__constructible<_UTypes...>())
1216 && (!__use_other_ctor<tuple<_UTypes...>>())
1217 && (!__dangles<_UTypes...>())
1218 constexpr explicit(!__use_other_ctor<tuple<_UTypes...>>())
1219 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_UTypes...>&& __u)
1220 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _UTypes...>&&>(__u))
1223 template<typename _Alloc, typename... _UTypes>
1224 requires (__constructible<_UTypes...>())
1225 && (!__use_other_ctor<tuple<_UTypes...>>())
1226 && (__dangles<_UTypes...>())
1227 tuple(allocator_arg_t, const _Alloc&, tuple<_UTypes...>&&) = delete;
1229#if __cpp_lib_ranges_zip // >= C++23
1230 template<typename _Alloc, typename... _UTypes>
1231 requires (__constructible<_UTypes&...>())
1232 && (!__use_other_ctor<tuple<_UTypes...>&>())
1233 && (!__dangles<_UTypes&...>())
1234 constexpr explicit(!__convertible<_UTypes&...>())
1235 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_UTypes...>& __u)
1236 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _UTypes...>&>(__u))
1239 template<typename _Alloc, typename... _UTypes>
1240 requires (__constructible<_UTypes&...>())
1241 && (!__use_other_ctor<tuple<_UTypes...>&>())
1242 && (__dangles<_UTypes&...>())
1243 tuple(allocator_arg_t, const _Alloc&, tuple<_UTypes...>&) = delete;
1245 template<typename _Alloc, typename... _UTypes>
1246 requires (__constructible<const _UTypes...>())
1247 && (!__use_other_ctor<const tuple<_UTypes...>>())
1248 && (!__dangles<const _UTypes...>())
1249 constexpr explicit(!__convertible<const _UTypes...>())
1250 tuple(allocator_arg_t __tag, const _Alloc& __a,
1251 const tuple<_UTypes...>&& __u)
1252 : _Inherited(__tag, __a,
1253 static_cast<const _Tuple_impl<0, _UTypes...>&&>(__u))
1256 template<typename _Alloc, typename... _UTypes>
1257 requires (__constructible<const _UTypes...>())
1258 && (!__use_other_ctor<const tuple<_UTypes...>>())
1259 && (__dangles<const _UTypes...>())
1260 tuple(allocator_arg_t, const _Alloc&, const tuple<_UTypes...>&&) = delete;
1263 template<typename _Alloc, typename _U1, typename _U2>
1264 requires (sizeof...(_Elements) == 2)
1265 && (__constructible<const _U1&, const _U2&>())
1266 && (!__dangles<const _U1&, const _U2&>())
1267 constexpr explicit(!__convertible<const _U1&, const _U2&>())
1268 tuple(allocator_arg_t __tag, const _Alloc& __a,
1269 const pair<_U1, _U2>& __u)
1270 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1271 : _Inherited(__tag, __a, __u.first, __u.second)
1274 template<typename _Alloc, typename _U1, typename _U2>
1275 requires (sizeof...(_Elements) == 2)
1276 && (__constructible<const _U1&, const _U2&>())
1277 && (__dangles<const _U1&, const _U2&>())
1278 tuple(allocator_arg_t, const _Alloc&, const pair<_U1, _U2>&) = delete;
1280 template<typename _Alloc, typename _U1, typename _U2>
1281 requires (sizeof...(_Elements) == 2)
1282 && (__constructible<_U1, _U2>())
1283 && (!__dangles<_U1, _U2>())
1284 constexpr explicit(!__convertible<_U1, _U2>())
1285 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __u)
1286 noexcept(__nothrow_constructible<_U1, _U2>())
1287 : _Inherited(__tag, __a, std::move(__u.first), std::move(__u.second))
1290 template<typename _Alloc, typename _U1, typename _U2>
1291 requires (sizeof...(_Elements) == 2)
1292 && (__constructible<_U1, _U2>())
1293 && (__dangles<_U1, _U2>())
1294 tuple(allocator_arg_t, const _Alloc&, pair<_U1, _U2>&&) = delete;
1296#if __cpp_lib_ranges_zip // >= C++23
1297 template<typename _Alloc, typename _U1, typename _U2>
1298 requires (sizeof...(_Elements) == 2)
1299 && (__constructible<_U1&, _U2&>())
1300 && (!__dangles<_U1&, _U2&>())
1301 constexpr explicit(!__convertible<_U1&, _U2&>())
1302 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>& __u)
1303 noexcept(__nothrow_constructible<_U1&, _U2&>())
1304 : _Inherited(__tag, __a, __u.first, __u.second)
1307 template<typename _Alloc, typename _U1, typename _U2>
1308 requires (sizeof...(_Elements) == 2)
1309 && (__constructible<_U1&, _U2&>())
1310 && (__dangles<_U1&, _U2&>())
1311 tuple(allocator_arg_t, const _Alloc&, pair<_U1, _U2>&) = delete;
1313 template<typename _Alloc, typename _U1, typename _U2>
1314 requires (sizeof...(_Elements) == 2)
1315 && (__constructible<const _U1, const _U2>())
1316 && (!__dangles<const _U1, const _U2>())
1317 constexpr explicit(!__convertible<const _U1, const _U2>())
1318 tuple(allocator_arg_t __tag, const _Alloc& __a,
1319 const pair<_U1, _U2>&& __u)
1320 noexcept(__nothrow_constructible<const _U1, const _U2>())
1321 : _Inherited(__tag, __a, std::move(__u.first), std::move(__u.second))
1324 template<typename _Alloc, typename _U1, typename _U2>
1325 requires (sizeof...(_Elements) == 2)
1326 && (__constructible<const _U1, const _U2>())
1327 && (__dangles<const _U1, const _U2>())
1328 tuple(allocator_arg_t, const _Alloc&, const pair<_U1, _U2>&&) = delete;
1331#if __cpp_lib_tuple_like // >= C++23
1332 template<typename _Alloc, __eligible_tuple_like<tuple> _UTuple>
1333 requires (__constructible_from_tuple_like<_UTuple>())
1334 && (!__use_other_ctor<_UTuple>())
1335 && (!__dangles_from_tuple_like<_UTuple>())
1336 constexpr explicit(!__convertible_from_tuple_like<_UTuple>())
1337 tuple(allocator_arg_t __tag, const _Alloc& __a, _UTuple&& __u)
1338 : _Inherited(__tuple_like_tag_t{},
1339 __tag, __a, std::forward<_UTuple>(__u),
1340 index_sequence_for<_Elements...>{})
1343 template<typename _Alloc, __eligible_tuple_like<tuple> _UTuple>
1344 requires (__constructible_from_tuple_like<_UTuple>())
1345 && (!__use_other_ctor<_UTuple>())
1346 && (__dangles_from_tuple_like<_UTuple>())
1347 tuple(allocator_arg_t, const _Alloc&, _UTuple&&) = delete;
1350#else // !(concepts && conditional_explicit)
1352 template<bool _Cond>
1353 using _TCC = _TupleConstraints<_Cond, _Elements...>;
1355 // Constraint for non-explicit default constructor
1356 template<bool _Dummy>
1357 using _ImplicitDefaultCtor = __enable_if_t<
1358 _TCC<_Dummy>::__is_implicitly_default_constructible(),
1361 // Constraint for explicit default constructor
1362 template<bool _Dummy>
1363 using _ExplicitDefaultCtor = __enable_if_t<
1364 _TCC<_Dummy>::__is_explicitly_default_constructible(),
1367 // Constraint for non-explicit constructors
1368 template<bool _Cond, typename... _Args>
1369 using _ImplicitCtor = __enable_if_t<
1370 _TCC<_Cond>::template __is_implicitly_constructible<_Args...>(),
1373 // Constraint for non-explicit constructors
1374 template<bool _Cond, typename... _Args>
1375 using _ExplicitCtor = __enable_if_t<
1376 _TCC<_Cond>::template __is_explicitly_constructible<_Args...>(),
1379 // Condition for noexcept-specifier of a constructor.
1380 template<typename... _UElements>
1381 static constexpr bool __nothrow_constructible()
1384 __and_<is_nothrow_constructible<_Elements, _UElements>...>::value;
1387 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) == 1.
1388 template<typename _Up>
1389 static constexpr bool __valid_args()
1391 return sizeof...(_Elements) == 1
1392 && !is_same<tuple, __remove_cvref_t<_Up>>::value;
1395 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) > 1.
1396 template<typename, typename, typename... _Tail>
1397 static constexpr bool __valid_args()
1398 { return (sizeof...(_Tail) + 2) == sizeof...(_Elements); }
1400 /* Constraint for constructors with a tuple<UTypes...> parameter ensures
1401 * that the constructor is only viable when it would not interfere with
1402 * tuple(UTypes&&...) or tuple(const tuple&) or tuple(tuple&&).
1403 * Such constructors are only viable if:
1404 * either sizeof...(Types) != 1,
1405 * or (when Types... expands to T and UTypes... expands to U)
1406 * is_convertible_v<TUPLE, T>, is_constructible_v<T, TUPLE>,
1407 * and is_same_v<T, U> are all false.
1409 template<typename _Tuple, typename = tuple,
1410 typename = __remove_cvref_t<_Tuple>>
1411 struct _UseOtherCtor
1414 // If TUPLE is convertible to the single element in *this,
1415 // then TUPLE should match tuple(UTypes&&...) instead.
1416 template<typename _Tuple, typename _Tp, typename _Up>
1417 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Up>>
1418 : __or_<is_convertible<_Tuple, _Tp>, is_constructible<_Tp, _Tuple>>::type
1420 // If TUPLE and *this each have a single element of the same type,
1421 // then TUPLE should match a copy/move constructor instead.
1422 template<typename _Tuple, typename _Tp>
1423 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Tp>>
1427 // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
1428 // and the single element in Types can be initialized from TUPLE,
1429 // or is the same type as tuple_element_t<0, TUPLE>.
1430 template<typename _Tuple>
1431 static constexpr bool __use_other_ctor()
1432 { return _UseOtherCtor<_Tuple>::value; }
1434 /// @cond undocumented
1435#undef __glibcxx_no_dangling_refs
1436#if __has_builtin(__reference_constructs_from_temporary) \
1437 && defined _GLIBCXX_DEBUG
1438 // Error if construction from U... would create a dangling ref.
1439# if __cpp_fold_expressions
1440# define __glibcxx_dangling_refs(U) \
1441 (__reference_constructs_from_temporary(_Elements, U) || ...)
1443# define __glibcxx_dangling_refs(U) \
1444 __or_<__bool_constant<__reference_constructs_from_temporary(_Elements, U) \
1447# define __glibcxx_no_dangling_refs(U) \
1448 static_assert(!__glibcxx_dangling_refs(U), \
1449 "std::tuple constructor creates a dangling reference")
1451# define __glibcxx_no_dangling_refs(U)
1456 template<typename _Dummy = void,
1457 _ImplicitDefaultCtor<is_void<_Dummy>::value> = true>
1460 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
1463 template<typename _Dummy = void,
1464 _ExplicitDefaultCtor<is_void<_Dummy>::value> = false>
1467 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
1470 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
1471 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
1473 tuple(const _Elements&... __elements)
1474 noexcept(__nothrow_constructible<const _Elements&...>())
1475 : _Inherited(__elements...) { }
1477 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
1478 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
1480 tuple(const _Elements&... __elements)
1481 noexcept(__nothrow_constructible<const _Elements&...>())
1482 : _Inherited(__elements...) { }
1484 template<typename... _UElements,
1485 bool _Valid = __valid_args<_UElements...>(),
1486 _ImplicitCtor<_Valid, _UElements...> = true>
1488 tuple(_UElements&&... __elements)
1489 noexcept(__nothrow_constructible<_UElements...>())
1490 : _Inherited(std::forward<_UElements>(__elements)...)
1491 { __glibcxx_no_dangling_refs(_UElements&&); }
1493 template<typename... _UElements,
1494 bool _Valid = __valid_args<_UElements...>(),
1495 _ExplicitCtor<_Valid, _UElements...> = false>
1497 tuple(_UElements&&... __elements)
1498 noexcept(__nothrow_constructible<_UElements...>())
1499 : _Inherited(std::forward<_UElements>(__elements)...)
1500 { __glibcxx_no_dangling_refs(_UElements&&); }
1502 constexpr tuple(const tuple&) = default;
1504 constexpr tuple(tuple&&) = default;
1506 template<typename... _UElements,
1507 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1508 && !__use_other_ctor<const tuple<_UElements...>&>(),
1509 _ImplicitCtor<_Valid, const _UElements&...> = true>
1511 tuple(const tuple<_UElements...>& __in)
1512 noexcept(__nothrow_constructible<const _UElements&...>())
1513 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1514 { __glibcxx_no_dangling_refs(const _UElements&); }
1516 template<typename... _UElements,
1517 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1518 && !__use_other_ctor<const tuple<_UElements...>&>(),
1519 _ExplicitCtor<_Valid, const _UElements&...> = false>
1521 tuple(const tuple<_UElements...>& __in)
1522 noexcept(__nothrow_constructible<const _UElements&...>())
1523 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1524 { __glibcxx_no_dangling_refs(const _UElements&); }
1526 template<typename... _UElements,
1527 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1528 && !__use_other_ctor<tuple<_UElements...>&&>(),
1529 _ImplicitCtor<_Valid, _UElements...> = true>
1531 tuple(tuple<_UElements...>&& __in)
1532 noexcept(__nothrow_constructible<_UElements...>())
1533 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1534 { __glibcxx_no_dangling_refs(_UElements&&); }
1536 template<typename... _UElements,
1537 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1538 && !__use_other_ctor<tuple<_UElements...>&&>(),
1539 _ExplicitCtor<_Valid, _UElements...> = false>
1541 tuple(tuple<_UElements...>&& __in)
1542 noexcept(__nothrow_constructible<_UElements...>())
1543 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1544 { __glibcxx_no_dangling_refs(_UElements&&); }
1546 // Allocator-extended constructors.
1548 template<typename _Alloc,
1549 _ImplicitDefaultCtor<is_object<_Alloc>::value> = true>
1550 _GLIBCXX20_CONSTEXPR
1551 tuple(allocator_arg_t __tag, const _Alloc& __a)
1552 : _Inherited(__tag, __a) { }
1554 template<typename _Alloc,
1555 _ExplicitDefaultCtor<is_object<_Alloc>::value> = false>
1556 _GLIBCXX20_CONSTEXPR
1558 tuple(allocator_arg_t __tag, const _Alloc& __a)
1559 : _Inherited(__tag, __a) { }
1561 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
1562 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
1563 _GLIBCXX20_CONSTEXPR
1564 tuple(allocator_arg_t __tag, const _Alloc& __a,
1565 const _Elements&... __elements)
1566 : _Inherited(__tag, __a, __elements...) { }
1568 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
1569 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
1570 _GLIBCXX20_CONSTEXPR
1572 tuple(allocator_arg_t __tag, const _Alloc& __a,
1573 const _Elements&... __elements)
1574 : _Inherited(__tag, __a, __elements...) { }
1576 template<typename _Alloc, typename... _UElements,
1577 bool _Valid = __valid_args<_UElements...>(),
1578 _ImplicitCtor<_Valid, _UElements...> = true>
1579 _GLIBCXX20_CONSTEXPR
1580 tuple(allocator_arg_t __tag, const _Alloc& __a,
1581 _UElements&&... __elements)
1582 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
1583 { __glibcxx_no_dangling_refs(_UElements&&); }
1585 template<typename _Alloc, typename... _UElements,
1586 bool _Valid = __valid_args<_UElements...>(),
1587 _ExplicitCtor<_Valid, _UElements...> = false>
1588 _GLIBCXX20_CONSTEXPR
1590 tuple(allocator_arg_t __tag, const _Alloc& __a,
1591 _UElements&&... __elements)
1592 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
1593 { __glibcxx_no_dangling_refs(_UElements&&); }
1595 template<typename _Alloc>
1596 _GLIBCXX20_CONSTEXPR
1597 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
1598 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
1600 template<typename _Alloc>
1601 _GLIBCXX20_CONSTEXPR
1602 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
1603 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
1605 template<typename _Alloc, typename... _UElements,
1606 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1607 && !__use_other_ctor<const tuple<_UElements...>&>(),
1608 _ImplicitCtor<_Valid, const _UElements&...> = true>
1609 _GLIBCXX20_CONSTEXPR
1610 tuple(allocator_arg_t __tag, const _Alloc& __a,
1611 const tuple<_UElements...>& __in)
1612 : _Inherited(__tag, __a,
1613 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1614 { __glibcxx_no_dangling_refs(const _UElements&); }
1616 template<typename _Alloc, typename... _UElements,
1617 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1618 && !__use_other_ctor<const tuple<_UElements...>&>(),
1619 _ExplicitCtor<_Valid, const _UElements&...> = false>
1620 _GLIBCXX20_CONSTEXPR
1622 tuple(allocator_arg_t __tag, const _Alloc& __a,
1623 const tuple<_UElements...>& __in)
1624 : _Inherited(__tag, __a,
1625 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1626 { __glibcxx_no_dangling_refs(const _UElements&); }
1628 template<typename _Alloc, typename... _UElements,
1629 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1630 && !__use_other_ctor<tuple<_UElements...>&&>(),
1631 _ImplicitCtor<_Valid, _UElements...> = true>
1632 _GLIBCXX20_CONSTEXPR
1633 tuple(allocator_arg_t __tag, const _Alloc& __a,
1634 tuple<_UElements...>&& __in)
1635 : _Inherited(__tag, __a,
1636 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1637 { __glibcxx_no_dangling_refs(_UElements&&); }
1639 template<typename _Alloc, typename... _UElements,
1640 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1641 && !__use_other_ctor<tuple<_UElements...>&&>(),
1642 _ExplicitCtor<_Valid, _UElements...> = false>
1643 _GLIBCXX20_CONSTEXPR
1645 tuple(allocator_arg_t __tag, const _Alloc& __a,
1646 tuple<_UElements...>&& __in)
1647 : _Inherited(__tag, __a,
1648 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1649 { __glibcxx_no_dangling_refs(_UElements&&); }
1650#endif // concepts && conditional_explicit
1654#if __cpp_concepts && __cpp_consteval // >= C++20
1656 template<typename... _UTypes>
1657 static consteval bool
1660 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
1661 return __and_v<is_assignable<_Elements&, _UTypes>...>;
1666 template<typename... _UTypes>
1667 static consteval bool
1668 __nothrow_assignable()
1670 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
1671 return __and_v<is_nothrow_assignable<_Elements&, _UTypes>...>;
1676#if __cpp_lib_ranges_zip // >= C++23
1677 template<typename... _UTypes>
1678 static consteval bool
1679 __const_assignable()
1681 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
1682 return __and_v<is_assignable<const _Elements&, _UTypes>...>;
1688#if __cpp_lib_tuple_like // >= C++23
1689 template<typename _UTuple>
1690 static consteval bool
1691 __assignable_from_tuple_like()
1693 return []<size_t... _Is>(index_sequence<_Is...>) {
1694 return __assignable<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
1695 }(index_sequence_for<_Elements...>{});
1698 template<typename _UTuple>
1699 static consteval bool
1700 __const_assignable_from_tuple_like()
1702 return []<size_t... _Is>(index_sequence<_Is...>) {
1703 return __const_assignable<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
1704 }(index_sequence_for<_Elements...>{});
1710 tuple& operator=(const tuple& __u) = delete;
1713 operator=(const tuple& __u)
1714 noexcept(__nothrow_assignable<const _Elements&...>())
1715 requires (__assignable<const _Elements&...>())
1717 this->_M_assign(__u);
1722 operator=(tuple&& __u)
1723 noexcept(__nothrow_assignable<_Elements...>())
1724 requires (__assignable<_Elements...>())
1726 this->_M_assign(std::move(__u));
1730 template<typename... _UTypes>
1731 requires (__assignable<const _UTypes&...>())
1733 operator=(const tuple<_UTypes...>& __u)
1734 noexcept(__nothrow_assignable<const _UTypes&...>())
1736 this->_M_assign(__u);
1740 template<typename... _UTypes>
1741 requires (__assignable<_UTypes...>())
1743 operator=(tuple<_UTypes...>&& __u)
1744 noexcept(__nothrow_assignable<_UTypes...>())
1746 this->_M_assign(std::move(__u));
1750#if __cpp_lib_ranges_zip // >= C++23
1751 constexpr const tuple&
1752 operator=(const tuple& __u) const
1753 requires (__const_assignable<const _Elements&...>())
1755 this->_M_assign(__u);
1759 constexpr const tuple&
1760 operator=(tuple&& __u) const
1761 requires (__const_assignable<_Elements...>())
1763 this->_M_assign(std::move(__u));
1767 template<typename... _UTypes>
1768 constexpr const tuple&
1769 operator=(const tuple<_UTypes...>& __u) const
1770 requires (__const_assignable<const _UTypes&...>())
1772 this->_M_assign(__u);
1776 template<typename... _UTypes>
1777 constexpr const tuple&
1778 operator=(tuple<_UTypes...>&& __u) const
1779 requires (__const_assignable<_UTypes...>())
1781 this->_M_assign(std::move(__u));
1786 template<typename _U1, typename _U2>
1787 requires (__assignable<const _U1&, const _U2&>())
1789 operator=(const pair<_U1, _U2>& __u)
1790 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1792 this->_M_head(*this) = __u.first;
1793 this->_M_tail(*this)._M_head(*this) = __u.second;
1797 template<typename _U1, typename _U2>
1798 requires (__assignable<_U1, _U2>())
1800 operator=(pair<_U1, _U2>&& __u)
1801 noexcept(__nothrow_assignable<_U1, _U2>())
1803 this->_M_head(*this) = std::forward<_U1>(__u.first);
1804 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__u.second);
1808#if __cpp_lib_ranges_zip // >= C++23
1809 template<typename _U1, typename _U2>
1810 requires (__const_assignable<const _U1&, const _U2>())
1811 constexpr const tuple&
1812 operator=(const pair<_U1, _U2>& __u) const
1814 this->_M_head(*this) = __u.first;
1815 this->_M_tail(*this)._M_head(*this) = __u.second;
1819 template<typename _U1, typename _U2>
1820 requires (__const_assignable<_U1, _U2>())
1821 constexpr const tuple&
1822 operator=(pair<_U1, _U2>&& __u) const
1824 this->_M_head(*this) = std::forward<_U1>(__u.first);
1825 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__u.second);
1830#if __cpp_lib_tuple_like // >= C++23
1831 template<__eligible_tuple_like<tuple> _UTuple>
1832 requires (__assignable_from_tuple_like<_UTuple>())
1834 operator=(_UTuple&& __u)
1836 this->_M_assign(__tuple_like_tag_t{}, std::forward<_UTuple>(__u));
1840 template<__eligible_tuple_like<tuple> _UTuple>
1841 requires (__const_assignable_from_tuple_like<_UTuple>())
1842 constexpr const tuple&
1843 operator=(_UTuple&& __u) const
1845 this->_M_assign(__tuple_like_tag_t{}, std::forward<_UTuple>(__u));
1849 template<__tuple_like _UTuple>
1850 requires (!__is_tuple_v<_UTuple>)
1851 friend constexpr bool
1852 operator== [[nodiscard]] (const tuple& __t, const _UTuple& __u)
1854 static_assert(sizeof...(_Elements) == tuple_size_v<_UTuple>,
1855 "tuple objects can only be compared if they have equal sizes.");
1856 return [&]<size_t... _Is>(index_sequence<_Is...>) {
1857 return (bool(std::get<_Is>(__t) == std::get<_Is>(__u))
1859 }(index_sequence_for<_Elements...>{});
1862 template<__tuple_like _UTuple,
1863 typename = make_index_sequence<tuple_size_v<_UTuple>>>
1864 struct __tuple_like_common_comparison_category;
1866 template<__tuple_like _UTuple, size_t... _Is>
1868 { typename void_t<__detail::__synth3way_t<_Elements, tuple_element_t<_Is, _UTuple>>...>; }
1869 struct __tuple_like_common_comparison_category<_UTuple, index_sequence<_Is...>>
1871 using type = common_comparison_category_t
1872 <__detail::__synth3way_t<_Elements, tuple_element_t<_Is, _UTuple>>...>;
1875 template<__tuple_like _UTuple>
1876 requires (!__is_tuple_v<_UTuple>)
1877 friend constexpr typename __tuple_like_common_comparison_category<_UTuple>::type
1878 operator<=>(const tuple& __t, const _UTuple& __u)
1880 using _Cat = typename __tuple_like_common_comparison_category<_UTuple>::type;
1881 return std::__tuple_cmp<_Cat>(__t, __u, index_sequence_for<_Elements...>());
1885#else // ! (concepts && consteval)
1888 template<typename... _UElements>
1890 __enable_if_t<sizeof...(_UElements) == sizeof...(_Elements), bool>
1892 { return __and_<is_assignable<_Elements&, _UElements>...>::value; }
1894 // Condition for noexcept-specifier of an assignment operator.
1895 template<typename... _UElements>
1896 static constexpr bool __nothrow_assignable()
1899 __and_<is_nothrow_assignable<_Elements&, _UElements>...>::value;
1904 _GLIBCXX20_CONSTEXPR
1906 operator=(__conditional_t<__assignable<const _Elements&...>(),
1908 const __nonesuch&> __in)
1909 noexcept(__nothrow_assignable<const _Elements&...>())
1911 this->_M_assign(__in);
1915 _GLIBCXX20_CONSTEXPR
1917 operator=(__conditional_t<__assignable<_Elements...>(),
1920 noexcept(__nothrow_assignable<_Elements...>())
1922 this->_M_assign(std::move(__in));
1926 template<typename... _UElements>
1927 _GLIBCXX20_CONSTEXPR
1928 __enable_if_t<__assignable<const _UElements&...>(), tuple&>
1929 operator=(const tuple<_UElements...>& __in)
1930 noexcept(__nothrow_assignable<const _UElements&...>())
1932 this->_M_assign(__in);
1936 template<typename... _UElements>
1937 _GLIBCXX20_CONSTEXPR
1938 __enable_if_t<__assignable<_UElements...>(), tuple&>
1939 operator=(tuple<_UElements...>&& __in)
1940 noexcept(__nothrow_assignable<_UElements...>())
1942 this->_M_assign(std::move(__in));
1945#endif // concepts && consteval
1948 _GLIBCXX20_CONSTEXPR
1951 noexcept(__and_<__is_nothrow_swappable<_Elements>...>::value)
1952 { _Inherited::_M_swap(__in); }
1954#if __cpp_lib_ranges_zip // >= C++23
1955 // As an extension, we constrain the const swap member function in order
1956 // to continue accepting explicit instantiation of tuples whose elements
1957 // are not all const swappable. Without this constraint, such an
1958 // explicit instantiation would also instantiate the ill-formed body of
1959 // this function and yield a hard error. This constraint shouldn't
1960 // affect the behavior of valid programs.
1962 swap(const tuple& __in) const
1963 noexcept(__and_v<__is_nothrow_swappable<const _Elements>...>)
1964 requires (is_swappable_v<const _Elements> && ...)
1965 { _Inherited::_M_swap(__in); }
1969#if __cpp_deduction_guides >= 201606
1970 template<typename... _UTypes>
1971 tuple(_UTypes...) -> tuple<_UTypes...>;
1972 template<typename _T1, typename _T2>
1973 tuple(pair<_T1, _T2>) -> tuple<_T1, _T2>;
1974 template<typename _Alloc, typename... _UTypes>
1975 tuple(allocator_arg_t, _Alloc, _UTypes...) -> tuple<_UTypes...>;
1976 template<typename _Alloc, typename _T1, typename _T2>
1977 tuple(allocator_arg_t, _Alloc, pair<_T1, _T2>) -> tuple<_T1, _T2>;
1978 template<typename _Alloc, typename... _UTypes>
1979 tuple(allocator_arg_t, _Alloc, tuple<_UTypes...>) -> tuple<_UTypes...>;
1982 // Explicit specialization, zero-element tuple.
1987 _GLIBCXX20_CONSTEXPR
1988 void swap(tuple&) noexcept { /* no-op */ }
1989#if __cpp_lib_ranges_zip // >= C++23
1990 constexpr void swap(const tuple&) const noexcept { /* no-op */ }
1992 // We need the default since we're going to define no-op
1993 // allocator constructors.
1995 // No-op allocator constructors.
1996 template<typename _Alloc>
1997 _GLIBCXX20_CONSTEXPR
1998 tuple(allocator_arg_t, const _Alloc&) noexcept { }
1999 template<typename _Alloc>
2000 _GLIBCXX20_CONSTEXPR
2001 tuple(allocator_arg_t, const _Alloc&, const tuple&) noexcept { }
2003#if __cpp_lib_tuple_like // >= C++23
2004 // Comparison operators for tuple<> with other empty tuple-like types
2005 template<__tuple_like _UTuple>
2006 requires (!__is_tuple_v<_UTuple> && tuple_size_v<_UTuple> == 0)
2008 friend constexpr bool
2009 operator==(const tuple&, const _UTuple&) noexcept
2012 template<__tuple_like _UTuple>
2013 requires (!__is_tuple_v<_UTuple> && tuple_size_v<_UTuple> == 0)
2014 friend constexpr strong_ordering
2015 operator<=>(const tuple&, const _UTuple&) noexcept
2016 { return strong_ordering::equal; }
2020#if !(__cpp_concepts && __cpp_consteval && __cpp_conditional_explicit) // !C++20
2021 /// Partial specialization, 2-element tuple.
2022 /// Includes construction and assignment from a pair.
2023 template<typename _T1, typename _T2>
2024 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
2026 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
2028 // Constraint for non-explicit default constructor
2029 template<bool _Dummy, typename _U1, typename _U2>
2030 using _ImplicitDefaultCtor = __enable_if_t<
2031 _TupleConstraints<_Dummy, _U1, _U2>::
2032 __is_implicitly_default_constructible(),
2035 // Constraint for explicit default constructor
2036 template<bool _Dummy, typename _U1, typename _U2>
2037 using _ExplicitDefaultCtor = __enable_if_t<
2038 _TupleConstraints<_Dummy, _U1, _U2>::
2039 __is_explicitly_default_constructible(),
2042 template<bool _Dummy>
2043 using _TCC = _TupleConstraints<_Dummy, _T1, _T2>;
2045 // Constraint for non-explicit constructors
2046 template<bool _Cond, typename _U1, typename _U2>
2047 using _ImplicitCtor = __enable_if_t<
2048 _TCC<_Cond>::template __is_implicitly_constructible<_U1, _U2>(),
2051 // Constraint for non-explicit constructors
2052 template<bool _Cond, typename _U1, typename _U2>
2053 using _ExplicitCtor = __enable_if_t<
2054 _TCC<_Cond>::template __is_explicitly_constructible<_U1, _U2>(),
2057 template<typename _U1, typename _U2>
2058 static constexpr bool __assignable()
2060 return __and_<is_assignable<_T1&, _U1>,
2061 is_assignable<_T2&, _U2>>::value;
2064 template<typename _U1, typename _U2>
2065 static constexpr bool __nothrow_assignable()
2067 return __and_<is_nothrow_assignable<_T1&, _U1>,
2068 is_nothrow_assignable<_T2&, _U2>>::value;
2071 template<typename _U1, typename _U2>
2072 static constexpr bool __nothrow_constructible()
2074 return __and_<is_nothrow_constructible<_T1, _U1>,
2075 is_nothrow_constructible<_T2, _U2>>::value;
2078 static constexpr bool __nothrow_default_constructible()
2080 return __and_<is_nothrow_default_constructible<_T1>,
2081 is_nothrow_default_constructible<_T2>>::value;
2084 template<typename _U1>
2085 static constexpr bool __is_alloc_arg()
2086 { return is_same<__remove_cvref_t<_U1>, allocator_arg_t>::value; }
2088 /// @cond undocumented
2089#undef __glibcxx_no_dangling_refs
2090 // Error if construction from _U1 and _U2 would create a dangling ref.
2091#if __has_builtin(__reference_constructs_from_temporary) \
2092 && defined _GLIBCXX_DEBUG
2093# define __glibcxx_no_dangling_refs(_U1, _U2) \
2094 static_assert(!__reference_constructs_from_temporary(_T1, _U1) \
2095 && !__reference_constructs_from_temporary(_T2, _U2), \
2096 "std::tuple constructor creates a dangling reference")
2098# define __glibcxx_no_dangling_refs(_U1, _U2)
2103 template<bool _Dummy = true,
2104 _ImplicitDefaultCtor<_Dummy, _T1, _T2> = true>
2107 noexcept(__nothrow_default_constructible())
2110 template<bool _Dummy = true,
2111 _ExplicitDefaultCtor<_Dummy, _T1, _T2> = false>
2114 noexcept(__nothrow_default_constructible())
2117 template<bool _Dummy = true,
2118 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
2120 tuple(const _T1& __a1, const _T2& __a2)
2121 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
2122 : _Inherited(__a1, __a2) { }
2124 template<bool _Dummy = true,
2125 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
2127 tuple(const _T1& __a1, const _T2& __a2)
2128 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
2129 : _Inherited(__a1, __a2) { }
2131 template<typename _U1, typename _U2,
2132 _ImplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = true>
2134 tuple(_U1&& __a1, _U2&& __a2)
2135 noexcept(__nothrow_constructible<_U1, _U2>())
2136 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2))
2137 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2139 template<typename _U1, typename _U2,
2140 _ExplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = false>
2142 tuple(_U1&& __a1, _U2&& __a2)
2143 noexcept(__nothrow_constructible<_U1, _U2>())
2144 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2))
2145 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2147 constexpr tuple(const tuple&) = default;
2149 constexpr tuple(tuple&&) = default;
2151 template<typename _U1, typename _U2,
2152 _ImplicitCtor<true, const _U1&, const _U2&> = true>
2154 tuple(const tuple<_U1, _U2>& __in)
2155 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2156 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2157 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2159 template<typename _U1, typename _U2,
2160 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2162 tuple(const tuple<_U1, _U2>& __in)
2163 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2164 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2165 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2167 template<typename _U1, typename _U2,
2168 _ImplicitCtor<true, _U1, _U2> = true>
2170 tuple(tuple<_U1, _U2>&& __in)
2171 noexcept(__nothrow_constructible<_U1, _U2>())
2172 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2173 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2175 template<typename _U1, typename _U2,
2176 _ExplicitCtor<true, _U1, _U2> = false>
2178 tuple(tuple<_U1, _U2>&& __in)
2179 noexcept(__nothrow_constructible<_U1, _U2>())
2180 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2181 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2183 template<typename _U1, typename _U2,
2184 _ImplicitCtor<true, const _U1&, const _U2&> = true>
2186 tuple(const pair<_U1, _U2>& __in)
2187 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2188 : _Inherited(__in.first, __in.second)
2189 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2191 template<typename _U1, typename _U2,
2192 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2194 tuple(const pair<_U1, _U2>& __in)
2195 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2196 : _Inherited(__in.first, __in.second)
2197 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2199 template<typename _U1, typename _U2,
2200 _ImplicitCtor<true, _U1, _U2> = true>
2202 tuple(pair<_U1, _U2>&& __in)
2203 noexcept(__nothrow_constructible<_U1, _U2>())
2204 : _Inherited(std::forward<_U1>(__in.first),
2205 std::forward<_U2>(__in.second))
2206 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2208 template<typename _U1, typename _U2,
2209 _ExplicitCtor<true, _U1, _U2> = false>
2211 tuple(pair<_U1, _U2>&& __in)
2212 noexcept(__nothrow_constructible<_U1, _U2>())
2213 : _Inherited(std::forward<_U1>(__in.first),
2214 std::forward<_U2>(__in.second))
2215 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2217 // Allocator-extended constructors.
2219 template<typename _Alloc,
2220 _ImplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = true>
2221 _GLIBCXX20_CONSTEXPR
2222 tuple(allocator_arg_t __tag, const _Alloc& __a)
2223 : _Inherited(__tag, __a) { }
2225 template<typename _Alloc,
2226 _ExplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = false>
2227 _GLIBCXX20_CONSTEXPR
2229 tuple(allocator_arg_t __tag, const _Alloc& __a)
2230 : _Inherited(__tag, __a) { }
2232 template<typename _Alloc, bool _Dummy = true,
2233 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
2234 _GLIBCXX20_CONSTEXPR
2235 tuple(allocator_arg_t __tag, const _Alloc& __a,
2236 const _T1& __a1, const _T2& __a2)
2237 : _Inherited(__tag, __a, __a1, __a2) { }
2239 template<typename _Alloc, bool _Dummy = true,
2240 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
2242 _GLIBCXX20_CONSTEXPR
2243 tuple(allocator_arg_t __tag, const _Alloc& __a,
2244 const _T1& __a1, const _T2& __a2)
2245 : _Inherited(__tag, __a, __a1, __a2) { }
2247 template<typename _Alloc, typename _U1, typename _U2,
2248 _ImplicitCtor<true, _U1, _U2> = true>
2249 _GLIBCXX20_CONSTEXPR
2250 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
2251 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
2252 std::forward<_U2>(__a2))
2253 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2255 template<typename _Alloc, typename _U1, typename _U2,
2256 _ExplicitCtor<true, _U1, _U2> = false>
2258 _GLIBCXX20_CONSTEXPR
2259 tuple(allocator_arg_t __tag, const _Alloc& __a,
2260 _U1&& __a1, _U2&& __a2)
2261 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
2262 std::forward<_U2>(__a2))
2263 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2265 template<typename _Alloc>
2266 _GLIBCXX20_CONSTEXPR
2267 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
2268 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
2270 template<typename _Alloc>
2271 _GLIBCXX20_CONSTEXPR
2272 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
2273 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
2275 template<typename _Alloc, typename _U1, typename _U2,
2276 _ImplicitCtor<true, const _U1&, const _U2&> = true>
2277 _GLIBCXX20_CONSTEXPR
2278 tuple(allocator_arg_t __tag, const _Alloc& __a,
2279 const tuple<_U1, _U2>& __in)
2280 : _Inherited(__tag, __a,
2281 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2282 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2284 template<typename _Alloc, typename _U1, typename _U2,
2285 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2287 _GLIBCXX20_CONSTEXPR
2288 tuple(allocator_arg_t __tag, const _Alloc& __a,
2289 const tuple<_U1, _U2>& __in)
2290 : _Inherited(__tag, __a,
2291 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2292 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2294 template<typename _Alloc, typename _U1, typename _U2,
2295 _ImplicitCtor<true, _U1, _U2> = true>
2296 _GLIBCXX20_CONSTEXPR
2297 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
2298 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2299 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2301 template<typename _Alloc, typename _U1, typename _U2,
2302 _ExplicitCtor<true, _U1, _U2> = false>
2304 _GLIBCXX20_CONSTEXPR
2305 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
2306 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2307 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2309 template<typename _Alloc, typename _U1, typename _U2,
2310 _ImplicitCtor<true, const _U1&, const _U2&> = true>
2311 _GLIBCXX20_CONSTEXPR
2312 tuple(allocator_arg_t __tag, const _Alloc& __a,
2313 const pair<_U1, _U2>& __in)
2314 : _Inherited(__tag, __a, __in.first, __in.second)
2315 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2317 template<typename _Alloc, typename _U1, typename _U2,
2318 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2320 _GLIBCXX20_CONSTEXPR
2321 tuple(allocator_arg_t __tag, const _Alloc& __a,
2322 const pair<_U1, _U2>& __in)
2323 : _Inherited(__tag, __a, __in.first, __in.second)
2324 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2326 template<typename _Alloc, typename _U1, typename _U2,
2327 _ImplicitCtor<true, _U1, _U2> = true>
2328 _GLIBCXX20_CONSTEXPR
2329 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
2330 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
2331 std::forward<_U2>(__in.second))
2332 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2334 template<typename _Alloc, typename _U1, typename _U2,
2335 _ExplicitCtor<true, _U1, _U2> = false>
2337 _GLIBCXX20_CONSTEXPR
2338 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
2339 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
2340 std::forward<_U2>(__in.second))
2341 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2343 // Tuple assignment.
2345 _GLIBCXX20_CONSTEXPR
2347 operator=(__conditional_t<__assignable<const _T1&, const _T2&>(),
2349 const __nonesuch&> __in)
2350 noexcept(__nothrow_assignable<const _T1&, const _T2&>())
2352 this->_M_assign(__in);
2356 _GLIBCXX20_CONSTEXPR
2358 operator=(__conditional_t<__assignable<_T1, _T2>(),
2361 noexcept(__nothrow_assignable<_T1, _T2>())
2363 this->_M_assign(std::move(__in));
2367 template<typename _U1, typename _U2>
2368 _GLIBCXX20_CONSTEXPR
2369 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
2370 operator=(const tuple<_U1, _U2>& __in)
2371 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
2373 this->_M_assign(__in);
2377 template<typename _U1, typename _U2>
2378 _GLIBCXX20_CONSTEXPR
2379 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
2380 operator=(tuple<_U1, _U2>&& __in)
2381 noexcept(__nothrow_assignable<_U1, _U2>())
2383 this->_M_assign(std::move(__in));
2387 template<typename _U1, typename _U2>
2388 _GLIBCXX20_CONSTEXPR
2389 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
2390 operator=(const pair<_U1, _U2>& __in)
2391 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
2393 this->_M_head(*this) = __in.first;
2394 this->_M_tail(*this)._M_head(*this) = __in.second;
2398 template<typename _U1, typename _U2>
2399 _GLIBCXX20_CONSTEXPR
2400 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
2401 operator=(pair<_U1, _U2>&& __in)
2402 noexcept(__nothrow_assignable<_U1, _U2>())
2404 this->_M_head(*this) = std::forward<_U1>(__in.first);
2405 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
2409 _GLIBCXX20_CONSTEXPR
2412 noexcept(__and_<__is_nothrow_swappable<_T1>,
2413 __is_nothrow_swappable<_T2>>::value)
2414 { _Inherited::_M_swap(__in); }
2416#endif // concepts && conditional_explicit
2418 /// class tuple_size
2419 template<typename... _Elements>
2420 struct tuple_size<tuple<_Elements...>>
2421 : public integral_constant<size_t, sizeof...(_Elements)> { };
2423#if __cplusplus >= 201703L
2424 template<typename... _Types>
2425 inline constexpr size_t tuple_size_v<tuple<_Types...>>
2426 = sizeof...(_Types);
2428 template<typename... _Types>
2429 inline constexpr size_t tuple_size_v<const tuple<_Types...>>
2430 = sizeof...(_Types);
2433 /// Trait to get the Ith element type from a tuple.
2434 template<size_t __i, typename... _Types>
2435 struct tuple_element<__i, tuple<_Types...>>
2437 static_assert(__i < sizeof...(_Types), "tuple index must be in range");
2439 using type = typename _Nth_type<__i, _Types...>::type;
2442 template<size_t __i, typename _Head, typename... _Tail>
2444 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
2445 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
2447 template<size_t __i, typename _Head, typename... _Tail>
2448 constexpr const _Head&
2449 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
2450 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
2452 // Deleted overload to improve diagnostics for invalid indices
2453 template<size_t __i, typename... _Types>
2454 __enable_if_t<(__i >= sizeof...(_Types))>
2455 __get_helper(const tuple<_Types...>&) = delete;
2457 /// Return a reference to the ith element of a tuple.
2458 template<size_t __i, typename... _Elements>
2459 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
2460 get(tuple<_Elements...>& __t) noexcept
2461 { return std::__get_helper<__i>(__t); }
2463 /// Return a const reference to the ith element of a const tuple.
2464 template<size_t __i, typename... _Elements>
2465 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
2466 get(const tuple<_Elements...>& __t) noexcept
2467 { return std::__get_helper<__i>(__t); }
2469 /// Return an rvalue reference to the ith element of a tuple rvalue.
2470 template<size_t __i, typename... _Elements>
2471 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
2472 get(tuple<_Elements...>&& __t) noexcept
2474 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
2475 return std::forward<__element_type>(std::__get_helper<__i>(__t));
2478 /// Return a const rvalue reference to the ith element of a const tuple rvalue.
2479 template<size_t __i, typename... _Elements>
2480 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&&
2481 get(const tuple<_Elements...>&& __t) noexcept
2483 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
2484 return std::forward<const __element_type>(std::__get_helper<__i>(__t));
2487 /// @cond undocumented
2488 // Deleted overload chosen for invalid indices.
2489 template<size_t __i, typename... _Elements>
2490 constexpr __enable_if_t<(__i >= sizeof...(_Elements))>
2491 get(const tuple<_Elements...>&) = delete;
2494#ifdef __cpp_lib_tuples_by_type // C++ >= 14
2495 /// Return a reference to the unique element of type _Tp of a tuple.
2496 template <typename _Tp, typename... _Types>
2498 get(tuple<_Types...>& __t) noexcept
2500 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2501 static_assert(__idx < sizeof...(_Types),
2502 "the type T in std::get<T> must occur exactly once in the tuple");
2503 return std::__get_helper<__idx>(__t);
2506 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
2507 template <typename _Tp, typename... _Types>
2509 get(tuple<_Types...>&& __t) noexcept
2511 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2512 static_assert(__idx < sizeof...(_Types),
2513 "the type T in std::get<T> must occur exactly once in the tuple");
2514 return std::forward<_Tp>(std::__get_helper<__idx>(__t));
2517 /// Return a const reference to the unique element of type _Tp of a tuple.
2518 template <typename _Tp, typename... _Types>
2519 constexpr const _Tp&
2520 get(const tuple<_Types...>& __t) noexcept
2522 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2523 static_assert(__idx < sizeof...(_Types),
2524 "the type T in std::get<T> must occur exactly once in the tuple");
2525 return std::__get_helper<__idx>(__t);
2528 /// Return a const reference to the unique element of type _Tp of
2529 /// a const tuple rvalue.
2530 template <typename _Tp, typename... _Types>
2531 constexpr const _Tp&&
2532 get(const tuple<_Types...>&& __t) noexcept
2534 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2535 static_assert(__idx < sizeof...(_Types),
2536 "the type T in std::get<T> must occur exactly once in the tuple");
2537 return std::forward<const _Tp>(std::__get_helper<__idx>(__t));
2541#if __cpp_lib_three_way_comparison
2542 template<typename... _Tps, typename... _Ups>
2543 requires (sizeof...(_Tps) == sizeof...(_Ups))
2544 && (requires (const _Tps& __t, const _Ups& __u) {
2545 { __t == __u } -> __detail::__boolean_testable;
2548 operator== [[nodiscard]] (const tuple<_Tps...>& __t,
2549 const tuple<_Ups...>& __u)
2551 return [&]<size_t... _Inds>(index_sequence<_Inds...>) {
2552 // Fold == over the tuples until non-equal elements are found.
2553 return (bool(std::get<_Inds>(__t) == std::get<_Inds>(__u)) && ...);
2554 }(index_sequence_for<_Tps...>{});
2557 template<typename _Cat, typename _Tp, typename _Up, typename _IndexSeq>
2560 __tuple_cmp(const _Tp& __t, const _Up& __u, _IndexSeq __indices)
2562 _Cat __c = _Cat::equivalent;
2564 // Set __c to the comparison result of two corresponding elements.
2565 // Return true they are equivalent.
2566 auto __cmp = [&]<size_t _Ind>(integral_constant<size_t, _Ind>) {
2567 __c = __detail::__synth3way(std::get<_Ind>(__t), std::get<_Ind>(__u));
2571 [&]<size_t... _Inds>(index_sequence<_Inds...>) {
2572 // Fold __cmp over the tuples until non-equivalent elements are found.
2573 (void)(__cmp(integral_constant<size_t, _Inds>{}) && ...);
2579 template<typename... _Tps, typename... _Ups>
2580 requires (sizeof...(_Tps) == sizeof...(_Ups))
2581 && (requires { typename __detail::__synth3way_t<_Tps, _Ups>; } && ...)
2583 common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>
2584 operator<=> [[nodiscard]] (const tuple<_Tps...>& __t,
2585 const tuple<_Ups...>& __u)
2588 = common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>;
2589 return std::__tuple_cmp<_Cat>(__t, __u, index_sequence_for<_Tps...>());
2593 // This class performs the comparison operations on tuples
2594 template<typename _Tp, typename _Up, size_t __i, size_t __size>
2595 struct __tuple_compare
2597 static constexpr bool
2598 __eq(const _Tp& __t, const _Up& __u)
2600 return bool(std::get<__i>(__t) == std::get<__i>(__u))
2601 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
2604 static constexpr bool
2605 __less(const _Tp& __t, const _Up& __u)
2607 return bool(std::get<__i>(__t) < std::get<__i>(__u))
2608 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
2609 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
2613 template<typename _Tp, typename _Up, size_t __size>
2614 struct __tuple_compare<_Tp, _Up, __size, __size>
2616 static constexpr bool
2617 __eq(const _Tp&, const _Up&) { return true; }
2619 static constexpr bool
2620 __less(const _Tp&, const _Up&) { return false; }
2623 template<typename... _TElements, typename... _UElements>
2626 operator==(const tuple<_TElements...>& __t,
2627 const tuple<_UElements...>& __u)
2629 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
2630 "tuple objects can only be compared if they have equal sizes.");
2631 using __compare = __tuple_compare<tuple<_TElements...>,
2632 tuple<_UElements...>,
2633 0, sizeof...(_TElements)>;
2634 return __compare::__eq(__t, __u);
2637 template<typename... _TElements, typename... _UElements>
2640 operator<(const tuple<_TElements...>& __t,
2641 const tuple<_UElements...>& __u)
2643 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
2644 "tuple objects can only be compared if they have equal sizes.");
2645 using __compare = __tuple_compare<tuple<_TElements...>,
2646 tuple<_UElements...>,
2647 0, sizeof...(_TElements)>;
2648 return __compare::__less(__t, __u);
2651 template<typename... _TElements, typename... _UElements>
2654 operator!=(const tuple<_TElements...>& __t,
2655 const tuple<_UElements...>& __u)
2656 { return !(__t == __u); }
2658 template<typename... _TElements, typename... _UElements>
2661 operator>(const tuple<_TElements...>& __t,
2662 const tuple<_UElements...>& __u)
2663 { return __u < __t; }
2665 template<typename... _TElements, typename... _UElements>
2668 operator<=(const tuple<_TElements...>& __t,
2669 const tuple<_UElements...>& __u)
2670 { return !(__u < __t); }
2672 template<typename... _TElements, typename... _UElements>
2675 operator>=(const tuple<_TElements...>& __t,
2676 const tuple<_UElements...>& __u)
2677 { return !(__t < __u); }
2678#endif // three_way_comparison
2681 /// Create a tuple containing copies of the arguments
2682 template<typename... _Elements>
2683 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
2684 make_tuple(_Elements&&... __args)
2686 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
2688 return __result_type(std::forward<_Elements>(__args)...);
2691 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2692 // 2275. Why is forward_as_tuple not constexpr?
2693 /// Create a tuple of lvalue or rvalue references to the arguments
2694 template<typename... _Elements>
2695 constexpr tuple<_Elements&&...>
2696 forward_as_tuple(_Elements&&... __args) noexcept
2697 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
2699 /// @cond undocumented
2700 template<typename _Tuple, typename _Idx_tuple>
2701 struct __do_make_tuple;
2703 template<typename _Tuple, size_t... _Idx>
2704 struct __do_make_tuple<_Tuple, _Index_tuple<_Idx...>>
2706 using __type = tuple<__tuple_element_t<_Idx, _Tuple>...>;
2709 // Returns the std::tuple equivalent of a tuple-like type.
2710 template<typename _Tuple,
2711 typename _Tup = __remove_cvref_t<_Tuple>,
2712 typename _Indices = _Build_index_tuple<tuple_size<_Tup>::value>>
2714 : __do_make_tuple<_Tup, typename _Indices::__type>
2717 // Combines several std::tuple types into a single one.
2718 template<typename...>
2719 struct __combine_tuples;
2722 struct __combine_tuples<>
2724 using __type = tuple<>;
2727 template<typename... _Ts>
2728 struct __combine_tuples<tuple<_Ts...>>
2730 using __type = tuple<_Ts...>;
2733 template<typename... _T1s, typename... _T2s>
2734 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>>
2736 using __type = tuple<_T1s..., _T2s...>;
2739 template<typename... _T1s, typename... _T2s, typename... _T3s,
2741 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, tuple<_T3s...>,
2744 using _First = tuple<_T1s..., _T2s..., _T3s...>;
2745 using _Second = typename __combine_tuples<_Rem...>::__type;
2746 using __type = typename __combine_tuples<_First, _Second>::__type;
2749 // Computes the result type of tuple_cat given a set of tuple-like types.
2750 template<typename... _Tpls>
2751 struct __tuple_cat_result
2753 typedef typename __combine_tuples
2754 <typename __make_tuple<_Tpls>::__type...>::__type __type;
2757 // Helper to determine the index set for the first tuple-like
2758 // type of a given set.
2759 template<typename...>
2760 struct __make_1st_indices;
2763 struct __make_1st_indices<>
2765 typedef _Index_tuple<> __type;
2768 template<typename _Tp, typename... _Tpls>
2769 struct __make_1st_indices<_Tp, _Tpls...>
2771 typedef typename _Build_index_tuple<tuple_size<
2772 typename remove_reference<_Tp>::type>::value>::__type __type;
2775 // Performs the actual concatenation by step-wise expanding tuple-like
2776 // objects into the elements, which are finally forwarded into the
2778 template<typename _Ret, typename _Indices, typename... _Tpls>
2779 struct __tuple_concater;
2781 template<typename _Ret, size_t... _Is, typename _Tp, typename... _Tpls>
2782 struct __tuple_concater<_Ret, _Index_tuple<_Is...>, _Tp, _Tpls...>
2784 template<typename... _Us>
2785 static constexpr _Ret
2786 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
2788 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
2789 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
2790 return __next::_S_do(std::forward<_Tpls>(__tps)...,
2791 std::forward<_Us>(__us)...,
2792 std::get<_Is>(std::forward<_Tp>(__tp))...);
2796 template<typename _Ret>
2797 struct __tuple_concater<_Ret, _Index_tuple<>>
2799 template<typename... _Us>
2800 static constexpr _Ret
2801 _S_do(_Us&&... __us)
2803 return _Ret(std::forward<_Us>(__us)...);
2807 template<typename... _Tps>
2808 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
2812 /// Create a `tuple` containing all elements from multiple tuple-like objects
2813#if __cpp_lib_tuple_like // >= C++23
2814 template<__tuple_like... _Tpls>
2816 template<typename... _Tpls, typename = typename
2817 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
2820 tuple_cat(_Tpls&&... __tpls)
2821 -> typename __tuple_cat_result<_Tpls...>::__type
2823 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
2824 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
2825 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
2826 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
2829 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2830 // 2301. Why is tie not constexpr?
2831 /// Return a tuple of lvalue references bound to the arguments
2832 template<typename... _Elements>
2833 constexpr tuple<_Elements&...>
2834 tie(_Elements&... __args) noexcept
2835 { return tuple<_Elements&...>(__args...); }
2837 /// Exchange the values of two tuples
2838 template<typename... _Elements>
2839 _GLIBCXX20_CONSTEXPR
2841#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
2842 // Constrained free swap overload, see p0185r1
2843 typename enable_if<__and_<__is_swappable<_Elements>...>::value
2848 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
2849 noexcept(noexcept(__x.swap(__y)))
2852#if __cpp_lib_ranges_zip // >= C++23
2853 /// Exchange the values of two const tuples (if const elements can be swapped)
2854 template<typename... _Elements>
2855 requires (is_swappable_v<const _Elements> && ...)
2857 swap(const tuple<_Elements...>& __x, const tuple<_Elements...>& __y)
2858 noexcept(noexcept(__x.swap(__y)))
2862#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
2863 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2864 // 2766. Swapping non-swappable types
2865 template<typename... _Elements>
2866 _GLIBCXX20_CONSTEXPR
2867 typename enable_if<!__and_<__is_swappable<_Elements>...>::value>::type
2868 swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
2871 /// Partial specialization for tuples
2872 template<typename... _Types, typename _Alloc>
2873 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
2875 // See stl_pair.h...
2876 /** "piecewise construction" using a tuple of arguments for each member.
2878 * @param __first Arguments for the first member of the pair.
2879 * @param __second Arguments for the second member of the pair.
2881 * The elements of each tuple will be used as the constructor arguments
2882 * for the data members of the pair.
2884 template<class _T1, class _T2>
2885 template<typename... _Args1, typename... _Args2>
2886 _GLIBCXX20_CONSTEXPR
2889 pair(piecewise_construct_t,
2890 tuple<_Args1...> __first, tuple<_Args2...> __second)
2891 : pair(__first, __second,
2892 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
2893 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
2896 template<class _T1, class _T2>
2897 template<typename... _Args1, size_t... _Indexes1,
2898 typename... _Args2, size_t... _Indexes2>
2899 _GLIBCXX20_CONSTEXPR inline
2901 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
2902 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
2903 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
2904 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
2907#if defined(__cpp_lib_apply) || defined(__cpp_lib_make_from_tuple) // C++ >= 17
2908 // Unpack a std::tuple into a type trait and use its value.
2909 // For cv std::tuple<_Up> the result is _Trait<_Tp, cv _Up...>::value.
2910 // For cv std::tuple<_Up>& the result is _Trait<_Tp, cv _Up&...>::value.
2911 // Otherwise the result is false (because we don't know if std::get throws).
2912 template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
2913 inline constexpr bool __unpack_std_tuple = false;
2915 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
2916 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>>
2917 = _Trait<_Tp, _Up...>::value;
2919 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
2920 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>&>
2921 = _Trait<_Tp, _Up&...>::value;
2923 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
2924 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>>
2925 = _Trait<_Tp, const _Up...>::value;
2927 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
2928 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>&>
2929 = _Trait<_Tp, const _Up&...>::value;
2932#ifdef __cpp_lib_apply // C++ >= 17
2933 template <typename _Fn, typename _Tuple, size_t... _Idx>
2934 constexpr decltype(auto)
2935 __apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
2937 return std::__invoke(std::forward<_Fn>(__f),
2938 std::get<_Idx>(std::forward<_Tuple>(__t))...);
2941#if __cpp_lib_tuple_like // >= C++23
2942 template <typename _Fn, __tuple_like _Tuple>
2944 template <typename _Fn, typename _Tuple>
2946 constexpr decltype(auto)
2947 apply(_Fn&& __f, _Tuple&& __t)
2948 noexcept(__unpack_std_tuple<is_nothrow_invocable, _Fn, _Tuple>)
2951 = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
2952 return std::__apply_impl(std::forward<_Fn>(__f),
2953 std::forward<_Tuple>(__t),
2958#ifdef __cpp_lib_make_from_tuple // C++ >= 17
2959 template <typename _Tp, typename _Tuple, typename _Seq
2960 = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>>
2961 constexpr bool __can_make_from_tuple = false;
2963 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2964 // 3528. make_from_tuple can perform (the equivalent of) a C-style cast
2965 template <typename _Tp, typename _Tuple, size_t... _Idx>
2966 constexpr bool __can_make_from_tuple<_Tp, _Tuple, index_sequence<_Idx...>>
2967 = is_constructible_v<_Tp,
2968 decltype(std::get<_Idx>(std::declval<_Tuple>()))...>;
2970 template <typename _Tp, typename _Tuple, size_t... _Idx>
2972 __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
2974 static_assert(__can_make_from_tuple<_Tp, _Tuple, index_sequence<_Idx...>>);
2975 return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...);
2978#if __cpp_lib_tuple_like // >= C++23
2979 template <typename _Tp, __tuple_like _Tuple>
2981 template <typename _Tp, typename _Tuple>
2984 make_from_tuple(_Tuple&& __t)
2985 noexcept(__unpack_std_tuple<is_nothrow_constructible, _Tp, _Tuple>)
2986#ifdef __cpp_concepts // >= C++20
2988 requires __can_make_from_tuple<_Tp, _Tuple>
2990 -> __enable_if_t<__can_make_from_tuple<_Tp, _Tuple>, _Tp>
2993 constexpr size_t __n = tuple_size_v<remove_reference_t<_Tuple>>;
2994#if __has_builtin(__reference_constructs_from_temporary)
2995 if constexpr (__n == 1)
2997 using _Elt = decltype(std::get<0>(std::declval<_Tuple>()));
2998 static_assert(!__reference_constructs_from_temporary(_Tp, _Elt));
3001 return __make_from_tuple_impl<_Tp>(std::forward<_Tuple>(__t),
3002 make_index_sequence<__n>{});
3006#if __cpp_lib_tuple_like // >= C++23
3007 template<__tuple_like _TTuple, __tuple_like _UTuple,
3008 template<typename> class _TQual, template<typename> class _UQual,
3009 typename = make_index_sequence<tuple_size_v<_TTuple>>>
3010 struct __tuple_like_common_reference;
3012 template<__tuple_like _TTuple, __tuple_like _UTuple,
3013 template<typename> class _TQual, template<typename> class _UQual,
3016 { typename tuple<common_reference_t<_TQual<tuple_element_t<_Is, _TTuple>>,
3017 _UQual<tuple_element_t<_Is, _UTuple>>>...>; }
3018 struct __tuple_like_common_reference<_TTuple, _UTuple, _TQual, _UQual, index_sequence<_Is...>>
3020 using type = tuple<common_reference_t<_TQual<tuple_element_t<_Is, _TTuple>>,
3021 _UQual<tuple_element_t<_Is, _UTuple>>>...>;
3024 template<__tuple_like _TTuple, __tuple_like _UTuple,
3025 template<typename> class _TQual, template<typename> class _UQual>
3026 requires (__is_tuple_v<_TTuple> || __is_tuple_v<_UTuple>)
3027 && is_same_v<_TTuple, decay_t<_TTuple>>
3028 && is_same_v<_UTuple, decay_t<_UTuple>>
3029 && (tuple_size_v<_TTuple> == tuple_size_v<_UTuple>)
3030 && requires { typename __tuple_like_common_reference<_TTuple, _UTuple, _TQual, _UQual>::type; }
3031 struct basic_common_reference<_TTuple, _UTuple, _TQual, _UQual>
3033 using type = typename __tuple_like_common_reference<_TTuple, _UTuple, _TQual, _UQual>::type;
3036 template<__tuple_like _TTuple, __tuple_like _UTuple,
3037 typename = make_index_sequence<tuple_size_v<_TTuple>>>
3038 struct __tuple_like_common_type;
3040 template<__tuple_like _TTuple, __tuple_like _UTuple, size_t... _Is>
3042 { typename tuple<common_type_t<tuple_element_t<_Is, _TTuple>,
3043 tuple_element_t<_Is, _UTuple>>...>; }
3044 struct __tuple_like_common_type<_TTuple, _UTuple, index_sequence<_Is...>>
3046 using type = tuple<common_type_t<tuple_element_t<_Is, _TTuple>,
3047 tuple_element_t<_Is, _UTuple>>...>;
3050 template<__tuple_like _TTuple, __tuple_like _UTuple>
3051 requires (__is_tuple_v<_TTuple> || __is_tuple_v<_UTuple>)
3052 && is_same_v<_TTuple, decay_t<_TTuple>>
3053 && is_same_v<_UTuple, decay_t<_UTuple>>
3054 && (tuple_size_v<_TTuple> == tuple_size_v<_UTuple>)
3055 && requires { typename __tuple_like_common_type<_TTuple, _UTuple>::type; }
3056 struct common_type<_TTuple, _UTuple>
3058 using type = typename __tuple_like_common_type<_TTuple, _UTuple>::type;
3064#undef __glibcxx_no_dangling_refs
3066_GLIBCXX_END_NAMESPACE_VERSION
3071#endif // _GLIBCXX_TUPLE