libstdc++
tuple
Go to the documentation of this file.
1// <tuple> -*- C++ -*-
2
3// Copyright (C) 2007-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file include/tuple
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_TUPLE
30#define _GLIBCXX_TUPLE 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#if __cplusplus < 201103L
37# include <bits/c++0x_warning.h>
38#else
39
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
45# include <compare>
46# include <bits/ranges_util.h> // for std::ranges::subrange
47#endif
48
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#define __glibcxx_want_structural_types
58#include <bits/version.h>
59
60namespace std _GLIBCXX_VISIBILITY(default)
61{
62_GLIBCXX_BEGIN_NAMESPACE_VERSION
63
64 /**
65 * @addtogroup utilities
66 * @{
67 */
68
69 template<typename... _Elements>
70 class tuple;
71
72#if ! __has_cpp_attribute(__no_unique_address__)
73#error "support for [[__no_unique_address__]] attribute is required"
74#endif
75
76 /// @cond undocumented
77#if ! _GLIBCXX_INLINE_VERSION
78 template<typename _Tp>
79 struct __is_empty_non_tuple : is_empty<_Tp> { };
80
81 // Using EBO for elements that are tuples causes ambiguous base errors.
82 // Although we have replaced EBO with [[no_unique_address]] we have to
83 // preserve the original layout for ABI compatibility.
84 template<typename _El0, typename... _El>
85 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
86
87 // Use [[no_unique_address]] for empty, non-final types that are not tuples.
88 template<typename _Tp>
89 using __empty_not_final
90 = __conditional_t<__is_final(_Tp), false_type,
91 __is_empty_non_tuple<_Tp>>;
92
93 template<size_t _Idx, typename _Head,
94 bool = __empty_not_final<_Head>::value>
95 struct _Head_base;
96#else
97 // For the unstable ABI we always use [[no_unique_address]].
98 template<size_t _Idx, typename _Head, bool = true>
99 struct _Head_base;
100#endif
101
102 // Primary template uses [[no_unique_address]].
103 template<size_t _Idx, typename _Head, bool /* = true */>
104 struct _Head_base
105 {
106 constexpr _Head_base()
107 : _M_head_impl() { }
108
109 constexpr _Head_base(const _Head& __h)
110 : _M_head_impl(__h) { }
111
112 constexpr _Head_base(const _Head_base&) = default;
113 constexpr _Head_base(_Head_base&&) = default;
114
115 template<typename _UHead>
116 constexpr _Head_base(_UHead&& __h)
117 : _M_head_impl(std::forward<_UHead>(__h)) { }
118
119 _GLIBCXX20_CONSTEXPR
120 _Head_base(allocator_arg_t, __uses_alloc0)
121 : _M_head_impl() { }
122
123 template<typename _Alloc>
124 _GLIBCXX20_CONSTEXPR
125 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
126 : _M_head_impl(allocator_arg, *__a._M_a) { }
127
128 template<typename _Alloc>
129 _GLIBCXX20_CONSTEXPR
130 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
131 : _M_head_impl(*__a._M_a) { }
132
133 template<typename _UHead>
134 _GLIBCXX20_CONSTEXPR
135 _Head_base(__uses_alloc0, _UHead&& __uhead)
136 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
137
138 template<typename _Alloc, typename _UHead>
139 _GLIBCXX20_CONSTEXPR
140 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
141 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
142 { }
143
144 template<typename _Alloc, typename _UHead>
145 _GLIBCXX20_CONSTEXPR
146 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
147 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
148
149 static constexpr _Head&
150 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
151
152 static constexpr const _Head&
153 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
154
155 [[__no_unique_address__]] _Head _M_head_impl;
156 };
157
158#if ! _GLIBCXX_INLINE_VERSION
159 // Partial specialization that doesn't use [[no_unique_address]].
160 template<size_t _Idx, typename _Head>
161 struct _Head_base<_Idx, _Head, false>
162 {
163 constexpr _Head_base()
164 : _M_head_impl() { }
165
166 constexpr _Head_base(const _Head& __h)
167 : _M_head_impl(__h) { }
168
169 constexpr _Head_base(const _Head_base&) = default;
170 constexpr _Head_base(_Head_base&&) = default;
171
172 template<typename _UHead>
173 constexpr _Head_base(_UHead&& __h)
174 : _M_head_impl(std::forward<_UHead>(__h)) { }
175
176 _GLIBCXX20_CONSTEXPR
177 _Head_base(allocator_arg_t, __uses_alloc0)
178 : _M_head_impl() { }
179
180 template<typename _Alloc>
181 _GLIBCXX20_CONSTEXPR
182 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
183 : _M_head_impl(allocator_arg, *__a._M_a) { }
184
185 template<typename _Alloc>
186 _GLIBCXX20_CONSTEXPR
187 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
188 : _M_head_impl(*__a._M_a) { }
189
190 template<typename _UHead>
191 _GLIBCXX20_CONSTEXPR
192 _Head_base(__uses_alloc0, _UHead&& __uhead)
193 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
194
195 template<typename _Alloc, typename _UHead>
196 _GLIBCXX20_CONSTEXPR
197 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
198 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
199 { }
200
201 template<typename _Alloc, typename _UHead>
202 _GLIBCXX20_CONSTEXPR
203 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
204 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
205
206 static constexpr _Head&
207 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
208
209 static constexpr const _Head&
210 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
211
212 _Head _M_head_impl;
213 };
214#endif
215
216#if __cpp_lib_tuple_like // >= C++23
217 struct __tuple_like_tag_t { explicit __tuple_like_tag_t() = default; };
218
219 // This forward declaration is used by the operator<=> overload for
220 // tuple-like types.
221 template<typename _Cat, typename _Tp, typename _Up, typename _IndexSeq>
222 constexpr _Cat
223 __tuple_cmp(const _Tp& __t, const _Up& __u, _IndexSeq);
224#endif // C++23
225
226 /**
227 * Contains the actual implementation of the @c tuple template, stored
228 * as a recursive inheritance hierarchy from the first element (most
229 * derived class) to the last (least derived class). The @c Idx
230 * parameter gives the 0-based index of the element stored at this
231 * point in the hierarchy; we use it to implement a constant-time
232 * get() operation.
233 */
234 template<size_t _Idx, typename... _Elements>
235 struct _Tuple_impl;
236
237 /**
238 * Recursive tuple implementation. Here we store the @c Head element
239 * and derive from a @c Tuple_impl containing the remaining elements
240 * (which contains the @c Tail).
241 */
242 template<size_t _Idx, typename _Head, typename... _Tail>
243 struct _GLIBCXX_STRUCTURAL _Tuple_impl<_Idx, _Head, _Tail...>
244 : public _Tuple_impl<_Idx + 1, _Tail...>,
245 private _Head_base<_Idx, _Head>
246 {
247 template<size_t, typename...> friend struct _Tuple_impl;
248
249 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
250 typedef _Head_base<_Idx, _Head> _Base;
251
252 static constexpr _Head&
253 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
254
255 static constexpr const _Head&
256 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
257
258 static constexpr _Inherited&
259 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
260
261 static constexpr const _Inherited&
262 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
263
264 constexpr _Tuple_impl()
265 : _Inherited(), _Base() { }
266
267 explicit constexpr
268 _Tuple_impl(const _Head& __head, const _Tail&... __tail)
269 : _Inherited(__tail...), _Base(__head)
270 { }
271
272 template<typename _UHead, typename... _UTail,
273 typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
274 explicit constexpr
275 _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
276 : _Inherited(std::forward<_UTail>(__tail)...),
277 _Base(std::forward<_UHead>(__head))
278 { }
279
280 constexpr _Tuple_impl(const _Tuple_impl&) = default;
281
282 // _GLIBCXX_RESOLVE_LIB_DEFECTS
283 // 2729. Missing SFINAE on std::pair::operator=
284 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
285
286 _Tuple_impl(_Tuple_impl&&) = default;
287
288 template<typename... _UElements>
289 constexpr
290 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
291 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
292 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in))
293 { }
294
295 template<typename _UHead, typename... _UTails>
296 constexpr
297 _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
298 : _Inherited(std::move
299 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
300 _Base(std::forward<_UHead>
301 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
302 { }
303
304#if __cpp_lib_ranges_zip // >= C++23
305 template<typename... _UElements>
306 constexpr
307 _Tuple_impl(_Tuple_impl<_Idx, _UElements...>& __in)
308 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
309 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in))
310 { }
311
312 template<typename _UHead, typename... _UTails>
313 constexpr
314 _Tuple_impl(const _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
315 : _Inherited(std::move
316 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
317 _Base(std::forward<const _UHead>
318 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
319 { }
320#endif // C++23
321
322#if __cpp_lib_tuple_like // >= C++23
323 template<typename _UTuple, size_t... _Is>
324 constexpr
325 _Tuple_impl(__tuple_like_tag_t, _UTuple&& __u, index_sequence<_Is...>)
326 : _Tuple_impl(std::get<_Is>(std::forward<_UTuple>(__u))...)
327 { }
328#endif // C++23
329
330 template<typename _Alloc>
331 _GLIBCXX20_CONSTEXPR
332 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
333 : _Inherited(__tag, __a),
334 _Base(__tag, __use_alloc<_Head>(__a))
335 { }
336
337 template<typename _Alloc>
338 _GLIBCXX20_CONSTEXPR
339 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
340 const _Head& __head, const _Tail&... __tail)
341 : _Inherited(__tag, __a, __tail...),
342 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head)
343 { }
344
345 template<typename _Alloc, typename _UHead, typename... _UTail,
346 typename = __enable_if_t<sizeof...(_Tail) == sizeof...(_UTail)>>
347 _GLIBCXX20_CONSTEXPR
348 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
349 _UHead&& __head, _UTail&&... __tail)
350 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
351 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
352 std::forward<_UHead>(__head))
353 { }
354
355 template<typename _Alloc>
356 _GLIBCXX20_CONSTEXPR
357 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
358 const _Tuple_impl& __in)
359 : _Inherited(__tag, __a, _M_tail(__in)),
360 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in))
361 { }
362
363 template<typename _Alloc>
364 _GLIBCXX20_CONSTEXPR
365 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
366 _Tuple_impl&& __in)
367 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
368 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
369 std::forward<_Head>(_M_head(__in)))
370 { }
371
372 template<typename _Alloc, typename _UHead, typename... _UTails>
373 _GLIBCXX20_CONSTEXPR
374 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
375 const _Tuple_impl<_Idx, _UHead, _UTails...>& __in)
376 : _Inherited(__tag, __a,
377 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)),
378 _Base(__use_alloc<_Head, _Alloc, const _UHead&>(__a),
379 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))
380 { }
381
382 template<typename _Alloc, typename _UHead, typename... _UTails>
383 _GLIBCXX20_CONSTEXPR
384 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
385 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
386 : _Inherited(__tag, __a, std::move
387 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
388 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
389 std::forward<_UHead>
390 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
391 { }
392
393#if __cpp_lib_ranges_zip // >= C++23
394 template<typename _Alloc, typename _UHead, typename... _UTails>
395 constexpr
396 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
397 _Tuple_impl<_Idx, _UHead, _UTails...>& __in)
398 : _Inherited(__tag, __a,
399 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)),
400 _Base(__use_alloc<_Head, _Alloc, _UHead&>(__a),
401 _Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))
402 { }
403
404 template<typename _Alloc, typename _UHead, typename... _UTails>
405 constexpr
406 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
407 const _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
408 : _Inherited(__tag, __a, std::move
409 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
410 _Base(__use_alloc<_Head, _Alloc, const _UHead>(__a),
411 std::forward<const _UHead>
412 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in)))
413 { }
414#endif // C++23
415
416#if __cpp_lib_tuple_like // >= C++23
417 template<typename _Alloc, typename _UTuple, size_t... _Is>
418 constexpr
419 _Tuple_impl(__tuple_like_tag_t, allocator_arg_t __tag, const _Alloc& __a,
420 _UTuple&& __u, index_sequence<_Is...>)
421 : _Tuple_impl(__tag, __a, std::get<_Is>(std::forward<_UTuple>(__u))...)
422 { }
423#endif // C++23
424
425 template<typename... _UElements>
426 _GLIBCXX20_CONSTEXPR
427 void
428 _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in)
429 {
430 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
431 _M_tail(*this)._M_assign(
432 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
433 }
434
435 template<typename _UHead, typename... _UTails>
436 _GLIBCXX20_CONSTEXPR
437 void
438 _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
439 {
440 _M_head(*this) = std::forward<_UHead>
441 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
442 _M_tail(*this)._M_assign(
443 std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
444 }
445
446#if __cpp_lib_ranges_zip // >= C++23
447 template<typename... _UElements>
448 constexpr void
449 _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in) const
450 {
451 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
452 _M_tail(*this)._M_assign(
453 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
454 }
455
456 template<typename _UHead, typename... _UTails>
457 constexpr void
458 _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in) const
459 {
460 _M_head(*this) = std::forward<_UHead>
461 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
462 _M_tail(*this)._M_assign(
463 std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
464 }
465#endif // C++23
466
467#if __cpp_lib_tuple_like // >= C++23
468 template<typename _UTuple>
469 constexpr void
470 _M_assign(__tuple_like_tag_t __tag, _UTuple&& __u)
471 {
472 _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u));
473 _M_tail(*this)._M_assign(__tag, std::forward<_UTuple>(__u));
474 }
475
476 template<typename _UTuple>
477 constexpr void
478 _M_assign(__tuple_like_tag_t __tag, _UTuple&& __u) const
479 {
480 _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u));
481 _M_tail(*this)._M_assign(__tag, std::forward<_UTuple>(__u));
482 }
483#endif // C++23
484
485 protected:
486 _GLIBCXX20_CONSTEXPR
487 void
488 _M_swap(_Tuple_impl& __in)
489 {
490 using std::swap;
491 swap(_M_head(*this), _M_head(__in));
492 _Inherited::_M_swap(_M_tail(__in));
493 }
494
495#if __cpp_lib_ranges_zip // >= C++23
496 constexpr void
497 _M_swap(const _Tuple_impl& __in) const
498 {
499 using std::swap;
500 swap(_M_head(*this), _M_head(__in));
501 _Inherited::_M_swap(_M_tail(__in));
502 }
503#endif // C++23
504 };
505
506 // Basis case of inheritance recursion.
507 template<size_t _Idx, typename _Head>
508 struct _GLIBCXX_STRUCTURAL _Tuple_impl<_Idx, _Head>
509 : private _Head_base<_Idx, _Head>
510 {
511 template<size_t, typename...> friend struct _Tuple_impl;
512
513 typedef _Head_base<_Idx, _Head> _Base;
514
515 static constexpr _Head&
516 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
517
518 static constexpr const _Head&
519 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
520
521 constexpr
522 _Tuple_impl()
523 : _Base() { }
524
525 explicit constexpr
526 _Tuple_impl(const _Head& __head)
527 : _Base(__head)
528 { }
529
530 template<typename _UHead>
531 explicit constexpr
532 _Tuple_impl(_UHead&& __head)
533 : _Base(std::forward<_UHead>(__head))
534 { }
535
536 constexpr _Tuple_impl(const _Tuple_impl&) = default;
537
538 // _GLIBCXX_RESOLVE_LIB_DEFECTS
539 // 2729. Missing SFINAE on std::pair::operator=
540 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
541
542#if _GLIBCXX_INLINE_VERSION
543 _Tuple_impl(_Tuple_impl&&) = default;
544#else
545 constexpr
546 _Tuple_impl(_Tuple_impl&& __in)
547 noexcept(is_nothrow_move_constructible<_Head>::value)
548 : _Base(static_cast<_Base&&>(__in))
549 { }
550#endif
551
552 template<typename _UHead>
553 constexpr
554 _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
555 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in))
556 { }
557
558 template<typename _UHead>
559 constexpr
560 _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
561 : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
562 { }
563
564#if __cpp_lib_ranges_zip // >= C++23
565 template<typename _UHead>
566 constexpr
567 _Tuple_impl(_Tuple_impl<_Idx, _UHead>& __in)
568 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in))
569 { }
570
571 template<typename _UHead>
572 constexpr
573 _Tuple_impl(const _Tuple_impl<_Idx, _UHead>&& __in)
574 : _Base(std::forward<const _UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
575 { }
576#endif // C++23
577
578#if __cpp_lib_tuple_like // >= C++23
579 template<typename _UTuple>
580 constexpr
581 _Tuple_impl(__tuple_like_tag_t, _UTuple&& __u, index_sequence<0>)
582 : _Tuple_impl(std::get<0>(std::forward<_UTuple>(__u)))
583 { }
584#endif // C++23
585
586 template<typename _Alloc>
587 _GLIBCXX20_CONSTEXPR
588 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
589 : _Base(__tag, __use_alloc<_Head>(__a))
590 { }
591
592 template<typename _Alloc>
593 _GLIBCXX20_CONSTEXPR
594 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
595 const _Head& __head)
596 : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), __head)
597 { }
598
599 template<typename _Alloc, typename _UHead>
600 _GLIBCXX20_CONSTEXPR
601 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
602 _UHead&& __head)
603 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
604 std::forward<_UHead>(__head))
605 { }
606
607 template<typename _Alloc>
608 _GLIBCXX20_CONSTEXPR
609 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
610 const _Tuple_impl& __in)
611 : _Base(__use_alloc<_Head, _Alloc, const _Head&>(__a), _M_head(__in))
612 { }
613
614 template<typename _Alloc>
615 _GLIBCXX20_CONSTEXPR
616 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
617 _Tuple_impl&& __in)
618 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
619 std::forward<_Head>(_M_head(__in)))
620 { }
621
622 template<typename _Alloc, typename _UHead>
623 _GLIBCXX20_CONSTEXPR
624 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
625 const _Tuple_impl<_Idx, _UHead>& __in)
626 : _Base(__use_alloc<_Head, _Alloc, const _UHead&>(__a),
627 _Tuple_impl<_Idx, _UHead>::_M_head(__in))
628 { }
629
630 template<typename _Alloc, typename _UHead>
631 _GLIBCXX20_CONSTEXPR
632 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
633 _Tuple_impl<_Idx, _UHead>&& __in)
634 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
635 std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
636 { }
637
638#if __cpp_lib_ranges_zip // >= C++23
639 template<typename _Alloc, typename _UHead>
640 constexpr
641 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
642 _Tuple_impl<_Idx, _UHead>& __in)
643 : _Base(__use_alloc<_Head, _Alloc, _UHead&>(__a),
644 _Tuple_impl<_Idx, _UHead>::_M_head(__in))
645 { }
646
647 template<typename _Alloc, typename _UHead>
648 constexpr
649 _Tuple_impl(allocator_arg_t, const _Alloc& __a,
650 const _Tuple_impl<_Idx, _UHead>&& __in)
651 : _Base(__use_alloc<_Head, _Alloc, const _UHead>(__a),
652 std::forward<const _UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
653 { }
654#endif // C++23
655
656#if __cpp_lib_tuple_like // >= C++23
657 template<typename _Alloc, typename _UTuple>
658 constexpr
659 _Tuple_impl(__tuple_like_tag_t, allocator_arg_t __tag, const _Alloc& __a,
660 _UTuple&& __u, index_sequence<0>)
661 : _Tuple_impl(__tag, __a, std::get<0>(std::forward<_UTuple>(__u)))
662 { }
663#endif // C++23
664
665 template<typename _UHead>
666 _GLIBCXX20_CONSTEXPR
667 void
668 _M_assign(const _Tuple_impl<_Idx, _UHead>& __in)
669 {
670 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
671 }
672
673 template<typename _UHead>
674 _GLIBCXX20_CONSTEXPR
675 void
676 _M_assign(_Tuple_impl<_Idx, _UHead>&& __in)
677 {
678 _M_head(*this)
679 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
680 }
681
682#if __cpp_lib_ranges_zip // >= C++23
683 template<typename _UHead>
684 constexpr void
685 _M_assign(const _Tuple_impl<_Idx, _UHead>& __in) const
686 {
687 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
688 }
689
690 template<typename _UHead>
691 constexpr void
692 _M_assign(_Tuple_impl<_Idx, _UHead>&& __in) const
693 {
694 _M_head(*this)
695 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
696 }
697#endif // C++23
698
699#if __cpp_lib_tuple_like // >= C++23
700 template<typename _UTuple>
701 constexpr void
702 _M_assign(__tuple_like_tag_t, _UTuple&& __u)
703 { _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u)); }
704
705 template<typename _UTuple>
706 constexpr void
707 _M_assign(__tuple_like_tag_t, _UTuple&& __u) const
708 { _M_head(*this) = std::get<_Idx>(std::forward<_UTuple>(__u)); }
709#endif // C++23
710
711 protected:
712 _GLIBCXX20_CONSTEXPR
713 void
714 _M_swap(_Tuple_impl& __in)
715 {
716 using std::swap;
717 swap(_M_head(*this), _M_head(__in));
718 }
719
720#if __cpp_lib_ranges_zip // >= C++23
721 constexpr void
722 _M_swap(const _Tuple_impl& __in) const
723 {
724 using std::swap;
725 swap(_M_head(*this), _M_head(__in));
726 }
727#endif // C++23
728 };
729
730 // Concept utility functions, reused in conditionally-explicit
731 // constructors.
732 template<bool, typename... _Types>
733 struct _TupleConstraints
734 {
735 template<typename... _UTypes>
736 using __constructible = __and_<is_constructible<_Types, _UTypes>...>;
737
738 template<typename... _UTypes>
739 using __convertible = __and_<is_convertible<_UTypes, _Types>...>;
740
741 // Constraint for a non-explicit constructor.
742 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
743 // and every Ui is implicitly convertible to Ti.
744 template<typename... _UTypes>
745 static constexpr bool __is_implicitly_constructible()
746 {
747 return __and_<__constructible<_UTypes...>,
748 __convertible<_UTypes...>
749 >::value;
750 }
751
752 // Constraint for a non-explicit constructor.
753 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
754 // but not every Ui is implicitly convertible to Ti.
755 template<typename... _UTypes>
756 static constexpr bool __is_explicitly_constructible()
757 {
758 return __and_<__constructible<_UTypes...>,
759 __not_<__convertible<_UTypes...>>
760 >::value;
761 }
762
763 static constexpr bool __is_implicitly_default_constructible()
764 {
765 return __and_<std::__is_implicitly_default_constructible<_Types>...
766 >::value;
767 }
768
769 static constexpr bool __is_explicitly_default_constructible()
770 {
771 return __and_<is_default_constructible<_Types>...,
772 __not_<__and_<
773 std::__is_implicitly_default_constructible<_Types>...>
774 >>::value;
775 }
776 };
777
778 // Partial specialization used when a required precondition isn't met,
779 // e.g. when sizeof...(_Types) != sizeof...(_UTypes).
780 template<typename... _Types>
781 struct _TupleConstraints<false, _Types...>
782 {
783 template<typename... _UTypes>
784 static constexpr bool __is_implicitly_constructible()
785 { return false; }
786
787 template<typename... _UTypes>
788 static constexpr bool __is_explicitly_constructible()
789 { return false; }
790 };
791 /// @endcond
792
793 /// Primary class template, tuple
794 template<typename... _Elements>
795 // _GLIBCXX_RESOLVE_LIB_DEFECTS
796 // 3990. Program-defined specializations of std::tuple and std::variant
797 // can't be properly supported
798 class _GLIBCXX_NO_SPECIALIZATIONS tuple
799 : public _Tuple_impl<0, _Elements...>
800 {
801 using _Inherited = _Tuple_impl<0, _Elements...>;
802
803#if __cpp_concepts && __cpp_consteval && __cpp_conditional_explicit // >= C++20
804 template<typename... _UTypes>
805 static consteval bool
806 __constructible()
807 {
808 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
809 return __and_v<is_constructible<_Elements, _UTypes>...>;
810 else
811 return false;
812 }
813
814 template<typename... _UTypes>
815 static consteval bool
816 __nothrow_constructible()
817 {
818 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
820 else
821 return false;
822 }
823
824 template<typename... _UTypes>
825 static consteval bool
826 __convertible()
827 {
828 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
829 return __and_v<is_convertible<_UTypes, _Elements>...>;
830 else
831 return false;
832 }
833
834 // _GLIBCXX_RESOLVE_LIB_DEFECTS
835 // 3121. tuple constructor constraints for UTypes&&... overloads
836 template<typename... _UTypes>
837 static consteval bool
838 __disambiguating_constraint()
839 {
840 if constexpr (sizeof...(_Elements) != sizeof...(_UTypes))
841 return false;
842 else if constexpr (sizeof...(_Elements) == 1)
843 {
844 using _U0 = typename _Nth_type<0, _UTypes...>::type;
845 return !is_same_v<remove_cvref_t<_U0>, tuple>;
846 }
847 else if constexpr (sizeof...(_Elements) < 4)
848 {
849 using _U0 = typename _Nth_type<0, _UTypes...>::type;
850 if constexpr (!is_same_v<remove_cvref_t<_U0>, allocator_arg_t>)
851 return true;
852 else
853 {
854 using _T0 = typename _Nth_type<0, _Elements...>::type;
855 return is_same_v<remove_cvref_t<_T0>, allocator_arg_t>;
856 }
857 }
858 return true;
859 }
860
861 // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
862 // and the single element in Types can be initialized from TUPLE,
863 // or is the same type as tuple_element_t<0, TUPLE>.
864 template<typename _Tuple>
865 static consteval bool
866 __use_other_ctor()
867 {
868 if constexpr (sizeof...(_Elements) != 1)
869 return false;
870 else if constexpr (is_same_v<remove_cvref_t<_Tuple>, tuple>)
871 return true; // Should use a copy/move constructor instead.
872 else
873 {
874 using _Tp = typename _Nth_type<0, _Elements...>::type;
875 if constexpr (is_convertible_v<_Tuple, _Tp>)
876 return true;
877 else if constexpr (is_constructible_v<_Tp, _Tuple>)
878 return true;
879 }
880 return false;
881 }
882
883 template<typename... _Up>
884 static consteval bool
885 __dangles()
886 {
887#if __has_builtin(__reference_constructs_from_temporary)
888 return (__reference_constructs_from_temporary(_Elements, _Up&&)
889 || ...);
890#else
891 return false;
892#endif
893 }
894
895#if __cpp_lib_tuple_like // >= C++23
896 // _GLIBCXX_RESOLVE_LIB_DEFECTS
897 // 4045. tuple can create dangling references from tuple-like
898 template<typename _UTuple>
899 static consteval bool
900 __dangles_from_tuple_like()
901 {
902 return []<size_t... _Is>(index_sequence<_Is...>) {
903 return __dangles<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
904 }(index_sequence_for<_Elements...>{});
905 }
906
907 template<typename _UTuple>
908 static consteval bool
909 __constructible_from_tuple_like()
910 {
911 return []<size_t... _Is>(index_sequence<_Is...>) {
912 return __constructible<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
913 }(index_sequence_for<_Elements...>{});
914 }
915
916 template<typename _UTuple>
917 static consteval bool
918 __convertible_from_tuple_like()
919 {
920 return []<size_t... _Is>(index_sequence<_Is...>) {
921 return __convertible<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
922 }(index_sequence_for<_Elements...>{});
923 }
924#endif // C++23
925
926 public:
927 constexpr
928 explicit(!(__is_implicitly_default_constructible_v<_Elements> && ...))
929 tuple()
930 noexcept((is_nothrow_default_constructible_v<_Elements> && ...))
931 requires (is_default_constructible_v<_Elements> && ...)
932 : _Inherited()
933 { }
934
935 // Defined as a template to work around PR libstdc++/116440.
936 template<typename = void>
937 constexpr explicit(!__convertible<const _Elements&...>())
938 tuple(const type_identity_t<_Elements>&... __elements)
939 noexcept(__nothrow_constructible<const _Elements&...>())
940 requires (__constructible<const _Elements&...>())
941 : _Inherited(__elements...)
942 { }
943
944 template<typename... _UTypes>
945 requires (__disambiguating_constraint<_UTypes...>())
946 && (__constructible<_UTypes...>())
947 && (!__dangles<_UTypes...>())
948 constexpr explicit(!__convertible<_UTypes...>())
949 tuple(_UTypes&&... __u)
950 noexcept(__nothrow_constructible<_UTypes...>())
951 : _Inherited(std::forward<_UTypes>(__u)...)
952 { }
953
954 template<typename... _UTypes>
955 requires (__disambiguating_constraint<_UTypes...>())
956 && (__constructible<_UTypes...>())
957 && (__dangles<_UTypes...>())
958 tuple(_UTypes&&...) = delete;
959
960 constexpr tuple(const tuple&) = default;
961
962 constexpr
963 tuple(tuple&&) requires (is_move_constructible_v<_Elements> && ...)
964 = default;
965
966 template<typename... _UTypes>
967 requires (__constructible<const _UTypes&...>())
968 && (!__use_other_ctor<const tuple<_UTypes...>&>())
969 && (!__dangles<const _UTypes&...>())
970 constexpr explicit(!__convertible<const _UTypes&...>())
971 tuple(const tuple<_UTypes...>& __u)
972 noexcept(__nothrow_constructible<const _UTypes&...>())
973 : _Inherited(static_cast<const _Tuple_impl<0, _UTypes...>&>(__u))
974 { }
975
976 template<typename... _UTypes>
977 requires (__constructible<const _UTypes&...>())
978 && (!__use_other_ctor<const tuple<_UTypes...>&>())
979 && (__dangles<const _UTypes&...>())
980 tuple(const tuple<_UTypes...>&) = delete;
981
982 template<typename... _UTypes>
983 requires (__constructible<_UTypes...>())
984 && (!__use_other_ctor<tuple<_UTypes...>>())
985 && (!__dangles<_UTypes...>())
986 constexpr explicit(!__convertible<_UTypes...>())
987 tuple(tuple<_UTypes...>&& __u)
988 noexcept(__nothrow_constructible<_UTypes...>())
989 : _Inherited(static_cast<_Tuple_impl<0, _UTypes...>&&>(__u))
990 { }
991
992 template<typename... _UTypes>
993 requires (__constructible<_UTypes...>())
994 && (!__use_other_ctor<tuple<_UTypes...>>())
995 && (__dangles<_UTypes...>())
996 tuple(tuple<_UTypes...>&&) = delete;
997
998#if __cpp_lib_ranges_zip // >= C++23
999 template<typename... _UTypes>
1000 requires (__constructible<_UTypes&...>())
1001 && (!__use_other_ctor<tuple<_UTypes...>&>())
1002 && (!__dangles<_UTypes&...>())
1003 constexpr explicit(!__convertible<_UTypes&...>())
1004 tuple(tuple<_UTypes...>& __u)
1005 noexcept(__nothrow_constructible<_UTypes&...>())
1006 : _Inherited(static_cast<_Tuple_impl<0, _UTypes...>&>(__u))
1007 { }
1008
1009 template<typename... _UTypes>
1010 requires (__constructible<_UTypes&...>())
1011 && (!__use_other_ctor<tuple<_UTypes...>&>())
1012 && (__dangles<_UTypes&...>())
1013 tuple(tuple<_UTypes...>&) = delete;
1014
1015 template<typename... _UTypes>
1016 requires (__constructible<const _UTypes...>())
1017 && (!__use_other_ctor<const tuple<_UTypes...>>())
1018 && (!__dangles<const _UTypes...>())
1019 constexpr explicit(!__convertible<const _UTypes...>())
1020 tuple(const tuple<_UTypes...>&& __u)
1021 noexcept(__nothrow_constructible<const _UTypes...>())
1022 : _Inherited(static_cast<const _Tuple_impl<0, _UTypes...>&&>(__u))
1023 { }
1024
1025 template<typename... _UTypes>
1026 requires (__constructible<const _UTypes...>())
1027 && (!__use_other_ctor<const tuple<_UTypes...>>())
1028 && (__dangles<const _UTypes...>())
1029 tuple(const tuple<_UTypes...>&&) = delete;
1030#endif // C++23
1031
1032 template<typename _U1, typename _U2>
1033 requires (sizeof...(_Elements) == 2)
1034 && (__constructible<const _U1&, const _U2&>())
1035 && (!__dangles<const _U1&, const _U2&>())
1036 constexpr explicit(!__convertible<const _U1&, const _U2&>())
1037 tuple(const pair<_U1, _U2>& __u)
1038 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1039 : _Inherited(__u.first, __u.second)
1040 { }
1041
1042 template<typename _U1, typename _U2>
1043 requires (sizeof...(_Elements) == 2)
1044 && (__constructible<const _U1&, const _U2&>())
1045 && (__dangles<const _U1&, const _U2&>())
1046 tuple(const pair<_U1, _U2>&) = delete;
1047
1048 template<typename _U1, typename _U2>
1049 requires (sizeof...(_Elements) == 2)
1050 && (__constructible<_U1, _U2>())
1051 && (!__dangles<_U1, _U2>())
1052 constexpr explicit(!__convertible<_U1, _U2>())
1053 tuple(pair<_U1, _U2>&& __u)
1054 noexcept(__nothrow_constructible<_U1, _U2>())
1055 : _Inherited(std::forward<_U1>(__u.first),
1056 std::forward<_U2>(__u.second))
1057 { }
1058
1059 template<typename _U1, typename _U2>
1060 requires (sizeof...(_Elements) == 2)
1061 && (__constructible<_U1, _U2>())
1062 && (__dangles<_U1, _U2>())
1063 tuple(pair<_U1, _U2>&&) = delete;
1064
1065#if __cpp_lib_ranges_zip // >= C++23
1066 template<typename _U1, typename _U2>
1067 requires (sizeof...(_Elements) == 2)
1068 && (__constructible<_U1&, _U2&>())
1069 && (!__dangles<_U1&, _U2&>())
1070 constexpr explicit(!__convertible<_U1&, _U2&>())
1071 tuple(pair<_U1, _U2>& __u)
1072 noexcept(__nothrow_constructible<_U1&, _U2&>())
1073 : _Inherited(__u.first, __u.second)
1074 { }
1075
1076 template<typename _U1, typename _U2>
1077 requires (sizeof...(_Elements) == 2)
1078 && (__constructible<_U1&, _U2&>())
1079 && (__dangles<_U1&, _U2&>())
1080 tuple(pair<_U1, _U2>&) = delete;
1081
1082 template<typename _U1, typename _U2>
1083 requires (sizeof...(_Elements) == 2)
1084 && (__constructible<const _U1, const _U2>())
1085 && (!__dangles<const _U1, const _U2>())
1086 constexpr explicit(!__convertible<const _U1, const _U2>())
1087 tuple(const pair<_U1, _U2>&& __u)
1088 noexcept(__nothrow_constructible<const _U1, const _U2>())
1089 : _Inherited(std::forward<const _U1>(__u.first),
1090 std::forward<const _U2>(__u.second))
1091 { }
1092
1093 template<typename _U1, typename _U2>
1094 requires (sizeof...(_Elements) == 2)
1095 && (__constructible<const _U1, const _U2>())
1096 && (__dangles<const _U1, const _U2>())
1097 tuple(const pair<_U1, _U2>&&) = delete;
1098#endif // C++23
1099
1100#if __cpp_lib_tuple_like // >= C++23
1101 template<__eligible_tuple_like<tuple> _UTuple>
1102 requires (__constructible_from_tuple_like<_UTuple>())
1103 && (!__use_other_ctor<_UTuple>())
1104 && (!__dangles_from_tuple_like<_UTuple>())
1105 constexpr explicit(!__convertible_from_tuple_like<_UTuple>())
1106 tuple(_UTuple&& __u)
1107 : _Inherited(__tuple_like_tag_t{},
1109 index_sequence_for<_Elements...>{})
1110 { }
1111
1112 template<__eligible_tuple_like<tuple> _UTuple>
1113 requires (__constructible_from_tuple_like<_UTuple>())
1114 && (!__use_other_ctor<_UTuple>())
1115 && (__dangles_from_tuple_like<_UTuple>())
1116 tuple(_UTuple&&) = delete;
1117#endif // C++23
1118
1119 // Allocator-extended constructors.
1120
1121 template<typename _Alloc>
1122 constexpr
1123 explicit(!(__is_implicitly_default_constructible_v<_Elements> && ...))
1124 tuple(allocator_arg_t __tag, const _Alloc& __a)
1125 requires (is_default_constructible_v<_Elements> && ...)
1126 : _Inherited(__tag, __a)
1127 { }
1128
1129 template<typename _Alloc>
1130 constexpr explicit(!__convertible<const _Elements&...>())
1131 tuple(allocator_arg_t __tag, const _Alloc& __a,
1132 const type_identity_t<_Elements>&... __elements)
1133 requires (__constructible<const _Elements&...>())
1134 : _Inherited(__tag, __a, __elements...)
1135 { }
1136
1137 template<typename _Alloc, typename... _UTypes>
1138 requires (__disambiguating_constraint<_UTypes...>())
1139 && (__constructible<_UTypes...>())
1140 && (!__dangles<_UTypes...>())
1141 constexpr explicit(!__convertible<_UTypes...>())
1142 tuple(allocator_arg_t __tag, const _Alloc& __a, _UTypes&&... __u)
1143 : _Inherited(__tag, __a, std::forward<_UTypes>(__u)...)
1144 { }
1145
1146 template<typename _Alloc, typename... _UTypes>
1147 requires (__disambiguating_constraint<_UTypes...>())
1148 && (__constructible<_UTypes...>())
1149 && (__dangles<_UTypes...>())
1150 tuple(allocator_arg_t, const _Alloc&, _UTypes&&...) = delete;
1151
1152 template<typename _Alloc>
1153 constexpr
1154 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __u)
1155 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__u))
1156 { }
1157
1158 template<typename _Alloc>
1159 requires (__constructible<_Elements...>())
1160 constexpr
1161 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __u)
1162 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__u))
1163 { }
1164
1165 template<typename _Alloc, typename... _UTypes>
1166 requires (__constructible<const _UTypes&...>())
1167 && (!__use_other_ctor<const tuple<_UTypes...>&>())
1168 && (!__dangles<const _UTypes&...>())
1169 constexpr explicit(!__convertible<const _UTypes&...>())
1170 tuple(allocator_arg_t __tag, const _Alloc& __a,
1171 const tuple<_UTypes...>& __u)
1172 : _Inherited(__tag, __a,
1173 static_cast<const _Tuple_impl<0, _UTypes...>&>(__u))
1174 { }
1175
1176 template<typename _Alloc, typename... _UTypes>
1177 requires (__constructible<const _UTypes&...>())
1178 && (!__use_other_ctor<const tuple<_UTypes...>&>())
1179 && (__dangles<const _UTypes&...>())
1180 tuple(allocator_arg_t, const _Alloc&, const tuple<_UTypes...>&) = delete;
1181
1182 template<typename _Alloc, typename... _UTypes>
1183 requires (__constructible<_UTypes...>())
1184 && (!__use_other_ctor<tuple<_UTypes...>>())
1185 && (!__dangles<_UTypes...>())
1186 constexpr explicit(!__use_other_ctor<tuple<_UTypes...>>())
1187 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_UTypes...>&& __u)
1188 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _UTypes...>&&>(__u))
1189 { }
1190
1191 template<typename _Alloc, typename... _UTypes>
1192 requires (__constructible<_UTypes...>())
1193 && (!__use_other_ctor<tuple<_UTypes...>>())
1194 && (__dangles<_UTypes...>())
1195 tuple(allocator_arg_t, const _Alloc&, tuple<_UTypes...>&&) = delete;
1196
1197#if __cpp_lib_ranges_zip // >= C++23
1198 template<typename _Alloc, typename... _UTypes>
1199 requires (__constructible<_UTypes&...>())
1200 && (!__use_other_ctor<tuple<_UTypes...>&>())
1201 && (!__dangles<_UTypes&...>())
1202 constexpr explicit(!__convertible<_UTypes&...>())
1203 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_UTypes...>& __u)
1204 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _UTypes...>&>(__u))
1205 { }
1206
1207 template<typename _Alloc, typename... _UTypes>
1208 requires (__constructible<_UTypes&...>())
1209 && (!__use_other_ctor<tuple<_UTypes...>&>())
1210 && (__dangles<_UTypes&...>())
1211 tuple(allocator_arg_t, const _Alloc&, tuple<_UTypes...>&) = delete;
1212
1213 template<typename _Alloc, typename... _UTypes>
1214 requires (__constructible<const _UTypes...>())
1215 && (!__use_other_ctor<const tuple<_UTypes...>>())
1216 && (!__dangles<const _UTypes...>())
1217 constexpr explicit(!__convertible<const _UTypes...>())
1218 tuple(allocator_arg_t __tag, const _Alloc& __a,
1219 const tuple<_UTypes...>&& __u)
1220 : _Inherited(__tag, __a,
1221 static_cast<const _Tuple_impl<0, _UTypes...>&&>(__u))
1222 { }
1223
1224 template<typename _Alloc, typename... _UTypes>
1225 requires (__constructible<const _UTypes...>())
1226 && (!__use_other_ctor<const tuple<_UTypes...>>())
1227 && (__dangles<const _UTypes...>())
1228 tuple(allocator_arg_t, const _Alloc&, const tuple<_UTypes...>&&) = delete;
1229#endif // C++23
1230
1231 template<typename _Alloc, typename _U1, typename _U2>
1232 requires (sizeof...(_Elements) == 2)
1233 && (__constructible<const _U1&, const _U2&>())
1234 && (!__dangles<const _U1&, const _U2&>())
1235 constexpr explicit(!__convertible<const _U1&, const _U2&>())
1236 tuple(allocator_arg_t __tag, const _Alloc& __a,
1237 const pair<_U1, _U2>& __u)
1238 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
1239 : _Inherited(__tag, __a, __u.first, __u.second)
1240 { }
1241
1242 template<typename _Alloc, typename _U1, typename _U2>
1243 requires (sizeof...(_Elements) == 2)
1244 && (__constructible<const _U1&, const _U2&>())
1245 && (__dangles<const _U1&, const _U2&>())
1246 tuple(allocator_arg_t, const _Alloc&, const pair<_U1, _U2>&) = delete;
1247
1248 template<typename _Alloc, typename _U1, typename _U2>
1249 requires (sizeof...(_Elements) == 2)
1250 && (__constructible<_U1, _U2>())
1251 && (!__dangles<_U1, _U2>())
1252 constexpr explicit(!__convertible<_U1, _U2>())
1253 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __u)
1254 noexcept(__nothrow_constructible<_U1, _U2>())
1255 : _Inherited(__tag, __a, std::move(__u.first), std::move(__u.second))
1256 { }
1257
1258 template<typename _Alloc, typename _U1, typename _U2>
1259 requires (sizeof...(_Elements) == 2)
1260 && (__constructible<_U1, _U2>())
1261 && (__dangles<_U1, _U2>())
1262 tuple(allocator_arg_t, const _Alloc&, pair<_U1, _U2>&&) = delete;
1263
1264#if __cpp_lib_ranges_zip // >= C++23
1265 template<typename _Alloc, typename _U1, typename _U2>
1266 requires (sizeof...(_Elements) == 2)
1267 && (__constructible<_U1&, _U2&>())
1268 && (!__dangles<_U1&, _U2&>())
1269 constexpr explicit(!__convertible<_U1&, _U2&>())
1270 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>& __u)
1271 noexcept(__nothrow_constructible<_U1&, _U2&>())
1272 : _Inherited(__tag, __a, __u.first, __u.second)
1273 { }
1274
1275 template<typename _Alloc, typename _U1, typename _U2>
1276 requires (sizeof...(_Elements) == 2)
1277 && (__constructible<_U1&, _U2&>())
1278 && (__dangles<_U1&, _U2&>())
1279 tuple(allocator_arg_t, const _Alloc&, pair<_U1, _U2>&) = delete;
1280
1281 template<typename _Alloc, typename _U1, typename _U2>
1282 requires (sizeof...(_Elements) == 2)
1283 && (__constructible<const _U1, const _U2>())
1284 && (!__dangles<const _U1, const _U2>())
1285 constexpr explicit(!__convertible<const _U1, const _U2>())
1286 tuple(allocator_arg_t __tag, const _Alloc& __a,
1287 const pair<_U1, _U2>&& __u)
1288 noexcept(__nothrow_constructible<const _U1, const _U2>())
1289 : _Inherited(__tag, __a, std::move(__u.first), std::move(__u.second))
1290 { }
1291
1292 template<typename _Alloc, typename _U1, typename _U2>
1293 requires (sizeof...(_Elements) == 2)
1294 && (__constructible<const _U1, const _U2>())
1295 && (__dangles<const _U1, const _U2>())
1296 tuple(allocator_arg_t, const _Alloc&, const pair<_U1, _U2>&&) = delete;
1297#endif // C++23
1298
1299#if __cpp_lib_tuple_like // >= C++23
1300 template<typename _Alloc, __eligible_tuple_like<tuple> _UTuple>
1301 requires (__constructible_from_tuple_like<_UTuple>())
1302 && (!__use_other_ctor<_UTuple>())
1303 && (!__dangles_from_tuple_like<_UTuple>())
1304 constexpr explicit(!__convertible_from_tuple_like<_UTuple>())
1305 tuple(allocator_arg_t __tag, const _Alloc& __a, _UTuple&& __u)
1306 : _Inherited(__tuple_like_tag_t{},
1307 __tag, __a, std::forward<_UTuple>(__u),
1308 index_sequence_for<_Elements...>{})
1309 { }
1310
1311 template<typename _Alloc, __eligible_tuple_like<tuple> _UTuple>
1312 requires (__constructible_from_tuple_like<_UTuple>())
1313 && (!__use_other_ctor<_UTuple>())
1314 && (__dangles_from_tuple_like<_UTuple>())
1315 tuple(allocator_arg_t, const _Alloc&, _UTuple&&) = delete;
1316#endif // C++23
1317
1318#else // !(concepts && conditional_explicit)
1319
1320 template<bool _Cond>
1321 using _TCC = _TupleConstraints<_Cond, _Elements...>;
1322
1323 // Constraint for non-explicit default constructor
1324 template<bool _Dummy>
1325 using _ImplicitDefaultCtor = __enable_if_t<
1326 _TCC<_Dummy>::__is_implicitly_default_constructible(),
1327 bool>;
1328
1329 // Constraint for explicit default constructor
1330 template<bool _Dummy>
1331 using _ExplicitDefaultCtor = __enable_if_t<
1332 _TCC<_Dummy>::__is_explicitly_default_constructible(),
1333 bool>;
1334
1335 // Constraint for non-explicit constructors
1336 template<bool _Cond, typename... _Args>
1337 using _ImplicitCtor = __enable_if_t<
1338 _TCC<_Cond>::template __is_implicitly_constructible<_Args...>(),
1339 bool>;
1340
1341 // Constraint for non-explicit constructors
1342 template<bool _Cond, typename... _Args>
1343 using _ExplicitCtor = __enable_if_t<
1344 _TCC<_Cond>::template __is_explicitly_constructible<_Args...>(),
1345 bool>;
1346
1347 // Condition for noexcept-specifier of a constructor.
1348 template<typename... _UElements>
1349 static constexpr bool __nothrow_constructible()
1350 {
1351 return
1352 __and_<is_nothrow_constructible<_Elements, _UElements>...>::value;
1353 }
1354
1355 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) == 1.
1356 template<typename _Up>
1357 static constexpr bool __valid_args()
1358 {
1359 return sizeof...(_Elements) == 1
1360 && !is_same<tuple, __remove_cvref_t<_Up>>::value;
1361 }
1362
1363 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) > 1.
1364 template<typename, typename, typename... _Tail>
1365 static constexpr bool __valid_args()
1366 { return (sizeof...(_Tail) + 2) == sizeof...(_Elements); }
1367
1368 /* Constraint for constructors with a tuple<UTypes...> parameter ensures
1369 * that the constructor is only viable when it would not interfere with
1370 * tuple(UTypes&&...) or tuple(const tuple&) or tuple(tuple&&).
1371 * Such constructors are only viable if:
1372 * either sizeof...(Types) != 1,
1373 * or (when Types... expands to T and UTypes... expands to U)
1374 * is_convertible_v<TUPLE, T>, is_constructible_v<T, TUPLE>,
1375 * and is_same_v<T, U> are all false.
1376 */
1377 template<typename _Tuple, typename = tuple,
1378 typename = __remove_cvref_t<_Tuple>>
1379 struct _UseOtherCtor
1380 : false_type
1381 { };
1382 // If TUPLE is convertible to the single element in *this,
1383 // then TUPLE should match tuple(UTypes&&...) instead.
1384 template<typename _Tuple, typename _Tp, typename _Up>
1385 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Up>>
1386 : __or_<is_convertible<_Tuple, _Tp>, is_constructible<_Tp, _Tuple>>::type
1387 { };
1388 // If TUPLE and *this each have a single element of the same type,
1389 // then TUPLE should match a copy/move constructor instead.
1390 template<typename _Tuple, typename _Tp>
1391 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Tp>>
1392 : true_type
1393 { };
1394
1395 // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
1396 // and the single element in Types can be initialized from TUPLE,
1397 // or is the same type as tuple_element_t<0, TUPLE>.
1398 template<typename _Tuple>
1399 static constexpr bool __use_other_ctor()
1400 { return _UseOtherCtor<_Tuple>::value; }
1401
1402 /// @cond undocumented
1403#undef __glibcxx_no_dangling_refs
1404#if __has_builtin(__reference_constructs_from_temporary) \
1405 && defined _GLIBCXX_DEBUG
1406 // Error if construction from U... would create a dangling ref.
1407# if __cpp_fold_expressions
1408# define __glibcxx_dangling_refs(U) \
1409 (__reference_constructs_from_temporary(_Elements, U) || ...)
1410# else
1411# define __glibcxx_dangling_refs(U) \
1412 __or_<__bool_constant<__reference_constructs_from_temporary(_Elements, U) \
1413 >...>::value
1414# endif
1415# define __glibcxx_no_dangling_refs(U) \
1416 static_assert(!__glibcxx_dangling_refs(U), \
1417 "std::tuple constructor creates a dangling reference")
1418#else
1419# define __glibcxx_no_dangling_refs(U)
1420#endif
1421 /// @endcond
1422
1423 public:
1424 template<typename _Dummy = void,
1425 _ImplicitDefaultCtor<is_void<_Dummy>::value> = true>
1426 constexpr
1427 tuple()
1428 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
1429 : _Inherited() { }
1430
1431 template<typename _Dummy = void,
1432 _ExplicitDefaultCtor<is_void<_Dummy>::value> = false>
1433 explicit constexpr
1434 tuple()
1435 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
1436 : _Inherited() { }
1437
1438 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
1439 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
1440 constexpr
1441 tuple(const __type_identity_t<_Elements>&... __elements)
1442 noexcept(__nothrow_constructible<const _Elements&...>())
1443 : _Inherited(__elements...) { }
1444
1445 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
1446 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
1447 explicit constexpr
1448 tuple(const __type_identity_t<_Elements>&... __elements)
1449 noexcept(__nothrow_constructible<const _Elements&...>())
1450 : _Inherited(__elements...) { }
1451
1452 template<typename... _UElements,
1453 bool _Valid = __valid_args<_UElements...>(),
1454 _ImplicitCtor<_Valid, _UElements...> = true>
1455 constexpr
1456 tuple(_UElements&&... __elements)
1457 noexcept(__nothrow_constructible<_UElements...>())
1458 : _Inherited(std::forward<_UElements>(__elements)...)
1459 { __glibcxx_no_dangling_refs(_UElements&&); }
1460
1461 template<typename... _UElements,
1462 bool _Valid = __valid_args<_UElements...>(),
1463 _ExplicitCtor<_Valid, _UElements...> = false>
1464 explicit constexpr
1465 tuple(_UElements&&... __elements)
1466 noexcept(__nothrow_constructible<_UElements...>())
1467 : _Inherited(std::forward<_UElements>(__elements)...)
1468 { __glibcxx_no_dangling_refs(_UElements&&); }
1469
1470 constexpr tuple(const tuple&) = default;
1471
1472 constexpr tuple(tuple&&) = default;
1473
1474 template<typename... _UElements,
1475 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1476 && !__use_other_ctor<const tuple<_UElements...>&>(),
1477 _ImplicitCtor<_Valid, const _UElements&...> = true>
1478 constexpr
1479 tuple(const tuple<_UElements...>& __in)
1480 noexcept(__nothrow_constructible<const _UElements&...>())
1481 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1482 { __glibcxx_no_dangling_refs(const _UElements&); }
1483
1484 template<typename... _UElements,
1485 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1486 && !__use_other_ctor<const tuple<_UElements...>&>(),
1487 _ExplicitCtor<_Valid, const _UElements&...> = false>
1488 explicit constexpr
1489 tuple(const tuple<_UElements...>& __in)
1490 noexcept(__nothrow_constructible<const _UElements&...>())
1491 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1492 { __glibcxx_no_dangling_refs(const _UElements&); }
1493
1494 template<typename... _UElements,
1495 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1496 && !__use_other_ctor<tuple<_UElements...>&&>(),
1497 _ImplicitCtor<_Valid, _UElements...> = true>
1498 constexpr
1499 tuple(tuple<_UElements...>&& __in)
1500 noexcept(__nothrow_constructible<_UElements...>())
1501 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1502 { __glibcxx_no_dangling_refs(_UElements&&); }
1503
1504 template<typename... _UElements,
1505 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1506 && !__use_other_ctor<tuple<_UElements...>&&>(),
1507 _ExplicitCtor<_Valid, _UElements...> = false>
1508 explicit constexpr
1509 tuple(tuple<_UElements...>&& __in)
1510 noexcept(__nothrow_constructible<_UElements...>())
1511 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1512 { __glibcxx_no_dangling_refs(_UElements&&); }
1513
1514 // Allocator-extended constructors.
1515
1516 template<typename _Alloc,
1517 _ImplicitDefaultCtor<is_object<_Alloc>::value> = true>
1518 _GLIBCXX20_CONSTEXPR
1519 tuple(allocator_arg_t __tag, const _Alloc& __a)
1520 : _Inherited(__tag, __a) { }
1521
1522 template<typename _Alloc,
1523 _ExplicitDefaultCtor<is_object<_Alloc>::value> = false>
1524 _GLIBCXX20_CONSTEXPR
1525 explicit
1526 tuple(allocator_arg_t __tag, const _Alloc& __a)
1527 : _Inherited(__tag, __a) { }
1528
1529 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
1530 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
1531 _GLIBCXX20_CONSTEXPR
1532 tuple(allocator_arg_t __tag, const _Alloc& __a,
1533 const __type_identity_t<_Elements>&... __elements)
1534 : _Inherited(__tag, __a, __elements...) { }
1535
1536 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
1537 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
1538 _GLIBCXX20_CONSTEXPR
1539 explicit
1540 tuple(allocator_arg_t __tag, const _Alloc& __a,
1541 const __type_identity_t<_Elements>&... __elements)
1542 : _Inherited(__tag, __a, __elements...) { }
1543
1544 template<typename _Alloc, typename... _UElements,
1545 bool _Valid = __valid_args<_UElements...>(),
1546 _ImplicitCtor<_Valid, _UElements...> = true>
1547 _GLIBCXX20_CONSTEXPR
1548 tuple(allocator_arg_t __tag, const _Alloc& __a,
1549 _UElements&&... __elements)
1550 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
1551 { __glibcxx_no_dangling_refs(_UElements&&); }
1552
1553 template<typename _Alloc, typename... _UElements,
1554 bool _Valid = __valid_args<_UElements...>(),
1555 _ExplicitCtor<_Valid, _UElements...> = false>
1556 _GLIBCXX20_CONSTEXPR
1557 explicit
1558 tuple(allocator_arg_t __tag, const _Alloc& __a,
1559 _UElements&&... __elements)
1560 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
1561 { __glibcxx_no_dangling_refs(_UElements&&); }
1562
1563 template<typename _Alloc>
1564 _GLIBCXX20_CONSTEXPR
1565 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
1566 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
1567
1568 template<typename _Alloc>
1569 _GLIBCXX20_CONSTEXPR
1570 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
1571 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
1572
1573 template<typename _Alloc, typename... _UElements,
1574 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1575 && !__use_other_ctor<const tuple<_UElements...>&>(),
1576 _ImplicitCtor<_Valid, const _UElements&...> = true>
1577 _GLIBCXX20_CONSTEXPR
1578 tuple(allocator_arg_t __tag, const _Alloc& __a,
1579 const tuple<_UElements...>& __in)
1580 : _Inherited(__tag, __a,
1581 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1582 { __glibcxx_no_dangling_refs(const _UElements&); }
1583
1584 template<typename _Alloc, typename... _UElements,
1585 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1586 && !__use_other_ctor<const tuple<_UElements...>&>(),
1587 _ExplicitCtor<_Valid, const _UElements&...> = false>
1588 _GLIBCXX20_CONSTEXPR
1589 explicit
1590 tuple(allocator_arg_t __tag, const _Alloc& __a,
1591 const tuple<_UElements...>& __in)
1592 : _Inherited(__tag, __a,
1593 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
1594 { __glibcxx_no_dangling_refs(const _UElements&); }
1595
1596 template<typename _Alloc, typename... _UElements,
1597 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1598 && !__use_other_ctor<tuple<_UElements...>&&>(),
1599 _ImplicitCtor<_Valid, _UElements...> = true>
1600 _GLIBCXX20_CONSTEXPR
1601 tuple(allocator_arg_t __tag, const _Alloc& __a,
1602 tuple<_UElements...>&& __in)
1603 : _Inherited(__tag, __a,
1604 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1605 { __glibcxx_no_dangling_refs(_UElements&&); }
1606
1607 template<typename _Alloc, typename... _UElements,
1608 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
1609 && !__use_other_ctor<tuple<_UElements...>&&>(),
1610 _ExplicitCtor<_Valid, _UElements...> = false>
1611 _GLIBCXX20_CONSTEXPR
1612 explicit
1613 tuple(allocator_arg_t __tag, const _Alloc& __a,
1614 tuple<_UElements...>&& __in)
1615 : _Inherited(__tag, __a,
1616 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
1617 { __glibcxx_no_dangling_refs(_UElements&&); }
1618#endif // concepts && conditional_explicit
1619
1620 // tuple assignment
1621
1622#if __cpp_concepts && __cpp_consteval // >= C++20
1623 private:
1624 template<typename... _UTypes>
1625 static consteval bool
1626 __assignable()
1627 {
1628 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
1629 return __and_v<is_assignable<_Elements&, _UTypes>...>;
1630 else
1631 return false;
1632 }
1633
1634 template<typename... _UTypes>
1635 static consteval bool
1636 __nothrow_assignable()
1637 {
1638 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
1640 else
1641 return false;
1642 }
1643
1644#if __cpp_lib_ranges_zip // >= C++23
1645 template<typename... _UTypes>
1646 static consteval bool
1647 __const_assignable()
1648 {
1649 if constexpr (sizeof...(_UTypes) == sizeof...(_Elements))
1651 else
1652 return false;
1653 }
1654#endif // C++23
1655
1656#if __cpp_lib_tuple_like // >= C++23
1657 template<typename _UTuple>
1658 static consteval bool
1659 __assignable_from_tuple_like()
1660 {
1661 return []<size_t... _Is>(index_sequence<_Is...>) {
1662 return __assignable<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
1663 }(index_sequence_for<_Elements...>{});
1664 }
1665
1666 template<typename _UTuple>
1667 static consteval bool
1668 __const_assignable_from_tuple_like()
1669 {
1670 return []<size_t... _Is>(index_sequence<_Is...>) {
1671 return __const_assignable<decltype(std::get<_Is>(std::declval<_UTuple>()))...>();
1672 }(index_sequence_for<_Elements...>{});
1673 }
1674#endif // C++23
1675
1676 public:
1677
1678 tuple& operator=(const tuple& __u) = delete;
1679
1680 constexpr tuple&
1681 operator=(const tuple& __u)
1682 noexcept(__nothrow_assignable<const _Elements&...>())
1683 requires (__assignable<const _Elements&...>())
1684 {
1685 this->_M_assign(__u);
1686 return *this;
1687 }
1688
1689 constexpr tuple&
1690 operator=(tuple&& __u)
1691 noexcept(__nothrow_assignable<_Elements...>())
1692 requires (__assignable<_Elements...>())
1693 {
1694 this->_M_assign(std::move(__u));
1695 return *this;
1696 }
1697
1698 template<typename... _UTypes>
1699 requires (__assignable<const _UTypes&...>())
1700 constexpr tuple&
1701 operator=(const tuple<_UTypes...>& __u)
1702 noexcept(__nothrow_assignable<const _UTypes&...>())
1703 {
1704 this->_M_assign(__u);
1705 return *this;
1706 }
1707
1708 template<typename... _UTypes>
1709 requires (__assignable<_UTypes...>())
1710 constexpr tuple&
1711 operator=(tuple<_UTypes...>&& __u)
1712 noexcept(__nothrow_assignable<_UTypes...>())
1713 {
1714 this->_M_assign(std::move(__u));
1715 return *this;
1716 }
1717
1718#if __cpp_lib_ranges_zip // >= C++23
1719 constexpr const tuple&
1720 operator=(const tuple& __u) const
1721 requires (__const_assignable<const _Elements&...>())
1722 {
1723 this->_M_assign(__u);
1724 return *this;
1725 }
1726
1727 constexpr const tuple&
1728 operator=(tuple&& __u) const
1729 requires (__const_assignable<_Elements...>())
1730 {
1731 this->_M_assign(std::move(__u));
1732 return *this;
1733 }
1734
1735 template<typename... _UTypes>
1736 constexpr const tuple&
1737 operator=(const tuple<_UTypes...>& __u) const
1738 requires (__const_assignable<const _UTypes&...>())
1739 {
1740 this->_M_assign(__u);
1741 return *this;
1742 }
1743
1744 template<typename... _UTypes>
1745 constexpr const tuple&
1746 operator=(tuple<_UTypes...>&& __u) const
1747 requires (__const_assignable<_UTypes...>())
1748 {
1749 this->_M_assign(std::move(__u));
1750 return *this;
1751 }
1752#endif // C++23
1753
1754 template<typename _U1, typename _U2>
1755 requires (__assignable<const _U1&, const _U2&>())
1756 constexpr tuple&
1757 operator=(const pair<_U1, _U2>& __u)
1758 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1759 {
1760 this->_M_head(*this) = __u.first;
1761 this->_M_tail(*this)._M_head(*this) = __u.second;
1762 return *this;
1763 }
1764
1765 template<typename _U1, typename _U2>
1766 requires (__assignable<_U1, _U2>())
1767 constexpr tuple&
1768 operator=(pair<_U1, _U2>&& __u)
1769 noexcept(__nothrow_assignable<_U1, _U2>())
1770 {
1771 this->_M_head(*this) = std::forward<_U1>(__u.first);
1772 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__u.second);
1773 return *this;
1774 }
1775
1776#if __cpp_lib_ranges_zip // >= C++23
1777 template<typename _U1, typename _U2>
1778 requires (__const_assignable<const _U1&, const _U2>())
1779 constexpr const tuple&
1780 operator=(const pair<_U1, _U2>& __u) const
1781 {
1782 this->_M_head(*this) = __u.first;
1783 this->_M_tail(*this)._M_head(*this) = __u.second;
1784 return *this;
1785 }
1786
1787 template<typename _U1, typename _U2>
1788 requires (__const_assignable<_U1, _U2>())
1789 constexpr const tuple&
1790 operator=(pair<_U1, _U2>&& __u) const
1791 {
1792 this->_M_head(*this) = std::forward<_U1>(__u.first);
1793 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__u.second);
1794 return *this;
1795 }
1796#endif // C++23
1797
1798#if __cpp_lib_tuple_like // >= C++23
1799 template<__eligible_tuple_like<tuple> _UTuple>
1800 requires (__assignable_from_tuple_like<_UTuple>())
1801 constexpr tuple&
1802 operator=(_UTuple&& __u)
1803 {
1804 this->_M_assign(__tuple_like_tag_t{}, std::forward<_UTuple>(__u));
1805 return *this;
1806 }
1807
1808 template<__eligible_tuple_like<tuple> _UTuple>
1809 requires (__const_assignable_from_tuple_like<_UTuple>())
1810 constexpr const tuple&
1811 operator=(_UTuple&& __u) const
1812 {
1813 this->_M_assign(__tuple_like_tag_t{}, std::forward<_UTuple>(__u));
1814 return *this;
1815 }
1816
1817 template<__tuple_like _UTuple>
1818 requires (!__is_tuple_v<_UTuple>)
1819 friend constexpr bool
1820 operator== [[nodiscard]] (const tuple& __t, const _UTuple& __u)
1821 {
1822 static_assert(sizeof...(_Elements) == tuple_size_v<_UTuple>,
1823 "tuple objects can only be compared if they have equal sizes.");
1824 return [&]<size_t... _Is>(index_sequence<_Is...>) {
1825 return (bool(std::get<_Is>(__t) == std::get<_Is>(__u))
1826 && ...);
1827 }(index_sequence_for<_Elements...>{});
1828 }
1829
1830 template<__tuple_like _UTuple,
1832 struct __tuple_like_common_comparison_category;
1833
1834 template<__tuple_like _UTuple, size_t... _Is>
1835 requires requires
1836 { typename void_t<__detail::__synth3way_t<_Elements, tuple_element_t<_Is, _UTuple>>...>; }
1837 struct __tuple_like_common_comparison_category<_UTuple, index_sequence<_Is...>>
1838 {
1839 using type = common_comparison_category_t
1840 <__detail::__synth3way_t<_Elements, tuple_element_t<_Is, _UTuple>>...>;
1841 };
1842
1843 template<__tuple_like _UTuple>
1844 requires (!__is_tuple_v<_UTuple>)
1845 friend constexpr typename __tuple_like_common_comparison_category<_UTuple>::type
1846 operator<=>(const tuple& __t, const _UTuple& __u)
1847 {
1848 using _Cat = typename __tuple_like_common_comparison_category<_UTuple>::type;
1849 return std::__tuple_cmp<_Cat>(__t, __u, index_sequence_for<_Elements...>());
1850 }
1851#endif // C++23
1852
1853#else // ! (concepts && consteval)
1854
1855 private:
1856 template<typename... _UElements>
1857 static constexpr
1858 __enable_if_t<sizeof...(_UElements) == sizeof...(_Elements), bool>
1859 __assignable()
1860 { return __and_<is_assignable<_Elements&, _UElements>...>::value; }
1861
1862 // Condition for noexcept-specifier of an assignment operator.
1863 template<typename... _UElements>
1864 static constexpr bool __nothrow_assignable()
1865 {
1866 return
1867 __and_<is_nothrow_assignable<_Elements&, _UElements>...>::value;
1868 }
1869
1870 public:
1871
1872 _GLIBCXX20_CONSTEXPR
1873 tuple&
1874 operator=(__conditional_t<__assignable<const _Elements&...>(),
1875 const tuple&,
1876 const __nonesuch&> __in)
1877 noexcept(__nothrow_assignable<const _Elements&...>())
1878 {
1879 this->_M_assign(__in);
1880 return *this;
1881 }
1882
1883 _GLIBCXX20_CONSTEXPR
1884 tuple&
1885 operator=(__conditional_t<__assignable<_Elements...>(),
1886 tuple&&,
1887 __nonesuch&&> __in)
1888 noexcept(__nothrow_assignable<_Elements...>())
1889 {
1890 this->_M_assign(std::move(__in));
1891 return *this;
1892 }
1893
1894 template<typename... _UElements>
1895 _GLIBCXX20_CONSTEXPR
1896 __enable_if_t<__assignable<const _UElements&...>(), tuple&>
1897 operator=(const tuple<_UElements...>& __in)
1898 noexcept(__nothrow_assignable<const _UElements&...>())
1899 {
1900 this->_M_assign(__in);
1901 return *this;
1902 }
1903
1904 template<typename... _UElements>
1905 _GLIBCXX20_CONSTEXPR
1906 __enable_if_t<__assignable<_UElements...>(), tuple&>
1907 operator=(tuple<_UElements...>&& __in)
1908 noexcept(__nothrow_assignable<_UElements...>())
1909 {
1910 this->_M_assign(std::move(__in));
1911 return *this;
1912 }
1913#endif // concepts && consteval
1914
1915 // tuple swap
1916 _GLIBCXX20_CONSTEXPR
1917 void
1918 swap(tuple& __in)
1919 noexcept(__and_<__is_nothrow_swappable<_Elements>...>::value)
1920 { _Inherited::_M_swap(__in); }
1921
1922#if __cpp_lib_ranges_zip // >= C++23
1923 // As an extension, we constrain the const swap member function in order
1924 // to continue accepting explicit instantiation of tuples whose elements
1925 // are not all const swappable. Without this constraint, such an
1926 // explicit instantiation would also instantiate the ill-formed body of
1927 // this function and yield a hard error. This constraint shouldn't
1928 // affect the behavior of valid programs.
1929 constexpr void
1930 swap(const tuple& __in) const
1931 noexcept(__and_v<__is_nothrow_swappable<const _Elements>...>)
1932 requires (is_swappable_v<const _Elements> && ...)
1933 { _Inherited::_M_swap(__in); }
1934#endif // C++23
1935 };
1936
1937#if __cpp_deduction_guides >= 201606
1938 template<typename... _UTypes>
1939 tuple(_UTypes...) -> tuple<_UTypes...>;
1940 template<typename _T1, typename _T2>
1942 template<typename _Alloc, typename... _UTypes>
1943 tuple(allocator_arg_t, _Alloc, _UTypes...) -> tuple<_UTypes...>;
1944 template<typename _Alloc, typename _T1, typename _T2>
1945 tuple(allocator_arg_t, _Alloc, pair<_T1, _T2>) -> tuple<_T1, _T2>;
1946 template<typename _Alloc, typename... _UTypes>
1947 tuple(allocator_arg_t, _Alloc, tuple<_UTypes...>) -> tuple<_UTypes...>;
1948#endif
1949
1950#pragma GCC diagnostic push
1951#pragma GCC diagnostic ignored "-Winvalid-specialization"
1952 // Explicit specialization, zero-element tuple.
1953 template<>
1954 class _GLIBCXX_STRUCTURAL tuple<>
1955 {
1956 public:
1957 // We need the default since we're going to define no-op
1958 // allocator constructors.
1959 tuple() = default;
1960 // Defaulted copy operations to maintain trivial copyability.
1961 // and support non-const assignment expressions.
1962 tuple(const tuple&) = default;
1963 tuple& operator=(const tuple&) = default;
1964
1965 _GLIBCXX20_CONSTEXPR
1966 void swap(tuple&) noexcept { /* no-op */ }
1967
1968#if __cpp_lib_ranges_zip // >= C++23
1969 template<same_as<tuple> _Tuple = tuple>
1970 constexpr const tuple&
1971 operator=(const _Tuple&) const noexcept
1972 { return *this; }
1973
1974 constexpr void swap(const tuple&) const noexcept
1975 { /* no-op */ }
1976#endif
1977
1978 // No-op allocator constructors.
1979 template<typename _Alloc>
1980 _GLIBCXX20_CONSTEXPR
1981 tuple(allocator_arg_t, const _Alloc&) noexcept { }
1982 template<typename _Alloc>
1983 _GLIBCXX20_CONSTEXPR
1984 tuple(allocator_arg_t, const _Alloc&, const tuple&) noexcept { }
1985
1986#if __cpp_lib_tuple_like // >= C++23
1987 template<__tuple_like _UTuple>
1988 requires (!is_same_v<remove_cvref_t<_UTuple>, tuple>)
1989 && (!is_same_v<remove_cvref_t<_UTuple>, allocator_arg_t>)
1990 && (tuple_size_v<remove_cvref_t<_UTuple>> == 0)
1991 constexpr
1992 tuple(_UTuple&&) noexcept { }
1993
1994 template<typename _Alloc, __tuple_like _UTuple>
1995 requires (!is_same_v<remove_cvref_t<_UTuple>, tuple>)
1996 && (tuple_size_v<remove_cvref_t<_UTuple>> == 0)
1997 constexpr
1998 tuple(allocator_arg_t, const _Alloc&, _UTuple&&) noexcept { }
1999
2000 template<__tuple_like _UTuple>
2001 requires (!is_same_v<remove_cvref_t<_UTuple>, tuple>)
2002 && (tuple_size_v<remove_cvref_t<_UTuple>> == 0)
2003 constexpr tuple&
2004 operator=(_UTuple&&) noexcept
2005 { return *this; }
2006
2007 template<__tuple_like _UTuple>
2008 requires (!is_same_v<remove_cvref_t<_UTuple>, tuple>)
2009 && (tuple_size_v<remove_cvref_t<_UTuple>> == 0)
2010 constexpr const tuple&
2011 operator=(_UTuple&&) const noexcept
2012 { return *this; }
2013
2014 template<__tuple_like _UTuple>
2015 requires (!__is_tuple_v<_UTuple>) && (tuple_size_v<_UTuple> == 0)
2016 [[nodiscard]]
2017 friend constexpr bool
2018 operator==(const tuple&, const _UTuple&) noexcept
2019 { return true; }
2020
2021 template<__tuple_like _UTuple>
2022 requires (!__is_tuple_v<_UTuple>) && (tuple_size_v<_UTuple> == 0)
2023 friend constexpr strong_ordering
2024 operator<=>(const tuple&, const _UTuple&) noexcept
2025 { return strong_ordering::equal; }
2026#endif // C++23
2027 };
2028
2029#if !(__cpp_concepts && __cpp_consteval && __cpp_conditional_explicit) // !C++20
2030 /// Partial specialization, 2-element tuple.
2031 /// Includes construction and assignment from a pair.
2032 template<typename _T1, typename _T2>
2033 class _GLIBCXX_STRUCTURAL tuple<_T1, _T2>
2034 : public _Tuple_impl<0, _T1, _T2>
2035 {
2036 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
2037
2038 // Constraint for non-explicit default constructor
2039 template<bool _Dummy, typename _U1, typename _U2>
2040 using _ImplicitDefaultCtor = __enable_if_t<
2041 _TupleConstraints<_Dummy, _U1, _U2>::
2042 __is_implicitly_default_constructible(),
2043 bool>;
2044
2045 // Constraint for explicit default constructor
2046 template<bool _Dummy, typename _U1, typename _U2>
2047 using _ExplicitDefaultCtor = __enable_if_t<
2048 _TupleConstraints<_Dummy, _U1, _U2>::
2049 __is_explicitly_default_constructible(),
2050 bool>;
2051
2052 template<bool _Dummy>
2053 using _TCC = _TupleConstraints<_Dummy, _T1, _T2>;
2054
2055 // Constraint for non-explicit constructors
2056 template<bool _Cond, typename _U1, typename _U2>
2057 using _ImplicitCtor = __enable_if_t<
2058 _TCC<_Cond>::template __is_implicitly_constructible<_U1, _U2>(),
2059 bool>;
2060
2061 // Constraint for non-explicit constructors
2062 template<bool _Cond, typename _U1, typename _U2>
2063 using _ExplicitCtor = __enable_if_t<
2064 _TCC<_Cond>::template __is_explicitly_constructible<_U1, _U2>(),
2065 bool>;
2066
2067 template<typename _U1, typename _U2>
2068 static constexpr bool __assignable()
2069 {
2070 return __and_<is_assignable<_T1&, _U1>,
2072 }
2073
2074 template<typename _U1, typename _U2>
2075 static constexpr bool __nothrow_assignable()
2076 {
2077 return __and_<is_nothrow_assignable<_T1&, _U1>,
2079 }
2080
2081 template<typename _U1, typename _U2>
2082 static constexpr bool __nothrow_constructible()
2083 {
2084 return __and_<is_nothrow_constructible<_T1, _U1>,
2086 }
2087
2088 static constexpr bool __nothrow_default_constructible()
2089 {
2090 return __and_<is_nothrow_default_constructible<_T1>,
2092 }
2093
2094 template<typename _U1>
2095 static constexpr bool __is_alloc_arg()
2096 { return is_same<__remove_cvref_t<_U1>, allocator_arg_t>::value; }
2097
2098 /// @cond undocumented
2099#undef __glibcxx_no_dangling_refs
2100 // Error if construction from _U1 and _U2 would create a dangling ref.
2101#if __has_builtin(__reference_constructs_from_temporary)
2102# define __glibcxx_no_dangling_refs(_U1, _U2) \
2103 static_assert(!__reference_constructs_from_temporary(_T1, _U1) \
2104 && !__reference_constructs_from_temporary(_T2, _U2), \
2105 "std::tuple constructor creates a dangling reference")
2106#else
2107# define __glibcxx_no_dangling_refs(_U1, _U2)
2108#endif
2109 /// @endcond
2110
2111 public:
2112 template<bool _Dummy = true,
2113 _ImplicitDefaultCtor<_Dummy, _T1, _T2> = true>
2114 constexpr
2115 tuple()
2116 noexcept(__nothrow_default_constructible())
2117 : _Inherited() { }
2118
2119 template<bool _Dummy = true,
2120 _ExplicitDefaultCtor<_Dummy, _T1, _T2> = false>
2121 explicit constexpr
2122 tuple()
2123 noexcept(__nothrow_default_constructible())
2124 : _Inherited() { }
2125
2126 template<bool _Dummy = true,
2127 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
2128 constexpr
2129 tuple(const _T1& __a1, const _T2& __a2)
2130 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
2131 : _Inherited(__a1, __a2) { }
2132
2133 template<bool _Dummy = true,
2134 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
2135 explicit constexpr
2136 tuple(const _T1& __a1, const _T2& __a2)
2137 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
2138 : _Inherited(__a1, __a2) { }
2139
2140 template<typename _U1, typename _U2,
2141 _ImplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = true>
2142 constexpr
2143 tuple(_U1&& __a1, _U2&& __a2)
2144 noexcept(__nothrow_constructible<_U1, _U2>())
2145 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2))
2146 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2147
2148 template<typename _U1, typename _U2,
2149 _ExplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = false>
2150 explicit constexpr
2151 tuple(_U1&& __a1, _U2&& __a2)
2152 noexcept(__nothrow_constructible<_U1, _U2>())
2153 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2))
2154 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2155
2156 constexpr tuple(const tuple&) = default;
2157
2158 constexpr tuple(tuple&&) = default;
2159
2160 template<typename _U1, typename _U2,
2161 _ImplicitCtor<true, const _U1&, const _U2&> = true>
2162 constexpr
2163 tuple(const tuple<_U1, _U2>& __in)
2164 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2165 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2166 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2167
2168 template<typename _U1, typename _U2,
2169 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2170 explicit constexpr
2171 tuple(const tuple<_U1, _U2>& __in)
2172 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2173 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2174 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2175
2176 template<typename _U1, typename _U2,
2177 _ImplicitCtor<true, _U1, _U2> = true>
2178 constexpr
2179 tuple(tuple<_U1, _U2>&& __in)
2180 noexcept(__nothrow_constructible<_U1, _U2>())
2181 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2182 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2183
2184 template<typename _U1, typename _U2,
2185 _ExplicitCtor<true, _U1, _U2> = false>
2186 explicit constexpr
2187 tuple(tuple<_U1, _U2>&& __in)
2188 noexcept(__nothrow_constructible<_U1, _U2>())
2189 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2190 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2191
2192 template<typename _U1, typename _U2,
2193 _ImplicitCtor<true, const _U1&, const _U2&> = true>
2194 constexpr
2195 tuple(const pair<_U1, _U2>& __in)
2196 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2197 : _Inherited(__in.first, __in.second)
2198 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2199
2200 template<typename _U1, typename _U2,
2201 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2202 explicit constexpr
2203 tuple(const pair<_U1, _U2>& __in)
2204 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
2205 : _Inherited(__in.first, __in.second)
2206 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2207
2208 template<typename _U1, typename _U2,
2209 _ImplicitCtor<true, _U1, _U2> = true>
2210 constexpr
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&&); }
2216
2217 template<typename _U1, typename _U2,
2218 _ExplicitCtor<true, _U1, _U2> = false>
2219 explicit constexpr
2220 tuple(pair<_U1, _U2>&& __in)
2221 noexcept(__nothrow_constructible<_U1, _U2>())
2222 : _Inherited(std::forward<_U1>(__in.first),
2223 std::forward<_U2>(__in.second))
2224 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2225
2226 // Allocator-extended constructors.
2227
2228 template<typename _Alloc,
2229 _ImplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = true>
2230 _GLIBCXX20_CONSTEXPR
2231 tuple(allocator_arg_t __tag, const _Alloc& __a)
2232 : _Inherited(__tag, __a) { }
2233
2234 template<typename _Alloc,
2235 _ExplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = false>
2236 _GLIBCXX20_CONSTEXPR
2237 explicit
2238 tuple(allocator_arg_t __tag, const _Alloc& __a)
2239 : _Inherited(__tag, __a) { }
2240
2241 template<typename _Alloc, bool _Dummy = true,
2242 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
2243 _GLIBCXX20_CONSTEXPR
2244 tuple(allocator_arg_t __tag, const _Alloc& __a,
2245 const _T1& __a1, const _T2& __a2)
2246 : _Inherited(__tag, __a, __a1, __a2) { }
2247
2248 template<typename _Alloc, bool _Dummy = true,
2249 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
2250 explicit
2251 _GLIBCXX20_CONSTEXPR
2252 tuple(allocator_arg_t __tag, const _Alloc& __a,
2253 const _T1& __a1, const _T2& __a2)
2254 : _Inherited(__tag, __a, __a1, __a2) { }
2255
2256 template<typename _Alloc, typename _U1, typename _U2,
2257 _ImplicitCtor<true, _U1, _U2> = true>
2258 _GLIBCXX20_CONSTEXPR
2259 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
2260 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
2261 std::forward<_U2>(__a2))
2262 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2263
2264 template<typename _Alloc, typename _U1, typename _U2,
2265 _ExplicitCtor<true, _U1, _U2> = false>
2266 explicit
2267 _GLIBCXX20_CONSTEXPR
2268 tuple(allocator_arg_t __tag, const _Alloc& __a,
2269 _U1&& __a1, _U2&& __a2)
2270 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
2271 std::forward<_U2>(__a2))
2272 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2273
2274 template<typename _Alloc>
2275 _GLIBCXX20_CONSTEXPR
2276 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
2277 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
2278
2279 template<typename _Alloc>
2280 _GLIBCXX20_CONSTEXPR
2281 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
2282 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
2283
2284 template<typename _Alloc, typename _U1, typename _U2,
2285 _ImplicitCtor<true, const _U1&, const _U2&> = true>
2286 _GLIBCXX20_CONSTEXPR
2287 tuple(allocator_arg_t __tag, const _Alloc& __a,
2288 const tuple<_U1, _U2>& __in)
2289 : _Inherited(__tag, __a,
2290 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2291 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2292
2293 template<typename _Alloc, typename _U1, typename _U2,
2294 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2295 explicit
2296 _GLIBCXX20_CONSTEXPR
2297 tuple(allocator_arg_t __tag, const _Alloc& __a,
2298 const tuple<_U1, _U2>& __in)
2299 : _Inherited(__tag, __a,
2300 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
2301 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2302
2303 template<typename _Alloc, typename _U1, typename _U2,
2304 _ImplicitCtor<true, _U1, _U2> = true>
2305 _GLIBCXX20_CONSTEXPR
2306 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
2307 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2308 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2309
2310 template<typename _Alloc, typename _U1, typename _U2,
2311 _ExplicitCtor<true, _U1, _U2> = false>
2312 explicit
2313 _GLIBCXX20_CONSTEXPR
2314 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
2315 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
2316 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2317
2318 template<typename _Alloc, typename _U1, typename _U2,
2319 _ImplicitCtor<true, const _U1&, const _U2&> = true>
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&); }
2325
2326 template<typename _Alloc, typename _U1, typename _U2,
2327 _ExplicitCtor<true, const _U1&, const _U2&> = false>
2328 explicit
2329 _GLIBCXX20_CONSTEXPR
2330 tuple(allocator_arg_t __tag, const _Alloc& __a,
2331 const pair<_U1, _U2>& __in)
2332 : _Inherited(__tag, __a, __in.first, __in.second)
2333 { __glibcxx_no_dangling_refs(const _U1&, const _U2&); }
2334
2335 template<typename _Alloc, typename _U1, typename _U2,
2336 _ImplicitCtor<true, _U1, _U2> = true>
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&&); }
2342
2343 template<typename _Alloc, typename _U1, typename _U2,
2344 _ExplicitCtor<true, _U1, _U2> = false>
2345 explicit
2346 _GLIBCXX20_CONSTEXPR
2347 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
2348 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
2349 std::forward<_U2>(__in.second))
2350 { __glibcxx_no_dangling_refs(_U1&&, _U2&&); }
2351
2352 // Tuple assignment.
2353
2354 _GLIBCXX20_CONSTEXPR
2355 tuple&
2356 operator=(__conditional_t<__assignable<const _T1&, const _T2&>(),
2357 const tuple&,
2358 const __nonesuch&> __in)
2359 noexcept(__nothrow_assignable<const _T1&, const _T2&>())
2360 {
2361 this->_M_assign(__in);
2362 return *this;
2363 }
2364
2365 _GLIBCXX20_CONSTEXPR
2366 tuple&
2367 operator=(__conditional_t<__assignable<_T1, _T2>(),
2368 tuple&&,
2369 __nonesuch&&> __in)
2370 noexcept(__nothrow_assignable<_T1, _T2>())
2371 {
2372 this->_M_assign(std::move(__in));
2373 return *this;
2374 }
2375
2376 template<typename _U1, typename _U2>
2377 _GLIBCXX20_CONSTEXPR
2378 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
2379 operator=(const tuple<_U1, _U2>& __in)
2380 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
2381 {
2382 this->_M_assign(__in);
2383 return *this;
2384 }
2385
2386 template<typename _U1, typename _U2>
2387 _GLIBCXX20_CONSTEXPR
2388 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
2389 operator=(tuple<_U1, _U2>&& __in)
2390 noexcept(__nothrow_assignable<_U1, _U2>())
2391 {
2392 this->_M_assign(std::move(__in));
2393 return *this;
2394 }
2395
2396 template<typename _U1, typename _U2>
2397 _GLIBCXX20_CONSTEXPR
2398 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
2399 operator=(const pair<_U1, _U2>& __in)
2400 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
2401 {
2402 this->_M_head(*this) = __in.first;
2403 this->_M_tail(*this)._M_head(*this) = __in.second;
2404 return *this;
2405 }
2406
2407 template<typename _U1, typename _U2>
2408 _GLIBCXX20_CONSTEXPR
2409 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
2410 operator=(pair<_U1, _U2>&& __in)
2411 noexcept(__nothrow_assignable<_U1, _U2>())
2412 {
2413 this->_M_head(*this) = std::forward<_U1>(__in.first);
2414 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
2415 return *this;
2416 }
2417
2418 _GLIBCXX20_CONSTEXPR
2419 void
2420 swap(tuple& __in)
2421 noexcept(__and_<__is_nothrow_swappable<_T1>,
2422 __is_nothrow_swappable<_T2>>::value)
2423 { _Inherited::_M_swap(__in); }
2424 };
2425#endif // concepts && conditional_explicit
2426#pragma GCC diagnostic pop
2427
2428 /// class tuple_size
2429 template<typename... _Elements>
2430 struct tuple_size<tuple<_Elements...>>
2431 : public integral_constant<size_t, sizeof...(_Elements)> { };
2432
2433#if __cplusplus >= 201703L
2434 template<typename... _Types>
2435 inline constexpr size_t tuple_size_v<tuple<_Types...>>
2436 = sizeof...(_Types);
2437
2438 template<typename... _Types>
2439 inline constexpr size_t tuple_size_v<const tuple<_Types...>>
2440 = sizeof...(_Types);
2441#endif
2442
2443 /// Trait to get the Ith element type from a tuple.
2444 template<size_t __i, typename... _Types>
2445 struct tuple_element<__i, tuple<_Types...>>
2446 {
2447 static_assert(__i < sizeof...(_Types), "tuple index must be in range");
2448
2449 using type = typename _Nth_type<__i, _Types...>::type;
2450 };
2451
2452 template<size_t __i, typename _Head, typename... _Tail>
2453 constexpr _Head&
2454 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
2455 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
2456
2457 template<size_t __i, typename _Head, typename... _Tail>
2458 constexpr const _Head&
2459 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
2460 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
2461
2462 // Deleted overload to improve diagnostics for invalid indices
2463 template<size_t __i, typename... _Types>
2464 __enable_if_t<(__i >= sizeof...(_Types))>
2465 __get_helper(const tuple<_Types...>&) = delete;
2466
2467 // Declared in bits/stl_pair.h
2468 /// Return a reference to the ith element of a tuple.
2469 template<size_t __i, typename... _Elements>
2470 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
2471 ::std::get(tuple<_Elements...>& __t) noexcept
2472 { return std::__get_helper<__i>(__t); }
2473
2474 /// Return a const reference to the ith element of a const tuple.
2475 template<size_t __i, typename... _Elements>
2476 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
2477 ::std::get(const tuple<_Elements...>& __t) noexcept
2478 { return std::__get_helper<__i>(__t); }
2479
2480 /// Return an rvalue reference to the ith element of a tuple rvalue.
2481 template<size_t __i, typename... _Elements>
2482 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
2483 ::std::get(tuple<_Elements...>&& __t) noexcept
2484 {
2485 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
2486 return std::forward<__element_type>(std::__get_helper<__i>(__t));
2487 }
2488
2489 /// Return a const rvalue reference to the ith element of a const tuple rvalue.
2490 template<size_t __i, typename... _Elements>
2491 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&&
2492 ::std::get(const tuple<_Elements...>&& __t) noexcept
2493 {
2494 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
2495 return std::forward<const __element_type>(std::__get_helper<__i>(__t));
2496 }
2497
2498#ifdef __cpp_lib_tuples_by_type // C++ >= 14
2499 /// Return a reference to the unique element of type _Tp of a tuple.
2500 template <typename _Tp, typename... _Types>
2501 constexpr _Tp&
2502 get(tuple<_Types...>& __t) noexcept
2503 {
2504 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2505 static_assert(__idx < sizeof...(_Types),
2506 "the type T in std::get<T> must occur exactly once in the tuple");
2507 return std::__get_helper<__idx>(__t);
2508 }
2509
2510 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
2511 template <typename _Tp, typename... _Types>
2512 constexpr _Tp&&
2513 get(tuple<_Types...>&& __t) noexcept
2514 {
2515 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2516 static_assert(__idx < sizeof...(_Types),
2517 "the type T in std::get<T> must occur exactly once in the tuple");
2518 return std::forward<_Tp>(std::__get_helper<__idx>(__t));
2519 }
2520
2521 /// Return a const reference to the unique element of type _Tp of a tuple.
2522 template <typename _Tp, typename... _Types>
2523 constexpr const _Tp&
2524 get(const tuple<_Types...>& __t) noexcept
2525 {
2526 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2527 static_assert(__idx < sizeof...(_Types),
2528 "the type T in std::get<T> must occur exactly once in the tuple");
2529 return std::__get_helper<__idx>(__t);
2530 }
2531
2532 /// Return a const reference to the unique element of type _Tp of
2533 /// a const tuple rvalue.
2534 template <typename _Tp, typename... _Types>
2535 constexpr const _Tp&&
2536 get(const tuple<_Types...>&& __t) noexcept
2537 {
2538 constexpr size_t __idx = __find_uniq_type_in_pack<_Tp, _Types...>();
2539 static_assert(__idx < sizeof...(_Types),
2540 "the type T in std::get<T> must occur exactly once in the tuple");
2541 return std::forward<const _Tp>(std::__get_helper<__idx>(__t));
2542 }
2543#endif
2544
2545#if __cpp_lib_three_way_comparison
2546 template<typename... _Tps, typename... _Ups>
2547 requires (sizeof...(_Tps) == sizeof...(_Ups))
2548 && (requires (const _Tps& __t, const _Ups& __u) {
2549 { __t == __u } -> __detail::__boolean_testable;
2550 } && ...)
2551 constexpr bool
2552 operator== [[nodiscard]] (const tuple<_Tps...>& __t,
2553 const tuple<_Ups...>& __u)
2554 {
2555 return [&]<size_t... _Inds>(index_sequence<_Inds...>) {
2556 // Fold == over the tuples until non-equal elements are found.
2557 return (bool(std::get<_Inds>(__t) == std::get<_Inds>(__u)) && ...);
2558 }(index_sequence_for<_Tps...>{});
2559 }
2560
2561 template<typename _Cat, typename _Tp, typename _Up, typename _IndexSeq>
2562 [[nodiscard]]
2563 constexpr _Cat
2564 __tuple_cmp(const _Tp& __t, const _Up& __u, _IndexSeq __indices)
2565 {
2566 _Cat __c = _Cat::equivalent;
2567
2568 // Set __c to the comparison result of two corresponding elements.
2569 // Return true they are equivalent.
2570 auto __cmp = [&]<size_t _Ind>(integral_constant<size_t, _Ind>) {
2571 __c = __detail::__synth3way(std::get<_Ind>(__t), std::get<_Ind>(__u));
2572 return __c == 0;
2573 };
2574
2575 [&]<size_t... _Inds>(index_sequence<_Inds...>) {
2576 // Fold __cmp over the tuples until non-equivalent elements are found.
2577 (void)(__cmp(integral_constant<size_t, _Inds>{}) && ...);
2578 }(__indices);
2579
2580 return __c;
2581 }
2582
2583 template<typename... _Tps, typename... _Ups>
2584 requires (sizeof...(_Tps) == sizeof...(_Ups))
2585 && (requires { typename __detail::__synth3way_t<_Tps, _Ups>; } && ...)
2586 constexpr
2587 common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>
2588 operator<=> [[nodiscard]] (const tuple<_Tps...>& __t,
2589 const tuple<_Ups...>& __u)
2590 {
2591 using _Cat
2592 = common_comparison_category_t<__detail::__synth3way_t<_Tps, _Ups>...>;
2593 return std::__tuple_cmp<_Cat>(__t, __u, index_sequence_for<_Tps...>());
2594 }
2595#else
2596
2597 // This class performs the comparison operations on tuples
2598 template<typename _Tp, typename _Up, size_t __i, size_t __size>
2599 struct __tuple_compare
2600 {
2601 static constexpr bool
2602 __eq(const _Tp& __t, const _Up& __u)
2603 {
2604 return bool(std::get<__i>(__t) == std::get<__i>(__u))
2605 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
2606 }
2607
2608 static constexpr bool
2609 __less(const _Tp& __t, const _Up& __u)
2610 {
2611 return bool(std::get<__i>(__t) < std::get<__i>(__u))
2612 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
2613 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
2614 }
2615 };
2616
2617 template<typename _Tp, typename _Up, size_t __size>
2618 struct __tuple_compare<_Tp, _Up, __size, __size>
2619 {
2620 static constexpr bool
2621 __eq(const _Tp&, const _Up&) { return true; }
2622
2623 static constexpr bool
2624 __less(const _Tp&, const _Up&) { return false; }
2625 };
2626
2627 template<typename... _TElements, typename... _UElements>
2628 _GLIBCXX_NODISCARD
2629 constexpr bool
2630 operator==(const tuple<_TElements...>& __t,
2631 const tuple<_UElements...>& __u)
2632 {
2633 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
2634 "tuple objects can only be compared if they have equal sizes.");
2635 using __compare = __tuple_compare<tuple<_TElements...>,
2636 tuple<_UElements...>,
2637 0, sizeof...(_TElements)>;
2638 return __compare::__eq(__t, __u);
2639 }
2640
2641 template<typename... _TElements, typename... _UElements>
2642 _GLIBCXX_NODISCARD
2643 constexpr bool
2644 operator<(const tuple<_TElements...>& __t,
2645 const tuple<_UElements...>& __u)
2646 {
2647 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
2648 "tuple objects can only be compared if they have equal sizes.");
2649 using __compare = __tuple_compare<tuple<_TElements...>,
2650 tuple<_UElements...>,
2651 0, sizeof...(_TElements)>;
2652 return __compare::__less(__t, __u);
2653 }
2654
2655 template<typename... _TElements, typename... _UElements>
2656 _GLIBCXX_NODISCARD
2657 constexpr bool
2658 operator!=(const tuple<_TElements...>& __t,
2659 const tuple<_UElements...>& __u)
2660 { return !(__t == __u); }
2661
2662 template<typename... _TElements, typename... _UElements>
2663 _GLIBCXX_NODISCARD
2664 constexpr bool
2665 operator>(const tuple<_TElements...>& __t,
2666 const tuple<_UElements...>& __u)
2667 { return __u < __t; }
2668
2669 template<typename... _TElements, typename... _UElements>
2670 _GLIBCXX_NODISCARD
2671 constexpr bool
2672 operator<=(const tuple<_TElements...>& __t,
2673 const tuple<_UElements...>& __u)
2674 { return !(__u < __t); }
2675
2676 template<typename... _TElements, typename... _UElements>
2677 _GLIBCXX_NODISCARD
2678 constexpr bool
2679 operator>=(const tuple<_TElements...>& __t,
2680 const tuple<_UElements...>& __u)
2681 { return !(__t < __u); }
2682#endif // three_way_comparison
2683
2684 // NB: DR 705.
2685 /// Create a tuple containing copies of the arguments
2686 template<typename... _Elements>
2688 make_tuple(_Elements&&... __args)
2689 {
2691 __result_type;
2692 return __result_type(std::forward<_Elements>(__args)...);
2693 }
2694
2695 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2696 // 2275. Why is forward_as_tuple not constexpr?
2697 /// Create a tuple of lvalue or rvalue references to the arguments
2698 template<typename... _Elements>
2699 constexpr tuple<_Elements&&...>
2700 forward_as_tuple(_Elements&&... __args) noexcept
2701 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
2702
2703 /// @cond undocumented
2704 template<typename _Tuple, typename _Idx_tuple>
2705 struct __do_make_tuple;
2706
2707 template<typename _Tuple, size_t... _Idx>
2708 struct __do_make_tuple<_Tuple, _Index_tuple<_Idx...>>
2709 {
2710 using __type = tuple<__tuple_element_t<_Idx, _Tuple>...>;
2711 };
2712
2713 // Returns the std::tuple equivalent of a tuple-like type.
2714 template<typename _Tuple,
2715 typename _Tup = __remove_cvref_t<_Tuple>,
2716 typename _Indices = _Build_index_tuple<tuple_size<_Tup>::value>>
2717 struct __make_tuple
2718 : __do_make_tuple<_Tup, typename _Indices::__type>
2719 { };
2720
2721 // Combines several std::tuple types into a single one.
2722 template<typename...>
2723 struct __combine_tuples;
2724
2725 template<>
2726 struct __combine_tuples<>
2727 {
2728 using __type = tuple<>;
2729 };
2730
2731 template<typename... _Ts>
2732 struct __combine_tuples<tuple<_Ts...>>
2733 {
2734 using __type = tuple<_Ts...>;
2735 };
2736
2737 template<typename... _T1s, typename... _T2s>
2738 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>>
2739 {
2740 using __type = tuple<_T1s..., _T2s...>;
2741 };
2742
2743 template<typename... _T1s, typename... _T2s, typename... _T3s,
2744 typename... _Rem>
2745 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, tuple<_T3s...>,
2746 _Rem...>
2747 {
2748 using _First = tuple<_T1s..., _T2s..., _T3s...>;
2749 using _Second = typename __combine_tuples<_Rem...>::__type;
2750 using __type = typename __combine_tuples<_First, _Second>::__type;
2751 };
2752
2753 // Computes the result type of tuple_cat given a set of tuple-like types.
2754 template<typename... _Tpls>
2755 struct __tuple_cat_result
2756 {
2757 typedef typename __combine_tuples
2758 <typename __make_tuple<_Tpls>::__type...>::__type __type;
2759 };
2760
2761 // Helper to determine the index set for the first tuple-like
2762 // type of a given set.
2763 template<typename...>
2764 struct __make_1st_indices;
2765
2766 template<>
2767 struct __make_1st_indices<>
2768 {
2769 typedef _Index_tuple<> __type;
2770 };
2771
2772 template<typename _Tp, typename... _Tpls>
2773 struct __make_1st_indices<_Tp, _Tpls...>
2774 {
2775 typedef typename _Build_index_tuple<tuple_size<
2776 typename remove_reference<_Tp>::type>::value>::__type __type;
2777 };
2778
2779 // Performs the actual concatenation by step-wise expanding tuple-like
2780 // objects into the elements, which are finally forwarded into the
2781 // result tuple.
2782 template<typename _Ret, typename _Indices, typename... _Tpls>
2783 struct __tuple_concater;
2784
2785 template<typename _Ret, size_t... _Is, typename _Tp, typename... _Tpls>
2786 struct __tuple_concater<_Ret, _Index_tuple<_Is...>, _Tp, _Tpls...>
2787 {
2788 template<typename... _Us>
2789 static constexpr _Ret
2790 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
2791 {
2792 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
2793 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
2794 return __next::_S_do(std::forward<_Tpls>(__tps)...,
2795 std::forward<_Us>(__us)...,
2796 std::get<_Is>(std::forward<_Tp>(__tp))...);
2797 }
2798 };
2799
2800 template<typename _Ret>
2801 struct __tuple_concater<_Ret, _Index_tuple<>>
2802 {
2803 template<typename... _Us>
2804 static constexpr _Ret
2805 _S_do(_Us&&... __us)
2806 {
2807 return _Ret(std::forward<_Us>(__us)...);
2808 }
2809 };
2810
2811 template<typename... _Tps>
2812 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
2813 { };
2814 /// @endcond
2815
2816 /// Create a `tuple` containing all elements from multiple tuple-like objects
2817#if __cpp_lib_tuple_like // >= C++23
2818 template<__tuple_like... _Tpls>
2819#else
2820 template<typename... _Tpls, typename = typename
2822#endif
2823 constexpr auto
2824 tuple_cat(_Tpls&&... __tpls)
2825 -> typename __tuple_cat_result<_Tpls...>::__type
2826 {
2827 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
2828 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
2829 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
2830 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
2831 }
2832
2833 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2834 // 2301. Why is tie not constexpr?
2835 /// Return a tuple of lvalue references bound to the arguments
2836 template<typename... _Elements>
2837 constexpr tuple<_Elements&...>
2838 tie(_Elements&... __args) noexcept
2839 { return tuple<_Elements&...>(__args...); }
2840
2841 /// Exchange the values of two tuples
2842 template<typename... _Elements>
2843 _GLIBCXX20_CONSTEXPR
2844 inline
2845#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
2846 // Constrained free swap overload, see p0185r1
2847 typename enable_if<__and_<__is_swappable<_Elements>...>::value
2848 >::type
2849#else
2850 void
2851#endif
2853 noexcept(noexcept(__x.swap(__y)))
2854 { __x.swap(__y); }
2855
2856#if __cpp_lib_ranges_zip // >= C++23
2857 /// Exchange the values of two const tuples (if const elements can be swapped)
2858 template<typename... _Elements>
2859 requires (is_swappable_v<const _Elements> && ...)
2860 constexpr void
2861 swap(const tuple<_Elements...>& __x, const tuple<_Elements...>& __y)
2862 noexcept(noexcept(__x.swap(__y)))
2863 { __x.swap(__y); }
2864#endif // C++23
2865
2866#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
2867 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2868 // 2766. Swapping non-swappable types
2869 template<typename... _Elements>
2870 _GLIBCXX20_CONSTEXPR
2871 typename enable_if<!__and_<__is_swappable<_Elements>...>::value>::type
2872 swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
2873#endif
2874
2875 /// Partial specialization for tuples
2876 template<typename... _Types, typename _Alloc>
2877 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
2878
2879 // See stl_pair.h...
2880 /** "piecewise construction" using a tuple of arguments for each member.
2881 *
2882 * @param __first Arguments for the first member of the pair.
2883 * @param __second Arguments for the second member of the pair.
2884 *
2885 * The elements of each tuple will be used as the constructor arguments
2886 * for the data members of the pair.
2887 */
2888 template<class _T1, class _T2>
2889 template<typename... _Args1, typename... _Args2>
2890 _GLIBCXX20_CONSTEXPR
2891 inline
2894 tuple<_Args1...> __first, tuple<_Args2...> __second)
2895 : pair(__first, __second,
2896 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
2897 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
2898 { }
2899
2900 template<class _T1, class _T2>
2901 template<typename... _Args1, size_t... _Indexes1,
2902 typename... _Args2, size_t... _Indexes2>
2903 _GLIBCXX20_CONSTEXPR inline
2905 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
2906 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
2907 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
2908 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
2909 { }
2910
2911#ifdef __cpp_lib_apply // C++ >= 17
2912 template <typename _Fn, typename _Tuple, size_t... _Idx>
2913 constexpr decltype(auto)
2914 __apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
2915 {
2916 return std::__invoke(std::forward<_Fn>(__f),
2917 std::get<_Idx>(std::forward<_Tuple>(__t))...);
2918 }
2919
2920#if __cpp_lib_tuple_like // >= C++23
2921 template <typename _Fn, __tuple_like _Tuple>
2922#else
2923 template <typename _Fn, typename _Tuple>
2924#endif
2925 constexpr auto
2926 apply(_Fn&& __f, _Tuple&& __t)
2927 noexcept(__noexcept_unpack_tuple_like_v<is_nothrow_invocable, _Fn, _Tuple>)
2928#if __glibcxx_apply >= 202506L // >= C++26
2930#else
2931 -> decltype(auto)
2932#endif
2933 {
2934 using _Indices
2936 return std::__apply_impl(std::forward<_Fn>(__f),
2938 _Indices{});
2939 }
2940#endif
2941
2942#ifdef __cpp_lib_make_from_tuple // C++ >= 17
2943 template <typename _Tp, typename _Tuple, size_t... _Idx>
2944 constexpr _Tp
2945 __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
2946 {
2947 static_assert(__unpack_tuple_like_v<is_constructible, _Tp, _Tuple>);
2948 return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...);
2949 }
2950
2951 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2952 // 3528. make_from_tuple can perform (the equivalent of) a C-style cast
2953
2954#if __cpp_lib_tuple_like // >= C++23
2955 template <typename _Tp, __tuple_like _Tuple>
2956#else
2957 template <typename _Tp, typename _Tuple>
2958#endif
2959 constexpr auto
2960 make_from_tuple(_Tuple&& __t)
2961 noexcept(__noexcept_unpack_tuple_like_v<is_nothrow_constructible, _Tp, _Tuple>)
2962#ifdef __cpp_concepts // >= C++20
2963 -> _Tp
2964 requires __unpack_tuple_like_v<is_constructible, _Tp, _Tuple>
2965#else
2966 -> __enable_if_t<__unpack_tuple_like_v<is_constructible, _Tp, _Tuple>, _Tp>
2967#endif
2968 {
2969 constexpr size_t __n = tuple_size_v<remove_reference_t<_Tuple>>;
2970#if __has_builtin(__reference_constructs_from_temporary)
2971 if constexpr (__n == 1)
2972 {
2973 using _Elt = decltype(std::get<0>(std::declval<_Tuple>()));
2974 static_assert(!__reference_constructs_from_temporary(_Tp, _Elt));
2975 }
2976#endif
2977 return __make_from_tuple_impl<_Tp>(std::forward<_Tuple>(__t),
2979 }
2980#endif
2981
2982#if __cpp_lib_tuple_like // >= C++23
2983 template<__tuple_like _TTuple, __tuple_like _UTuple,
2984 template<typename> class _TQual, template<typename> class _UQual,
2986 struct __tuple_like_common_reference;
2987
2988 template<__tuple_like _TTuple, __tuple_like _UTuple,
2989 template<typename> class _TQual, template<typename> class _UQual,
2990 size_t... _Is>
2991 requires requires
2993 _UQual<tuple_element_t<_Is, _UTuple>>>...>; }
2994 struct __tuple_like_common_reference<_TTuple, _UTuple, _TQual, _UQual, index_sequence<_Is...>>
2995 {
2996 using type = tuple<common_reference_t<_TQual<tuple_element_t<_Is, _TTuple>>,
2997 _UQual<tuple_element_t<_Is, _UTuple>>>...>;
2998 };
2999
3000 template<__tuple_like _TTuple, __tuple_like _UTuple,
3001 template<typename> class _TQual, template<typename> class _UQual>
3002 requires (__is_tuple_v<_TTuple> || __is_tuple_v<_UTuple>)
3003 && is_same_v<_TTuple, decay_t<_TTuple>>
3004 && is_same_v<_UTuple, decay_t<_UTuple>>
3005 && (tuple_size_v<_TTuple> == tuple_size_v<_UTuple>)
3006 && requires { typename __tuple_like_common_reference<_TTuple, _UTuple, _TQual, _UQual>::type; }
3007 struct basic_common_reference<_TTuple, _UTuple, _TQual, _UQual>
3008 {
3009 using type = typename __tuple_like_common_reference<_TTuple, _UTuple, _TQual, _UQual>::type;
3010 };
3011
3012 template<__tuple_like _TTuple, __tuple_like _UTuple,
3014 struct __tuple_like_common_type;
3015
3016 template<__tuple_like _TTuple, __tuple_like _UTuple, size_t... _Is>
3017 requires requires
3019 tuple_element_t<_Is, _UTuple>>...>; }
3020 struct __tuple_like_common_type<_TTuple, _UTuple, index_sequence<_Is...>>
3021 {
3022 using type = tuple<common_type_t<tuple_element_t<_Is, _TTuple>,
3023 tuple_element_t<_Is, _UTuple>>...>;
3024 };
3025
3026 template<__tuple_like _TTuple, __tuple_like _UTuple>
3027 requires (__is_tuple_v<_TTuple> || __is_tuple_v<_UTuple>)
3028 && is_same_v<_TTuple, decay_t<_TTuple>>
3029 && is_same_v<_UTuple, decay_t<_UTuple>>
3030 && (tuple_size_v<_TTuple> == tuple_size_v<_UTuple>)
3031 && requires { typename __tuple_like_common_type<_TTuple, _UTuple>::type; }
3032 struct common_type<_TTuple, _UTuple>
3033 {
3034 using type = typename __tuple_like_common_type<_TTuple, _UTuple>::type;
3035 };
3036#endif // C++23
3037
3038 /// @}
3039
3040#undef __glibcxx_no_dangling_refs
3041
3042_GLIBCXX_END_NAMESPACE_VERSION
3043} // namespace std
3044
3045#endif // C++11
3046
3047#endif // _GLIBCXX_TUPLE
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:121
typename apply_result< _Fn, _Tuple >::type apply_result_t
Definition type_traits:4425
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:124
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2967
constexpr auto tuple_cat(_Tpls &&... __tpls) -> typename __tuple_cat_result< _Tpls... >::__type
Create a tuple containing all elements from multiple tuple-like objects.
Definition tuple:2824
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2743
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition tuple:2700
constexpr tuple< typename __decay_and_strip< _Elements >::__type... > make_tuple(_Elements &&... __args)
Create a tuple containing copies of the arguments.
Definition tuple:2688
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr tuple< _Elements &... > tie(_Elements &... __args) noexcept
Return a tuple of lvalue references bound to the arguments.
Definition tuple:2838
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
Definition utility.h:528
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
Definition utility.h:524
make_index_sequence< sizeof...(_Types)> index_sequence_for
Alias template index_sequence_for.
Definition utility.h:532
Primary class template, tuple.
Definition tuple:800
integral_constant
Definition type_traits:98
Define a member typedef type only if a boolean constant is true.
Definition type_traits:139
is_constructible
Definition type_traits:1267
is_nothrow_constructible
Definition type_traits:1345
is_nothrow_default_constructible
Definition type_traits:1354
is_assignable
Definition type_traits:1386
is_nothrow_assignable
Definition type_traits:1420
common_type
Definition type_traits:2602
Declare uses_allocator so it can be specialized in <queue> etc.
Definition memoryfwd.h:76
Struct holding two objects (or references) of arbitrary type.
Definition stl_pair.h:397
constexpr pair(const pair &)=default
Copy constructor.
Tag type for piecewise construction of std::pair objects.
Definition stl_pair.h:79
Finds the size of a given tuple type.
Definition utility.h:55
Gives the type of the ith element of a given tuple type.
Definition utility.h:86