• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_TUPLE
11#define _LIBCPP_TUPLE
12
13// clang-format off
14
15/*
16    tuple synopsis
17
18namespace std
19{
20
21template <class... T>
22class tuple {
23public:
24    explicit(see-below) constexpr tuple();
25    explicit(see-below) tuple(const T&...);  // constexpr in C++14
26    template <class... U>
27        explicit(see-below) tuple(U&&...);  // constexpr in C++14
28    tuple(const tuple&) = default;
29    tuple(tuple&&) = default;
30
31    template<class... UTypes>
32        constexpr explicit(see-below) tuple(tuple<UTypes...>&);  // C++23
33    template <class... U>
34        explicit(see-below) tuple(const tuple<U...>&);  // constexpr in C++14
35    template <class... U>
36        explicit(see-below) tuple(tuple<U...>&&);  // constexpr in C++14
37    template<class... UTypes>
38        constexpr explicit(see-below) tuple(const tuple<UTypes...>&&); // C++23
39
40    template<class U1, class U2>
41        constexpr explicit(see-below) tuple(pair<U1, U2>&);  // iff sizeof...(Types) == 2 // C++23
42    template <class U1, class U2>
43        explicit(see-below) tuple(const pair<U1, U2>&); // iff sizeof...(T) == 2 // constexpr in C++14
44    template <class U1, class U2>
45        explicit(see-below) tuple(pair<U1, U2>&&); // iff sizeof...(T) == 2  // constexpr in C++14
46    template<class U1, class U2>
47        constexpr explicit(see-below) tuple(const pair<U1, U2>&&);  // iff sizeof...(Types) == 2 // C++23
48
49    // allocator-extended constructors
50    template <class Alloc>
51        tuple(allocator_arg_t, const Alloc& a);
52    template <class Alloc>
53        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, const T&...);          // constexpr in C++20
54    template <class Alloc, class... U>
55        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, U&&...);               // constexpr in C++20
56    template <class Alloc>
57        tuple(allocator_arg_t, const Alloc& a, const tuple&);                             // constexpr in C++20
58    template <class Alloc>
59        tuple(allocator_arg_t, const Alloc& a, tuple&&);                                  // constexpr in C++20
60    template<class Alloc, class... UTypes>
61        constexpr explicit(see-below)
62          tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&);                      // C++23
63    template <class Alloc, class... U>
64        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, const tuple<U...>&);   // constexpr in C++20
65    template <class Alloc, class... U>
66        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, tuple<U...>&&);        // constexpr in C++20
67    template<class Alloc, class... UTypes>
68        constexpr explicit(see-below)
69          tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&&);               // C++23
70    template<class Alloc, class U1, class U2>
71        constexpr explicit(see-below)
72          tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&);                          // C++23
73    template <class Alloc, class U1, class U2>
74        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&);  // constexpr in C++20
75    template <class Alloc, class U1, class U2>
76        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&&);       // constexpr in C++20
77    template<class Alloc, class U1, class U2>
78        constexpr explicit(see-below)
79          tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&&);                   // C++23
80
81    tuple& operator=(const tuple&);                                                       // constexpr in C++20
82    constexpr const tuple& operator=(const tuple&) const;                                 // C++23
83    tuple& operator=(tuple&&) noexcept(is_nothrow_move_assignable_v<T> && ...);           // constexpr in C++20
84    constexpr const tuple& operator=(tuple&&) const;                                      // C++23
85    template <class... U>
86        tuple& operator=(const tuple<U...>&);                                             // constexpr in C++20
87    template<class... UTypes>
88        constexpr const tuple& operator=(const tuple<UTypes...>&) const;                  // C++23
89    template <class... U>
90        tuple& operator=(tuple<U...>&&);                                                  // constexpr in C++20
91    template<class... UTypes>
92        constexpr const tuple& operator=(tuple<UTypes...>&&) const;                       // C++23
93    template <class U1, class U2>
94        tuple& operator=(const pair<U1, U2>&); // iff sizeof...(T) == 2                   // constexpr in C++20
95    template<class U1, class U2>
96        constexpr const tuple& operator=(const pair<U1, U2>&) const;   // iff sizeof...(Types) == 2 // C++23
97    template <class U1, class U2>
98        tuple& operator=(pair<U1, U2>&&); // iff sizeof...(T) == 2                        // constexpr in C++20
99    template<class U1, class U2>
100        constexpr const tuple& operator=(pair<U1, U2>&&) const;  // iff sizeof...(Types) == 2 // C++23
101
102    template<class U, size_t N>
103        tuple& operator=(array<U, N> const&) // iff sizeof...(T) == N, EXTENSION
104    template<class U, size_t N>
105        tuple& operator=(array<U, N>&&) // iff sizeof...(T) == N, EXTENSION
106
107    void swap(tuple&) noexcept(AND(swap(declval<T&>(), declval<T&>())...));               // constexpr in C++20
108    constexpr void swap(const tuple&) const noexcept(see-below);                          // C++23
109};
110
111
112template<class... TTypes, class... UTypes, template<class> class TQual, template<class> class UQual> // since C++23
113  requires requires { typename tuple<common_reference_t<TQual<TTypes>, UQual<UTypes>>...>; }
114struct basic_common_reference<tuple<TTypes...>, tuple<UTypes...>, TQual, UQual> {
115  using type = tuple<common_reference_t<TQual<TTypes>, UQual<UTypes>>...>;
116};
117
118template<class... TTypes, class... UTypes>                                // since C++23
119  requires requires { typename tuple<common_type_t<TTypes, UTypes>...>; }
120struct common_type<tuple<TTypes...>, tuple<UTypes...>> {
121  using type = tuple<common_type_t<TTypes, UTypes>...>;
122};
123
124template <class ...T>
125tuple(T...) -> tuple<T...>;                                         // since C++17
126template <class T1, class T2>
127tuple(pair<T1, T2>) -> tuple<T1, T2>;                               // since C++17
128template <class Alloc, class ...T>
129tuple(allocator_arg_t, Alloc, T...) -> tuple<T...>;                 // since C++17
130template <class Alloc, class T1, class T2>
131tuple(allocator_arg_t, Alloc, pair<T1, T2>) -> tuple<T1, T2>;       // since C++17
132template <class Alloc, class ...T>
133tuple(allocator_arg_t, Alloc, tuple<T...>) -> tuple<T...>;          // since C++17
134
135struct ignore-type { // exposition only                             // Since C++26
136  constexpr const ignore-type&
137    operator=(const auto &) const noexcept
138      { return *this; }
139};
140inline constexpr ignore-type ignore;
141
142template <class... T> tuple<V...>  make_tuple(T&&...); // constexpr in C++14
143template <class... T> tuple<ATypes...> forward_as_tuple(T&&...) noexcept; // constexpr in C++14
144template <class... T> tuple<T&...> tie(T&...) noexcept; // constexpr in C++14
145template <class... Tuples> tuple<CTypes...> tuple_cat(Tuples&&... tpls); // constexpr in C++14
146
147// [tuple.apply], calling a function with a tuple of arguments:
148template <class F, class Tuple>
149  constexpr decltype(auto) apply(F&& f, Tuple&& t) noexcept(see below);  // C++17 noexcept since C++23
150template <class T, class Tuple>
151  constexpr T make_from_tuple(Tuple&& t); // C++17
152
153// 20.4.1.4, tuple helper classes:
154template <class T> struct tuple_size; // undefined
155template <class... T> struct tuple_size<tuple<T...>>;
156template <class T>
157 inline constexpr size_t tuple_size_v = tuple_size<T>::value; // C++17
158template <size_t I, class T> struct tuple_element; // undefined
159template <size_t I, class... T> struct tuple_element<I, tuple<T...>>;
160template <size_t I, class T>
161  using tuple_element_t = typename tuple_element <I, T>::type; // C++14
162
163// 20.4.1.5, element access:
164template <size_t I, class... T>
165    typename tuple_element<I, tuple<T...>>::type&
166    get(tuple<T...>&) noexcept; // constexpr in C++14
167template <size_t I, class... T>
168    const typename tuple_element<I, tuple<T...>>::type&
169    get(const tuple<T...>&) noexcept; // constexpr in C++14
170template <size_t I, class... T>
171    typename tuple_element<I, tuple<T...>>::type&&
172    get(tuple<T...>&&) noexcept; // constexpr in C++14
173template <size_t I, class... T>
174    const typename tuple_element<I, tuple<T...>>::type&&
175    get(const tuple<T...>&&) noexcept; // constexpr in C++14
176
177template <class T1, class... T>
178    constexpr T1& get(tuple<T...>&) noexcept;  // C++14
179template <class T1, class... T>
180    constexpr const T1& get(const tuple<T...>&) noexcept;   // C++14
181template <class T1, class... T>
182    constexpr T1&& get(tuple<T...>&&) noexcept;   // C++14
183template <class T1, class... T>
184    constexpr const T1&& get(const tuple<T...>&&) noexcept;   // C++14
185
186// 20.4.1.6, relational operators:
187template<class... T, class... U> bool operator==(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
188template<class... T, class... U> bool operator<(const tuple<T...>&, const tuple<U...>&);  // constexpr in C++14, removed in C++20
189template<class... T, class... U> bool operator!=(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14, removed in C++20
190template<class... T, class... U> bool operator>(const tuple<T...>&, const tuple<U...>&);  // constexpr in C++14, removed in C++20
191template<class... T, class... U> bool operator<=(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14, removed in C++20
192template<class... T, class... U> bool operator>=(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14, removed in C++20
193template<class... T, class... U>
194  constexpr common_comparison_category_t<synth-three-way-result<T, U>...>
195    operator<=>(const tuple<T...>&, const tuple<U...>&);                                  // since C++20
196
197template <class... Types, class Alloc>
198  struct uses_allocator<tuple<Types...>, Alloc>;
199
200template <class... Types>
201  void
202  swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(noexcept(x.swap(y)));
203
204template <class... Types>
205  constexpr void swap(const tuple<Types...>& x, const tuple<Types...>& y) noexcept(see-below);   // C++23
206
207}  // std
208
209*/
210
211// clang-format on
212
213#include <__compare/common_comparison_category.h>
214#include <__compare/ordering.h>
215#include <__compare/synth_three_way.h>
216#include <__config>
217#include <__cstddef/size_t.h>
218#include <__fwd/array.h>
219#include <__fwd/pair.h>
220#include <__fwd/tuple.h>
221#include <__memory/allocator_arg_t.h>
222#include <__memory/uses_allocator.h>
223#include <__tuple/find_index.h>
224#include <__tuple/ignore.h>
225#include <__tuple/make_tuple_types.h>
226#include <__tuple/sfinae_helpers.h>
227#include <__tuple/tuple_element.h>
228#include <__tuple/tuple_indices.h>
229#include <__tuple/tuple_like_ext.h>
230#include <__tuple/tuple_size.h>
231#include <__tuple/tuple_types.h>
232#include <__type_traits/common_reference.h>
233#include <__type_traits/common_type.h>
234#include <__type_traits/conditional.h>
235#include <__type_traits/conjunction.h>
236#include <__type_traits/copy_cvref.h>
237#include <__type_traits/disjunction.h>
238#include <__type_traits/enable_if.h>
239#include <__type_traits/invoke.h>
240#include <__type_traits/is_arithmetic.h>
241#include <__type_traits/is_assignable.h>
242#include <__type_traits/is_constructible.h>
243#include <__type_traits/is_convertible.h>
244#include <__type_traits/is_empty.h>
245#include <__type_traits/is_final.h>
246#include <__type_traits/is_implicitly_default_constructible.h>
247#include <__type_traits/is_nothrow_assignable.h>
248#include <__type_traits/is_nothrow_constructible.h>
249#include <__type_traits/is_reference.h>
250#include <__type_traits/is_same.h>
251#include <__type_traits/is_swappable.h>
252#include <__type_traits/is_trivially_relocatable.h>
253#include <__type_traits/lazy.h>
254#include <__type_traits/maybe_const.h>
255#include <__type_traits/nat.h>
256#include <__type_traits/negation.h>
257#include <__type_traits/remove_cvref.h>
258#include <__type_traits/remove_reference.h>
259#include <__type_traits/unwrap_ref.h>
260#include <__utility/declval.h>
261#include <__utility/forward.h>
262#include <__utility/integer_sequence.h>
263#include <__utility/move.h>
264#include <__utility/piecewise_construct.h>
265#include <__utility/swap.h>
266#include <version>
267
268// standard-mandated includes
269
270// [tuple.syn]
271#include <compare>
272
273#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
274#  pragma GCC system_header
275#endif
276
277_LIBCPP_PUSH_MACROS
278#include <__undef_macros>
279
280_LIBCPP_BEGIN_NAMESPACE_STD
281
282#ifndef _LIBCPP_CXX03_LANG
283
284// __tuple_leaf
285
286template <size_t _Ip, class _Hp, bool = is_empty<_Hp>::value && !__libcpp_is_final<_Hp>::value >
287class __tuple_leaf;
288
289template <size_t _Ip, class _Hp, bool _Ep>
290inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
291swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y) noexcept(__is_nothrow_swappable_v<_Hp>) {
292  swap(__x.get(), __y.get());
293}
294
295template <size_t _Ip, class _Hp, bool _Ep>
296_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
297swap(const __tuple_leaf<_Ip, _Hp, _Ep>& __x,
298     const __tuple_leaf<_Ip, _Hp, _Ep>& __y) noexcept(__is_nothrow_swappable_v<const _Hp>) {
299  swap(__x.get(), __y.get());
300}
301
302template <size_t _Ip, class _Hp, bool>
303class __tuple_leaf {
304  _Hp __value_;
305
306  template <class _Tp>
307  static _LIBCPP_HIDE_FROM_ABI constexpr bool __can_bind_reference() {
308#  if __has_keyword(__reference_binds_to_temporary)
309    return !__reference_binds_to_temporary(_Hp, _Tp);
310#  else
311    return true;
312#  endif
313  }
314
315public:
316  _LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_leaf& operator=(const __tuple_leaf&) = delete;
317
318  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf() noexcept(is_nothrow_default_constructible<_Hp>::value) : __value_() {
319    static_assert(!is_reference<_Hp>::value, "Attempted to default construct a reference element in a tuple");
320  }
321
322  template <class _Alloc>
323  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf(integral_constant<int, 0>, const _Alloc&) : __value_() {
324    static_assert(!is_reference<_Hp>::value, "Attempted to default construct a reference element in a tuple");
325  }
326
327  template <class _Alloc>
328  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
329      : __value_(allocator_arg_t(), __a) {
330    static_assert(!is_reference<_Hp>::value, "Attempted to default construct a reference element in a tuple");
331  }
332
333  template <class _Alloc>
334  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a) : __value_(__a) {
335    static_assert(!is_reference<_Hp>::value, "Attempted to default construct a reference element in a tuple");
336  }
337
338  template <
339      class _Tp,
340      __enable_if_t<_And<_IsNotSame<__remove_cvref_t<_Tp>, __tuple_leaf>, is_constructible<_Hp, _Tp> >::value, int> = 0>
341  _LIBCPP_HIDE_FROM_ABI
342  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_leaf(_Tp&& __t) noexcept(is_nothrow_constructible<_Hp, _Tp>::value)
343      : __value_(std::forward<_Tp>(__t)) {
344    static_assert(__can_bind_reference<_Tp&&>(),
345                  "Attempted construction of reference element binds to a temporary whose lifetime has ended");
346  }
347
348  template <class _Tp, class _Alloc>
349  _LIBCPP_HIDE_FROM_ABI
350  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
351      : __value_(std::forward<_Tp>(__t)) {
352    static_assert(__can_bind_reference<_Tp&&>(),
353                  "Attempted construction of reference element binds to a temporary whose lifetime has ended");
354  }
355
356  template <class _Tp, class _Alloc>
357  _LIBCPP_HIDE_FROM_ABI
358  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
359      : __value_(allocator_arg_t(), __a, std::forward<_Tp>(__t)) {
360    static_assert(!is_reference<_Hp>::value, "Attempted to uses-allocator construct a reference element in a tuple");
361  }
362
363  template <class _Tp, class _Alloc>
364  _LIBCPP_HIDE_FROM_ABI
365  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
366      : __value_(std::forward<_Tp>(__t), __a) {
367    static_assert(!is_reference<_Hp>::value, "Attempted to uses-allocator construct a reference element in a tuple");
368  }
369
370  _LIBCPP_HIDE_FROM_ABI __tuple_leaf(const __tuple_leaf& __t) = default;
371  _LIBCPP_HIDE_FROM_ABI __tuple_leaf(__tuple_leaf&& __t)      = default;
372
373  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int
374  swap(__tuple_leaf& __t) noexcept(__is_nothrow_swappable_v<__tuple_leaf>) {
375    std::swap(*this, __t);
376    return 0;
377  }
378
379  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int swap(const __tuple_leaf& __t) const
380      noexcept(__is_nothrow_swappable_v<const __tuple_leaf>) {
381    std::swap(*this, __t);
382    return 0;
383  }
384
385  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Hp& get() _NOEXCEPT { return __value_; }
386  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _Hp& get() const _NOEXCEPT { return __value_; }
387};
388
389template <size_t _Ip, class _Hp>
390class __tuple_leaf<_Ip, _Hp, true> : private _Hp {
391public:
392  _LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_leaf& operator=(const __tuple_leaf&) = delete;
393
394  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf() noexcept(is_nothrow_default_constructible<_Hp>::value) {}
395
396  template <class _Alloc>
397  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf(integral_constant<int, 0>, const _Alloc&) {}
398
399  template <class _Alloc>
400  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
401      : _Hp(allocator_arg_t(), __a) {}
402
403  template <class _Alloc>
404  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a) : _Hp(__a) {}
405
406  template <class _Tp,
407            __enable_if_t< _And< _IsNotSame<__remove_cvref_t<_Tp>, __tuple_leaf>, is_constructible<_Hp, _Tp> >::value,
408                           int> = 0>
409  _LIBCPP_HIDE_FROM_ABI
410  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_leaf(_Tp&& __t) noexcept(is_nothrow_constructible<_Hp, _Tp>::value)
411      : _Hp(std::forward<_Tp>(__t)) {}
412
413  template <class _Tp, class _Alloc>
414  _LIBCPP_HIDE_FROM_ABI constexpr explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
415      : _Hp(std::forward<_Tp>(__t)) {}
416
417  template <class _Tp, class _Alloc>
418  _LIBCPP_HIDE_FROM_ABI constexpr explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
419      : _Hp(allocator_arg_t(), __a, std::forward<_Tp>(__t)) {}
420
421  template <class _Tp, class _Alloc>
422  _LIBCPP_HIDE_FROM_ABI constexpr explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
423      : _Hp(std::forward<_Tp>(__t), __a) {}
424
425  __tuple_leaf(__tuple_leaf const&) = default;
426  __tuple_leaf(__tuple_leaf&&)      = default;
427
428  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int
429  swap(__tuple_leaf& __t) noexcept(__is_nothrow_swappable_v<__tuple_leaf>) {
430    std::swap(*this, __t);
431    return 0;
432  }
433
434  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int swap(const __tuple_leaf& __rhs) const
435      noexcept(__is_nothrow_swappable_v<const __tuple_leaf>) {
436    std::swap(*this, __rhs);
437    return 0;
438  }
439
440  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Hp& get() _NOEXCEPT { return static_cast<_Hp&>(*this); }
441  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _Hp& get() const _NOEXCEPT {
442    return static_cast<const _Hp&>(*this);
443  }
444};
445
446template <class... _Tp>
447_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __swallow(_Tp&&...) _NOEXCEPT {}
448
449template <class _Tp>
450struct __all_default_constructible;
451
452template <class... _Tp>
453struct __all_default_constructible<__tuple_types<_Tp...>> : __all<is_default_constructible<_Tp>::value...> {};
454
455// __tuple_impl
456
457template <class _Indx, class... _Tp>
458struct __tuple_impl;
459
460template <size_t... _Indx, class... _Tp>
461struct _LIBCPP_DECLSPEC_EMPTY_BASES __tuple_impl<__tuple_indices<_Indx...>, _Tp...>
462    : public __tuple_leaf<_Indx, _Tp>... {
463  _LIBCPP_HIDE_FROM_ABI constexpr __tuple_impl() noexcept(
464      __all<is_nothrow_default_constructible<_Tp>::value...>::value) {}
465
466  template <size_t... _Uf, class... _Tf, size_t... _Ul, class... _Tl, class... _Up>
467  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_impl(
468      __tuple_indices<_Uf...>,
469      __tuple_types<_Tf...>,
470      __tuple_indices<_Ul...>,
471      __tuple_types<_Tl...>,
472      _Up&&... __u) noexcept(__all<is_nothrow_constructible<_Tf, _Up>::value...>::value &&
473                             __all<is_nothrow_default_constructible<_Tl>::value...>::value)
474      : __tuple_leaf<_Uf, _Tf>(std::forward<_Up>(__u))..., __tuple_leaf<_Ul, _Tl>()... {}
475
476  template <class _Alloc, size_t... _Uf, class... _Tf, size_t... _Ul, class... _Tl, class... _Up>
477  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __tuple_impl(
478      allocator_arg_t,
479      const _Alloc& __a,
480      __tuple_indices<_Uf...>,
481      __tuple_types<_Tf...>,
482      __tuple_indices<_Ul...>,
483      __tuple_types<_Tl...>,
484      _Up&&... __u)
485      : __tuple_leaf<_Uf, _Tf>(__uses_alloc_ctor<_Tf, _Alloc, _Up>(), __a, std::forward<_Up>(__u))...,
486        __tuple_leaf<_Ul, _Tl>(__uses_alloc_ctor<_Tl, _Alloc>(), __a)... {}
487
488  template <class _Tuple, __enable_if_t<__tuple_constructible<_Tuple, tuple<_Tp...> >::value, int> = 0>
489  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_impl(_Tuple&& __t) noexcept(
490      (__all<is_nothrow_constructible<
491           _Tp,
492           typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>::value...>::value))
493      : __tuple_leaf<_Indx, _Tp>(
494            std::forward<typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>(
495                std::get<_Indx>(__t)))... {}
496
497  template <class _Alloc, class _Tuple, __enable_if_t<__tuple_constructible<_Tuple, tuple<_Tp...> >::value, int> = 0>
498  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_impl(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
499      : __tuple_leaf<_Indx, _Tp>(
500            __uses_alloc_ctor<_Tp,
501                              _Alloc,
502                              typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>(),
503            __a,
504            std::forward<typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>(
505                std::get<_Indx>(__t)))... {}
506
507  __tuple_impl(const __tuple_impl&) = default;
508  __tuple_impl(__tuple_impl&&)      = default;
509
510  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
511  swap(__tuple_impl& __t) noexcept(__all<__is_nothrow_swappable_v<_Tp>...>::value) {
512    std::__swallow(__tuple_leaf<_Indx, _Tp>::swap(static_cast<__tuple_leaf<_Indx, _Tp>&>(__t))...);
513  }
514
515  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void swap(const __tuple_impl& __t) const
516      noexcept(__all<__is_nothrow_swappable_v<const _Tp>...>::value) {
517    std::__swallow(__tuple_leaf<_Indx, _Tp>::swap(static_cast<const __tuple_leaf<_Indx, _Tp>&>(__t))...);
518  }
519};
520
521template <class _Dest, class _Source, size_t... _Np>
522_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
523__memberwise_copy_assign(_Dest& __dest, _Source const& __source, __tuple_indices<_Np...>) {
524  std::__swallow(((std::get<_Np>(__dest) = std::get<_Np>(__source)), void(), 0)...);
525}
526
527template <class _Dest, class _Source, class... _Up, size_t... _Np>
528_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
529__memberwise_forward_assign(_Dest& __dest, _Source&& __source, __tuple_types<_Up...>, __tuple_indices<_Np...>) {
530  std::__swallow(((std::get<_Np>(__dest) = std::forward<_Up>(std::get<_Np>(__source))), void(), 0)...);
531}
532
533template <class... _Tp>
534class _LIBCPP_TEMPLATE_VIS tuple {
535  typedef __tuple_impl<typename __make_tuple_indices<sizeof...(_Tp)>::type, _Tp...> _BaseT;
536
537  _BaseT __base_;
538
539  template <size_t _Jp, class... _Up>
540  friend _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Jp, tuple<_Up...> >::type& get(tuple<_Up...>&) _NOEXCEPT;
541  template <size_t _Jp, class... _Up>
542  friend _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Jp, tuple<_Up...> >::type&
543  get(const tuple<_Up...>&) _NOEXCEPT;
544  template <size_t _Jp, class... _Up>
545  friend _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Jp, tuple<_Up...> >::type&&
546  get(tuple<_Up...>&&) _NOEXCEPT;
547  template <size_t _Jp, class... _Up>
548  friend _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Jp, tuple<_Up...> >::type&&
549  get(const tuple<_Up...>&&) _NOEXCEPT;
550
551public:
552  using __trivially_relocatable = __conditional_t<_And<__libcpp_is_trivially_relocatable<_Tp>...>::value, tuple, void>;
553
554  // [tuple.cnstr]
555
556  // tuple() constructors (including allocator_arg_t variants)
557  template <template <class...> class _IsImpDefault                = __is_implicitly_default_constructible,
558            template <class...> class _IsDefault                   = is_default_constructible,
559            __enable_if_t< _And< _IsDefault<_Tp>... >::value, int> = 0>
560  _LIBCPP_HIDE_FROM_ABI constexpr explicit(_Not<_Lazy<_And, _IsImpDefault<_Tp>...> >::value)
561      tuple() noexcept(_And<is_nothrow_default_constructible<_Tp>...>::value) {}
562
563  template <class _Alloc,
564            template <class...> class _IsImpDefault                = __is_implicitly_default_constructible,
565            template <class...> class _IsDefault                   = is_default_constructible,
566            __enable_if_t< _And< _IsDefault<_Tp>... >::value, int> = 0>
567  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(_Not<_Lazy<_And, _IsImpDefault<_Tp>...> >::value)
568      tuple(allocator_arg_t, _Alloc const& __a)
569      : __base_(allocator_arg_t(),
570                __a,
571                __tuple_indices<>(),
572                __tuple_types<>(),
573                typename __make_tuple_indices<sizeof...(_Tp), 0>::type(),
574                __tuple_types<_Tp...>()) {}
575
576  // tuple(const T&...) constructors (including allocator_arg_t variants)
577  template <template <class...> class _And = _And,
578            __enable_if_t< _And< _BoolConstant<sizeof...(_Tp) >= 1>, is_copy_constructible<_Tp>... >::value, int> = 0>
579  _LIBCPP_HIDE_FROM_ABI
580  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(_Not<_Lazy<_And, is_convertible<const _Tp&, _Tp>...> >::value)
581      tuple(const _Tp&... __t) noexcept(_And<is_nothrow_copy_constructible<_Tp>...>::value)
582      : __base_(typename __make_tuple_indices<sizeof...(_Tp)>::type(),
583                typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
584                typename __make_tuple_indices<0>::type(),
585                typename __make_tuple_types<tuple, 0>::type(),
586                __t...) {}
587
588  template <class _Alloc,
589            template <class...> class _And = _And,
590            __enable_if_t< _And< _BoolConstant<sizeof...(_Tp) >= 1>, is_copy_constructible<_Tp>... >::value, int> = 0>
591  _LIBCPP_HIDE_FROM_ABI
592  _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(_Not<_Lazy<_And, is_convertible<const _Tp&, _Tp>...> >::value)
593      tuple(allocator_arg_t, const _Alloc& __a, const _Tp&... __t)
594      : __base_(allocator_arg_t(),
595                __a,
596                typename __make_tuple_indices<sizeof...(_Tp)>::type(),
597                typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
598                typename __make_tuple_indices<0>::type(),
599                typename __make_tuple_types<tuple, 0>::type(),
600                __t...) {}
601
602  // tuple(U&& ...) constructors (including allocator_arg_t variants)
603  template <class... _Up>
604  struct _IsThisTuple : false_type {};
605  template <class _Up>
606  struct _IsThisTuple<_Up> : is_same<__remove_cvref_t<_Up>, tuple> {};
607
608  template <class... _Up>
609  struct _EnableUTypesCtor
610      : _And< _BoolConstant<sizeof...(_Tp) >= 1>,
611              _Not<_IsThisTuple<_Up...> >, // extension to allow mis-behaved user constructors
612              is_constructible<_Tp, _Up>... > {};
613
614  template <class... _Up,
615            __enable_if_t< _And< _BoolConstant<sizeof...(_Up) == sizeof...(_Tp)>, _EnableUTypesCtor<_Up...> >::value,
616                           int> = 0>
617  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(_Not<_Lazy<_And, is_convertible<_Up, _Tp>...> >::value)
618      tuple(_Up&&... __u) noexcept(_And<is_nothrow_constructible<_Tp, _Up>...>::value)
619      : __base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
620                typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
621                typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
622                typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
623                std::forward<_Up>(__u)...) {}
624
625  template <class _Alloc,
626            class... _Up,
627            __enable_if_t< _And< _BoolConstant<sizeof...(_Up) == sizeof...(_Tp)>, _EnableUTypesCtor<_Up...> >::value,
628                           int> = 0>
629  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(_Not<_Lazy<_And, is_convertible<_Up, _Tp>...> >::value)
630      tuple(allocator_arg_t, const _Alloc& __a, _Up&&... __u)
631      : __base_(allocator_arg_t(),
632                __a,
633                typename __make_tuple_indices<sizeof...(_Up)>::type(),
634                typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
635                typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
636                typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
637                std::forward<_Up>(__u)...) {}
638
639  // Copy and move constructors (including the allocator_arg_t variants)
640  tuple(const tuple&) = default;
641  tuple(tuple&&)      = default;
642
643  template <class _Alloc,
644            template <class...> class _And                                  = _And,
645            __enable_if_t< _And<is_copy_constructible<_Tp>...>::value, int> = 0>
646  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple(allocator_arg_t, const _Alloc& __alloc, const tuple& __t)
647      : __base_(allocator_arg_t(), __alloc, __t) {}
648
649  template <class _Alloc,
650            template <class...> class _And                                  = _And,
651            __enable_if_t< _And<is_move_constructible<_Tp>...>::value, int> = 0>
652  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple(allocator_arg_t, const _Alloc& __alloc, tuple&& __t)
653      : __base_(allocator_arg_t(), __alloc, std::move(__t)) {}
654
655  // tuple(const tuple<U...>&) constructors (including allocator_arg_t variants)
656
657  template <class _OtherTuple, class _DecayedOtherTuple = __remove_cvref_t<_OtherTuple>, class = void>
658  struct _EnableCtorFromUTypesTuple : false_type {};
659
660  template <class _OtherTuple, class... _Up>
661  struct _EnableCtorFromUTypesTuple<
662      _OtherTuple,
663      tuple<_Up...>,
664      // the length of the packs needs to checked first otherwise the 2 packs cannot be expanded simultaneously below
665      __enable_if_t<sizeof...(_Up) == sizeof...(_Tp)>>
666      : _And<
667            // the two conditions below are not in spec. The purpose is to disable the UTypes Ctor when copy/move Ctor
668            // can work. Otherwise, is_constructible can trigger hard error in those cases
669            // https://godbolt.org/z/M94cGdKcE
670            _Not<is_same<_OtherTuple, const tuple&> >,
671            _Not<is_same<_OtherTuple, tuple&&> >,
672            is_constructible<_Tp, __copy_cvref_t<_OtherTuple, _Up> >...,
673            _Lazy<_Or,
674                  _BoolConstant<sizeof...(_Tp) != 1>,
675                  // _Tp and _Up are 1-element packs - the pack expansions look
676                  // weird to avoid tripping up the type traits in degenerate cases
677                  _Lazy<_And,
678                        _Not<is_same<_Tp, _Up> >...,
679                        _Not<is_convertible<_OtherTuple, _Tp> >...,
680                        _Not<is_constructible<_Tp, _OtherTuple> >... > > > {};
681
682  template <class... _Up, __enable_if_t< _And< _EnableCtorFromUTypesTuple<const tuple<_Up...>&> >::value, int> = 0>
683  _LIBCPP_HIDE_FROM_ABI
684  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(_Not<_Lazy<_And, is_convertible<const _Up&, _Tp>...> >::value)
685      tuple(const tuple<_Up...>& __t) noexcept(_And<is_nothrow_constructible<_Tp, const _Up&>...>::value)
686      : __base_(__t) {}
687
688  template <class... _Up,
689            class _Alloc,
690            __enable_if_t< _And< _EnableCtorFromUTypesTuple<const tuple<_Up...>&> >::value, int> = 0>
691  _LIBCPP_HIDE_FROM_ABI
692  _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(_Not<_Lazy<_And, is_convertible<const _Up&, _Tp>...> >::value)
693      tuple(allocator_arg_t, const _Alloc& __a, const tuple<_Up...>& __t)
694      : __base_(allocator_arg_t(), __a, __t) {}
695
696#  if _LIBCPP_STD_VER >= 23
697  // tuple(tuple<U...>&) constructors (including allocator_arg_t variants)
698
699  template <class... _Up, enable_if_t< _EnableCtorFromUTypesTuple<tuple<_Up...>&>::value>* = nullptr>
700  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_Lazy<_And, is_convertible<_Up&, _Tp>...>::value) tuple(tuple<_Up...>& __t)
701      : __base_(__t) {}
702
703  template <class _Alloc, class... _Up, enable_if_t< _EnableCtorFromUTypesTuple<tuple<_Up...>&>::value>* = nullptr>
704  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_Lazy<_And, is_convertible<_Up&, _Tp>...>::value)
705      tuple(allocator_arg_t, const _Alloc& __alloc, tuple<_Up...>& __t)
706      : __base_(allocator_arg_t(), __alloc, __t) {}
707#  endif // _LIBCPP_STD_VER >= 23
708
709  // tuple(tuple<U...>&&) constructors (including allocator_arg_t variants)
710  template <class... _Up, __enable_if_t< _And< _EnableCtorFromUTypesTuple<tuple<_Up...>&&> >::value, int> = 0>
711  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(_Not<_Lazy<_And, is_convertible<_Up, _Tp>...> >::value)
712      tuple(tuple<_Up...>&& __t) noexcept(_And<is_nothrow_constructible<_Tp, _Up>...>::value)
713      : __base_(std::move(__t)) {}
714
715  template <class _Alloc,
716            class... _Up,
717            __enable_if_t< _And< _EnableCtorFromUTypesTuple<tuple<_Up...>&&> >::value, int> = 0>
718  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(_Not<_Lazy<_And, is_convertible<_Up, _Tp>...> >::value)
719      tuple(allocator_arg_t, const _Alloc& __a, tuple<_Up...>&& __t)
720      : __base_(allocator_arg_t(), __a, std::move(__t)) {}
721
722#  if _LIBCPP_STD_VER >= 23
723  // tuple(const tuple<U...>&&) constructors (including allocator_arg_t variants)
724
725  template <class... _Up, enable_if_t< _EnableCtorFromUTypesTuple<const tuple<_Up...>&&>::value>* = nullptr>
726  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_Lazy<_And, is_convertible<const _Up&&, _Tp>...>::value)
727      tuple(const tuple<_Up...>&& __t)
728      : __base_(std::move(__t)) {}
729
730  template <class _Alloc,
731            class... _Up,
732            enable_if_t< _EnableCtorFromUTypesTuple<const tuple<_Up...>&&>::value>* = nullptr>
733  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_Lazy<_And, is_convertible<const _Up&&, _Tp>...>::value)
734      tuple(allocator_arg_t, const _Alloc& __alloc, const tuple<_Up...>&& __t)
735      : __base_(allocator_arg_t(), __alloc, std::move(__t)) {}
736#  endif // _LIBCPP_STD_VER >= 23
737
738  // tuple(const pair<U1, U2>&) constructors (including allocator_arg_t variants)
739
740  template <template <class...> class _Pred,
741            class _Pair,
742            class _DecayedPair = __remove_cvref_t<_Pair>,
743            class _Tuple       = tuple>
744  struct _CtorPredicateFromPair : false_type {};
745
746  template <template <class...> class _Pred, class _Pair, class _Up1, class _Up2, class _Tp1, class _Tp2>
747  struct _CtorPredicateFromPair<_Pred, _Pair, pair<_Up1, _Up2>, tuple<_Tp1, _Tp2> >
748      : _And< _Pred<_Tp1, __copy_cvref_t<_Pair, _Up1> >, _Pred<_Tp2, __copy_cvref_t<_Pair, _Up2> > > {};
749
750  template <class _Pair>
751  struct _EnableCtorFromPair : _CtorPredicateFromPair<is_constructible, _Pair> {};
752
753  template <class _Pair>
754  struct _NothrowConstructibleFromPair : _CtorPredicateFromPair<is_nothrow_constructible, _Pair> {};
755
756  template <class _Pair, class _DecayedPair = __remove_cvref_t<_Pair>, class _Tuple = tuple>
757  struct _BothImplicitlyConvertible : false_type {};
758
759  template <class _Pair, class _Up1, class _Up2, class _Tp1, class _Tp2>
760  struct _BothImplicitlyConvertible<_Pair, pair<_Up1, _Up2>, tuple<_Tp1, _Tp2> >
761      : _And< is_convertible<__copy_cvref_t<_Pair, _Up1>, _Tp1>, is_convertible<__copy_cvref_t<_Pair, _Up2>, _Tp2> > {};
762
763  template <class _Up1,
764            class _Up2,
765            template <class...> class _And                                                   = _And,
766            __enable_if_t< _And< _EnableCtorFromPair<const pair<_Up1, _Up2>&> >::value, int> = 0>
767  _LIBCPP_HIDE_FROM_ABI
768  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(_Not<_BothImplicitlyConvertible<const pair<_Up1, _Up2>&> >::value)
769      tuple(const pair<_Up1, _Up2>& __p) noexcept(_NothrowConstructibleFromPair<const pair<_Up1, _Up2>&>::value)
770      : __base_(__p) {}
771
772  template <class _Alloc,
773            class _Up1,
774            class _Up2,
775            template <class...> class _And                                                   = _And,
776            __enable_if_t< _And< _EnableCtorFromPair<const pair<_Up1, _Up2>&> >::value, int> = 0>
777  _LIBCPP_HIDE_FROM_ABI
778  _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(_Not<_BothImplicitlyConvertible<const pair<_Up1, _Up2>&> >::value)
779      tuple(allocator_arg_t, const _Alloc& __a, const pair<_Up1, _Up2>& __p)
780      : __base_(allocator_arg_t(), __a, __p) {}
781
782#  if _LIBCPP_STD_VER >= 23
783  // tuple(pair<U1, U2>&) constructors (including allocator_arg_t variants)
784
785  template <class _U1, class _U2, enable_if_t< _EnableCtorFromPair<pair<_U1, _U2>&>::value>* = nullptr>
786  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_BothImplicitlyConvertible<pair<_U1, _U2>&>::value)
787      tuple(pair<_U1, _U2>& __p)
788      : __base_(__p) {}
789
790  template <class _Alloc,
791            class _U1,
792            class _U2,
793            enable_if_t< _EnableCtorFromPair<std::pair<_U1, _U2>&>::value>* = nullptr>
794  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_BothImplicitlyConvertible<pair<_U1, _U2>&>::value)
795      tuple(allocator_arg_t, const _Alloc& __alloc, pair<_U1, _U2>& __p)
796      : __base_(allocator_arg_t(), __alloc, __p) {}
797#  endif
798
799  // tuple(pair<U1, U2>&&) constructors (including allocator_arg_t variants)
800
801  template <class _Up1,
802            class _Up2,
803            template <class...> class _And                                              = _And,
804            __enable_if_t< _And< _EnableCtorFromPair<pair<_Up1, _Up2>&&> >::value, int> = 0>
805  _LIBCPP_HIDE_FROM_ABI
806  _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit(_Not<_BothImplicitlyConvertible<pair<_Up1, _Up2>&&> >::value)
807      tuple(pair<_Up1, _Up2>&& __p) noexcept(_NothrowConstructibleFromPair<pair<_Up1, _Up2>&&>::value)
808      : __base_(std::move(__p)) {}
809
810  template <class _Alloc,
811            class _Up1,
812            class _Up2,
813            template <class...> class _And                                              = _And,
814            __enable_if_t< _And< _EnableCtorFromPair<pair<_Up1, _Up2>&&> >::value, int> = 0>
815  _LIBCPP_HIDE_FROM_ABI
816  _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit(_Not<_BothImplicitlyConvertible<pair<_Up1, _Up2>&&> >::value)
817      tuple(allocator_arg_t, const _Alloc& __a, pair<_Up1, _Up2>&& __p)
818      : __base_(allocator_arg_t(), __a, std::move(__p)) {}
819
820#  if _LIBCPP_STD_VER >= 23
821  // tuple(const pair<U1, U2>&&) constructors (including allocator_arg_t variants)
822
823  template <class _U1, class _U2, enable_if_t< _EnableCtorFromPair<const pair<_U1, _U2>&&>::value>* = nullptr>
824  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_BothImplicitlyConvertible<const pair<_U1, _U2>&&>::value)
825      tuple(const pair<_U1, _U2>&& __p)
826      : __base_(std::move(__p)) {}
827
828  template <class _Alloc,
829            class _U1,
830            class _U2,
831            enable_if_t< _EnableCtorFromPair<const pair<_U1, _U2>&&>::value>* = nullptr>
832  _LIBCPP_HIDE_FROM_ABI constexpr explicit(!_BothImplicitlyConvertible<const pair<_U1, _U2>&&>::value)
833      tuple(allocator_arg_t, const _Alloc& __alloc, const pair<_U1, _U2>&& __p)
834      : __base_(allocator_arg_t(), __alloc, std::move(__p)) {}
835#  endif // _LIBCPP_STD_VER >= 23
836
837  // [tuple.assign]
838  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
839  operator=(_If<_And<is_copy_assignable<_Tp>...>::value, tuple, __nat> const& __tuple) noexcept(
840      _And<is_nothrow_copy_assignable<_Tp>...>::value) {
841    std::__memberwise_copy_assign(*this, __tuple, typename __make_tuple_indices<sizeof...(_Tp)>::type());
842    return *this;
843  }
844
845#  if _LIBCPP_STD_VER >= 23
846  _LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(tuple const& __tuple) const
847    requires(_And<is_copy_assignable<const _Tp>...>::value)
848  {
849    std::__memberwise_copy_assign(*this, __tuple, typename __make_tuple_indices<sizeof...(_Tp)>::type());
850    return *this;
851  }
852
853  _LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(tuple&& __tuple) const
854    requires(_And<is_assignable<const _Tp&, _Tp>...>::value)
855  {
856    std::__memberwise_forward_assign(
857        *this, std::move(__tuple), __tuple_types<_Tp...>(), typename __make_tuple_indices<sizeof...(_Tp)>::type());
858    return *this;
859  }
860#  endif // _LIBCPP_STD_VER >= 23
861
862  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
863  operator=(_If<_And<is_move_assignable<_Tp>...>::value, tuple, __nat>&& __tuple) noexcept(
864      _And<is_nothrow_move_assignable<_Tp>...>::value) {
865    std::__memberwise_forward_assign(
866        *this, std::move(__tuple), __tuple_types<_Tp...>(), typename __make_tuple_indices<sizeof...(_Tp)>::type());
867    return *this;
868  }
869
870  template <
871      class... _Up,
872      __enable_if_t< _And< _BoolConstant<sizeof...(_Tp) == sizeof...(_Up)>, is_assignable<_Tp&, _Up const&>... >::value,
873                     int> = 0>
874  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
875  operator=(tuple<_Up...> const& __tuple) noexcept(_And<is_nothrow_assignable<_Tp&, _Up const&>...>::value) {
876    std::__memberwise_copy_assign(*this, __tuple, typename __make_tuple_indices<sizeof...(_Tp)>::type());
877    return *this;
878  }
879
880  template <class... _Up,
881            __enable_if_t< _And< _BoolConstant<sizeof...(_Tp) == sizeof...(_Up)>, is_assignable<_Tp&, _Up>... >::value,
882                           int> = 0>
883  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
884  operator=(tuple<_Up...>&& __tuple) noexcept(_And<is_nothrow_assignable<_Tp&, _Up>...>::value) {
885    std::__memberwise_forward_assign(
886        *this, std::move(__tuple), __tuple_types<_Up...>(), typename __make_tuple_indices<sizeof...(_Tp)>::type());
887    return *this;
888  }
889
890#  if _LIBCPP_STD_VER >= 23
891  template <class... _UTypes,
892            enable_if_t< _And<_BoolConstant<sizeof...(_Tp) == sizeof...(_UTypes)>,
893                              is_assignable<const _Tp&, const _UTypes&>...>::value>* = nullptr>
894  _LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(const tuple<_UTypes...>& __u) const {
895    std::__memberwise_copy_assign(*this, __u, typename __make_tuple_indices<sizeof...(_Tp)>::type());
896    return *this;
897  }
898
899  template <class... _UTypes,
900            enable_if_t< _And<_BoolConstant<sizeof...(_Tp) == sizeof...(_UTypes)>,
901                              is_assignable<const _Tp&, _UTypes>...>::value>* = nullptr>
902  _LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(tuple<_UTypes...>&& __u) const {
903    std::__memberwise_forward_assign(
904        *this, __u, __tuple_types<_UTypes...>(), typename __make_tuple_indices<sizeof...(_Tp)>::type());
905    return *this;
906  }
907#  endif // _LIBCPP_STD_VER >= 23
908
909  template <template <class...> class _Pred,
910            bool _Const,
911            class _Pair,
912            class _DecayedPair = __remove_cvref_t<_Pair>,
913            class _Tuple       = tuple>
914  struct _AssignPredicateFromPair : false_type {};
915
916  template <template <class...> class _Pred, bool _Const, class _Pair, class _Up1, class _Up2, class _Tp1, class _Tp2>
917  struct _AssignPredicateFromPair<_Pred, _Const, _Pair, pair<_Up1, _Up2>, tuple<_Tp1, _Tp2> >
918      : _And<_Pred<__maybe_const<_Const, _Tp1>&, __copy_cvref_t<_Pair, _Up1> >,
919             _Pred<__maybe_const<_Const, _Tp2>&, __copy_cvref_t<_Pair, _Up2> > > {};
920
921  template <bool _Const, class _Pair>
922  struct _EnableAssignFromPair : _AssignPredicateFromPair<is_assignable, _Const, _Pair> {};
923
924  template <bool _Const, class _Pair>
925  struct _NothrowAssignFromPair : _AssignPredicateFromPair<is_nothrow_assignable, _Const, _Pair> {};
926
927#  if _LIBCPP_STD_VER >= 23
928  template <class _U1, class _U2, enable_if_t< _EnableAssignFromPair<true, const pair<_U1, _U2>&>::value>* = nullptr>
929  _LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(const pair<_U1, _U2>& __pair) const
930      noexcept(_NothrowAssignFromPair<true, const pair<_U1, _U2>&>::value) {
931    std::get<0>(*this) = __pair.first;
932    std::get<1>(*this) = __pair.second;
933    return *this;
934  }
935
936  template <class _U1, class _U2, enable_if_t< _EnableAssignFromPair<true, pair<_U1, _U2>&&>::value>* = nullptr>
937  _LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(pair<_U1, _U2>&& __pair) const
938      noexcept(_NothrowAssignFromPair<true, pair<_U1, _U2>&&>::value) {
939    std::get<0>(*this) = std::move(__pair.first);
940    std::get<1>(*this) = std::move(__pair.second);
941    return *this;
942  }
943#  endif // _LIBCPP_STD_VER >= 23
944
945  template <class _Up1,
946            class _Up2,
947            __enable_if_t< _EnableAssignFromPair<false, pair<_Up1, _Up2> const&>::value, int> = 0>
948  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
949  operator=(pair<_Up1, _Up2> const& __pair) noexcept(_NothrowAssignFromPair<false, pair<_Up1, _Up2> const&>::value) {
950    std::get<0>(*this) = __pair.first;
951    std::get<1>(*this) = __pair.second;
952    return *this;
953  }
954
955  template <class _Up1, class _Up2, __enable_if_t< _EnableAssignFromPair<false, pair<_Up1, _Up2>&&>::value, int> = 0>
956  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
957  operator=(pair<_Up1, _Up2>&& __pair) noexcept(_NothrowAssignFromPair<false, pair<_Up1, _Up2>&&>::value) {
958    std::get<0>(*this) = std::forward<_Up1>(__pair.first);
959    std::get<1>(*this) = std::forward<_Up2>(__pair.second);
960    return *this;
961  }
962
963  // EXTENSION
964  template <
965      class _Up,
966      size_t _Np,
967      __enable_if_t< _And< _BoolConstant<_Np == sizeof...(_Tp)>, is_assignable<_Tp&, _Up const&>... >::value, int> = 0>
968  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
969  operator=(array<_Up, _Np> const& __array) noexcept(_And<is_nothrow_assignable<_Tp&, _Up const&>...>::value) {
970    std::__memberwise_copy_assign(*this, __array, typename __make_tuple_indices<sizeof...(_Tp)>::type());
971    return *this;
972  }
973
974  // EXTENSION
975  template <class _Up,
976            size_t _Np,
977            class = void,
978            __enable_if_t< _And< _BoolConstant<_Np == sizeof...(_Tp)>, is_assignable<_Tp&, _Up>... >::value, int> = 0>
979  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
980  operator=(array<_Up, _Np>&& __array) noexcept(_And<is_nothrow_assignable<_Tp&, _Up>...>::value) {
981    std::__memberwise_forward_assign(
982        *this,
983        std::move(__array),
984        __tuple_types<_If<true, _Up, _Tp>...>(),
985        typename __make_tuple_indices<sizeof...(_Tp)>::type());
986    return *this;
987  }
988
989  // [tuple.swap]
990  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
991  swap(tuple& __t) noexcept(__all<__is_nothrow_swappable_v<_Tp>...>::value) {
992    __base_.swap(__t.__base_);
993  }
994
995#  if _LIBCPP_STD_VER >= 23
996  _LIBCPP_HIDE_FROM_ABI constexpr void swap(const tuple& __t) const
997      noexcept(__all<is_nothrow_swappable_v<const _Tp&>...>::value) {
998    __base_.swap(__t.__base_);
999  }
1000#  endif // _LIBCPP_STD_VER >= 23
1001};
1002
1003template <>
1004class _LIBCPP_TEMPLATE_VIS tuple<> {
1005public:
1006  constexpr tuple() _NOEXCEPT = default;
1007  template <class _Alloc>
1008  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1009  template <class _Alloc>
1010  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple(allocator_arg_t, const _Alloc&, const tuple&) _NOEXCEPT {}
1011  template <class _Up>
1012  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple(array<_Up, 0>) _NOEXCEPT {}
1013  template <class _Alloc, class _Up>
1014  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple(allocator_arg_t, const _Alloc&, array<_Up, 0>) _NOEXCEPT {}
1015  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(tuple&) _NOEXCEPT {}
1016#  if _LIBCPP_STD_VER >= 23
1017  _LIBCPP_HIDE_FROM_ABI constexpr void swap(const tuple&) const noexcept {}
1018#  endif
1019};
1020
1021#  if _LIBCPP_STD_VER >= 23
1022template <class... _TTypes, class... _UTypes, template <class> class _TQual, template <class> class _UQual>
1023  requires requires { typename tuple<common_reference_t<_TQual<_TTypes>, _UQual<_UTypes>>...>; }
1024struct basic_common_reference<tuple<_TTypes...>, tuple<_UTypes...>, _TQual, _UQual> {
1025  using type = tuple<common_reference_t<_TQual<_TTypes>, _UQual<_UTypes>>...>;
1026};
1027
1028template <class... _TTypes, class... _UTypes>
1029  requires requires { typename tuple<common_type_t<_TTypes, _UTypes>...>; }
1030struct common_type<tuple<_TTypes...>, tuple<_UTypes...>> {
1031  using type = tuple<common_type_t<_TTypes, _UTypes>...>;
1032};
1033#  endif // _LIBCPP_STD_VER >= 23
1034
1035#  if _LIBCPP_STD_VER >= 17
1036template <class... _Tp>
1037tuple(_Tp...) -> tuple<_Tp...>;
1038template <class _Tp1, class _Tp2>
1039tuple(pair<_Tp1, _Tp2>) -> tuple<_Tp1, _Tp2>;
1040template <class _Alloc, class... _Tp>
1041tuple(allocator_arg_t, _Alloc, _Tp...) -> tuple<_Tp...>;
1042template <class _Alloc, class _Tp1, class _Tp2>
1043tuple(allocator_arg_t, _Alloc, pair<_Tp1, _Tp2>) -> tuple<_Tp1, _Tp2>;
1044template <class _Alloc, class... _Tp>
1045tuple(allocator_arg_t, _Alloc, tuple<_Tp...>) -> tuple<_Tp...>;
1046#  endif
1047
1048template <class... _Tp, __enable_if_t<__all<__is_swappable_v<_Tp>...>::value, int> = 0>
1049inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
1050swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u) noexcept(__all<__is_nothrow_swappable_v<_Tp>...>::value) {
1051  __t.swap(__u);
1052}
1053
1054#  if _LIBCPP_STD_VER >= 23
1055template <class... _Tp>
1056_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<__all<is_swappable_v<const _Tp>...>::value, void>
1057swap(const tuple<_Tp...>& __lhs,
1058     const tuple<_Tp...>& __rhs) noexcept(__all<is_nothrow_swappable_v<const _Tp>...>::value) {
1059  __lhs.swap(__rhs);
1060}
1061#  endif
1062
1063// get
1064
1065template <size_t _Ip, class... _Tp>
1066inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Ip, tuple<_Tp...> >::type&
1067get(tuple<_Tp...>& __t) _NOEXCEPT {
1068  typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1069  return static_cast<__tuple_leaf<_Ip, type>&>(__t.__base_).get();
1070}
1071
1072template <size_t _Ip, class... _Tp>
1073inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, tuple<_Tp...> >::type&
1074get(const tuple<_Tp...>& __t) _NOEXCEPT {
1075  typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1076  return static_cast<const __tuple_leaf<_Ip, type>&>(__t.__base_).get();
1077}
1078
1079template <size_t _Ip, class... _Tp>
1080inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Ip, tuple<_Tp...> >::type&&
1081get(tuple<_Tp...>&& __t) _NOEXCEPT {
1082  typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1083  return static_cast<type&&>(static_cast<__tuple_leaf<_Ip, type>&&>(__t.__base_).get());
1084}
1085
1086template <size_t _Ip, class... _Tp>
1087inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, tuple<_Tp...> >::type&&
1088get(const tuple<_Tp...>&& __t) _NOEXCEPT {
1089  typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1090  return static_cast<const type&&>(static_cast<const __tuple_leaf<_Ip, type>&&>(__t.__base_).get());
1091}
1092
1093#  if _LIBCPP_STD_VER >= 14
1094
1095template <class _T1, class... _Args>
1096inline _LIBCPP_HIDE_FROM_ABI constexpr _T1& get(tuple<_Args...>& __tup) noexcept {
1097  return std::get<__find_exactly_one_t<_T1, _Args...>::value>(__tup);
1098}
1099
1100template <class _T1, class... _Args>
1101inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const& get(tuple<_Args...> const& __tup) noexcept {
1102  return std::get<__find_exactly_one_t<_T1, _Args...>::value>(__tup);
1103}
1104
1105template <class _T1, class... _Args>
1106inline _LIBCPP_HIDE_FROM_ABI constexpr _T1&& get(tuple<_Args...>&& __tup) noexcept {
1107  return std::get<__find_exactly_one_t<_T1, _Args...>::value>(std::move(__tup));
1108}
1109
1110template <class _T1, class... _Args>
1111inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const&& get(tuple<_Args...> const&& __tup) noexcept {
1112  return std::get<__find_exactly_one_t<_T1, _Args...>::value>(std::move(__tup));
1113}
1114
1115#  endif
1116
1117// tie
1118
1119template <class... _Tp>
1120inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 tuple<_Tp&...> tie(_Tp&... __t) _NOEXCEPT {
1121  return tuple<_Tp&...>(__t...);
1122}
1123
1124template <class... _Tp>
1125inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 tuple<typename __unwrap_ref_decay<_Tp>::type...>
1126make_tuple(_Tp&&... __t) {
1127  return tuple<typename __unwrap_ref_decay<_Tp>::type...>(std::forward<_Tp>(__t)...);
1128}
1129
1130template <class... _Tp>
1131inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 tuple<_Tp&&...> forward_as_tuple(_Tp&&... __t) _NOEXCEPT {
1132  return tuple<_Tp&&...>(std::forward<_Tp>(__t)...);
1133}
1134
1135template <size_t _Ip>
1136struct __tuple_equal {
1137  template <class _Tp, class _Up>
1138  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Up& __y) {
1139    return __tuple_equal<_Ip - 1>()(__x, __y) && std::get<_Ip - 1>(__x) == std::get<_Ip - 1>(__y);
1140  }
1141};
1142
1143template <>
1144struct __tuple_equal<0> {
1145  template <class _Tp, class _Up>
1146  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp&, const _Up&) {
1147    return true;
1148  }
1149};
1150
1151template <class... _Tp, class... _Up>
1152inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
1153operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1154  static_assert(sizeof...(_Tp) == sizeof...(_Up), "Can't compare tuples of different sizes");
1155  return __tuple_equal<sizeof...(_Tp)>()(__x, __y);
1156}
1157
1158#  if _LIBCPP_STD_VER >= 20
1159
1160// operator<=>
1161
1162template <class... _Tp, class... _Up, size_t... _Is>
1163_LIBCPP_HIDE_FROM_ABI constexpr auto
1164__tuple_compare_three_way(const tuple<_Tp...>& __x, const tuple<_Up...>& __y, index_sequence<_Is...>) {
1165  common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...> __result = strong_ordering::equal;
1166  static_cast<void>(
1167      ((__result = std::__synth_three_way(std::get<_Is>(__x), std::get<_Is>(__y)), __result != 0) || ...));
1168  return __result;
1169}
1170
1171template <class... _Tp, class... _Up>
1172  requires(sizeof...(_Tp) == sizeof...(_Up))
1173_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...>
1174operator<=>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1175  return std::__tuple_compare_three_way(__x, __y, index_sequence_for<_Tp...>{});
1176}
1177
1178#  else // _LIBCPP_STD_VER >= 20
1179
1180template <class... _Tp, class... _Up>
1181inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
1182operator!=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1183  return !(__x == __y);
1184}
1185
1186template <size_t _Ip>
1187struct __tuple_less {
1188  template <class _Tp, class _Up>
1189  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Up& __y) {
1190    const size_t __idx = tuple_size<_Tp>::value - _Ip;
1191    if (std::get<__idx>(__x) < std::get<__idx>(__y))
1192      return true;
1193    if (std::get<__idx>(__y) < std::get<__idx>(__x))
1194      return false;
1195    return __tuple_less<_Ip - 1>()(__x, __y);
1196  }
1197};
1198
1199template <>
1200struct __tuple_less<0> {
1201  template <class _Tp, class _Up>
1202  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp&, const _Up&) {
1203    return false;
1204  }
1205};
1206
1207template <class... _Tp, class... _Up>
1208inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
1209operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1210  static_assert(sizeof...(_Tp) == sizeof...(_Up), "Can't compare tuples of different sizes");
1211  return __tuple_less<sizeof...(_Tp)>()(__x, __y);
1212}
1213
1214template <class... _Tp, class... _Up>
1215inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
1216operator>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1217  return __y < __x;
1218}
1219
1220template <class... _Tp, class... _Up>
1221inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
1222operator>=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1223  return !(__x < __y);
1224}
1225
1226template <class... _Tp, class... _Up>
1227inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
1228operator<=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1229  return !(__y < __x);
1230}
1231
1232#  endif // _LIBCPP_STD_VER >= 20
1233
1234// tuple_cat
1235
1236template <class _Tp, class _Up>
1237struct __tuple_cat_type;
1238
1239template <class... _Ttypes, class... _Utypes>
1240struct __tuple_cat_type<tuple<_Ttypes...>, __tuple_types<_Utypes...> > {
1241  typedef _LIBCPP_NODEBUG tuple<_Ttypes..., _Utypes...> type;
1242};
1243
1244template <class _ResultTuple, bool _Is_Tuple0TupleLike, class... _Tuples>
1245struct __tuple_cat_return_1 {};
1246
1247template <class... _Types, class _Tuple0>
1248struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0> {
1249  using type _LIBCPP_NODEBUG =
1250      typename __tuple_cat_type< tuple<_Types...>,
1251                                 typename __make_tuple_types<__remove_cvref_t<_Tuple0> >::type >::type;
1252};
1253
1254template <class... _Types, class _Tuple0, class _Tuple1, class... _Tuples>
1255struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0, _Tuple1, _Tuples...>
1256    : public __tuple_cat_return_1<
1257          typename __tuple_cat_type< tuple<_Types...>,
1258                                     typename __make_tuple_types<__remove_cvref_t<_Tuple0> >::type >::type,
1259          __tuple_like_ext<__libcpp_remove_reference_t<_Tuple1> >::value,
1260          _Tuple1,
1261          _Tuples...> {};
1262
1263template <class... _Tuples>
1264struct __tuple_cat_return;
1265
1266template <class _Tuple0, class... _Tuples>
1267struct __tuple_cat_return<_Tuple0, _Tuples...>
1268    : public __tuple_cat_return_1<tuple<>,
1269                                  __tuple_like_ext<__libcpp_remove_reference_t<_Tuple0> >::value,
1270                                  _Tuple0,
1271                                  _Tuples...> {};
1272
1273template <>
1274struct __tuple_cat_return<> {
1275  typedef _LIBCPP_NODEBUG tuple<> type;
1276};
1277
1278inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 tuple<> tuple_cat() { return tuple<>(); }
1279
1280template <class _Rp, class _Indices, class _Tuple0, class... _Tuples>
1281struct __tuple_cat_return_ref_imp;
1282
1283template <class... _Types, size_t... _I0, class _Tuple0>
1284struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>, _Tuple0> {
1285  typedef _LIBCPP_NODEBUG __libcpp_remove_reference_t<_Tuple0> _T0;
1286  typedef tuple<_Types..., __copy_cvref_t<_Tuple0, typename tuple_element<_I0, _T0>::type>&&...> type;
1287};
1288
1289template <class... _Types, size_t... _I0, class _Tuple0, class _Tuple1, class... _Tuples>
1290struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>, _Tuple0, _Tuple1, _Tuples...>
1291    : public __tuple_cat_return_ref_imp<
1292          tuple<_Types...,
1293                __copy_cvref_t<_Tuple0, typename tuple_element<_I0, __libcpp_remove_reference_t<_Tuple0>>::type>&&...>,
1294          typename __make_tuple_indices<tuple_size<__libcpp_remove_reference_t<_Tuple1> >::value>::type,
1295          _Tuple1,
1296          _Tuples...> {};
1297
1298template <class _Tuple0, class... _Tuples>
1299struct __tuple_cat_return_ref
1300    : public __tuple_cat_return_ref_imp<
1301          tuple<>,
1302          typename __make_tuple_indices< tuple_size<__libcpp_remove_reference_t<_Tuple0> >::value >::type,
1303          _Tuple0,
1304          _Tuples...> {};
1305
1306template <class _Types, class _I0, class _J0>
1307struct __tuple_cat;
1308
1309template <class... _Types, size_t... _I0, size_t... _J0>
1310struct __tuple_cat<tuple<_Types...>, __tuple_indices<_I0...>, __tuple_indices<_J0...> > {
1311  template <class _Tuple0>
1312  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
1313  typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&>::type
1314  operator()(tuple<_Types...> __t, _Tuple0&& __t0) {
1315    (void)__t; // avoid unused parameter warning on GCC when _I0 is empty
1316    return std::forward_as_tuple(
1317        std::forward<_Types>(std::get<_I0>(__t))..., std::get<_J0>(std::forward<_Tuple0>(__t0))...);
1318  }
1319
1320  template <class _Tuple0, class _Tuple1, class... _Tuples>
1321  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
1322  typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&, _Tuple1&&, _Tuples&&...>::type
1323  operator()(tuple<_Types...> __t, _Tuple0&& __t0, _Tuple1&& __t1, _Tuples&&... __tpls) {
1324    (void)__t; // avoid unused parameter warning on GCC when _I0 is empty
1325    typedef _LIBCPP_NODEBUG __libcpp_remove_reference_t<_Tuple0> _T0;
1326    typedef _LIBCPP_NODEBUG __libcpp_remove_reference_t<_Tuple1> _T1;
1327    return __tuple_cat<tuple<_Types..., __copy_cvref_t<_Tuple0, typename tuple_element<_J0, _T0>::type>&&...>,
1328                       typename __make_tuple_indices<sizeof...(_Types) + tuple_size<_T0>::value>::type,
1329                       typename __make_tuple_indices<tuple_size<_T1>::value>::type>()(
1330        std::forward_as_tuple(
1331            std::forward<_Types>(std::get<_I0>(__t))..., std::get<_J0>(std::forward<_Tuple0>(__t0))...),
1332        std::forward<_Tuple1>(__t1),
1333        std::forward<_Tuples>(__tpls)...);
1334  }
1335};
1336
1337template <class _Tuple0, class... _Tuples>
1338inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __tuple_cat_return<_Tuple0, _Tuples...>::type
1339tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls) {
1340  typedef _LIBCPP_NODEBUG __libcpp_remove_reference_t<_Tuple0> _T0;
1341  return __tuple_cat<tuple<>, __tuple_indices<>, typename __make_tuple_indices<tuple_size<_T0>::value>::type>()(
1342      tuple<>(), std::forward<_Tuple0>(__t0), std::forward<_Tuples>(__tpls)...);
1343}
1344
1345template <class... _Tp, class _Alloc>
1346struct _LIBCPP_TEMPLATE_VIS uses_allocator<tuple<_Tp...>, _Alloc> : true_type {};
1347
1348#  if _LIBCPP_STD_VER >= 17
1349#    define _LIBCPP_NOEXCEPT_RETURN(...)                                                                               \
1350      noexcept(noexcept(__VA_ARGS__)) { return __VA_ARGS__; }
1351
1352// The _LIBCPP_NOEXCEPT_RETURN macro breaks formatting.
1353// clang-format off
1354template <class _Fn, class _Tuple, size_t... _Id>
1355inline _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto)
1356__apply_tuple_impl(_Fn&& __f, _Tuple&& __t, __tuple_indices<_Id...>)
1357    _LIBCPP_NOEXCEPT_RETURN(std::__invoke(std::forward<_Fn>(__f), std::get<_Id>(std::forward<_Tuple>(__t))...))
1358
1359template <class _Fn, class _Tuple>
1360inline _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) apply(_Fn&& __f, _Tuple&& __t)
1361    _LIBCPP_NOEXCEPT_RETURN(std::__apply_tuple_impl(
1362        std::forward<_Fn>(__f),
1363        std::forward<_Tuple>(__t),
1364        typename __make_tuple_indices<tuple_size_v<remove_reference_t<_Tuple>>>::type{}))
1365
1366#if _LIBCPP_STD_VER >= 20
1367template <class _Tp, class _Tuple, size_t... _Idx>
1368inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp __make_from_tuple_impl(_Tuple&& __t, __tuple_indices<_Idx...>)
1369  noexcept(noexcept(_Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...)))
1370  requires is_constructible_v<_Tp, decltype(std::get<_Idx>(std::forward<_Tuple>(__t)))...> {
1371  return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...);
1372}
1373#else
1374template <class _Tp, class _Tuple, size_t... _Idx>
1375inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp __make_from_tuple_impl(_Tuple&& __t, __tuple_indices<_Idx...>,
1376    enable_if_t<is_constructible_v<_Tp, decltype(std::get<_Idx>(std::forward<_Tuple>(__t)))...>> * = nullptr)
1377    _LIBCPP_NOEXCEPT_RETURN(_Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...))
1378#endif // _LIBCPP_STD_VER >= 20
1379
1380template <class _Tp, class _Tuple,
1381          class _Seq = typename __make_tuple_indices<tuple_size_v<remove_reference_t<_Tuple>>>::type, class = void>
1382inline constexpr bool __can_make_from_tuple = false;
1383
1384template <class _Tp, class _Tuple, size_t... _Idx>
1385inline constexpr bool __can_make_from_tuple<_Tp, _Tuple, __tuple_indices<_Idx...>,
1386    enable_if_t<is_constructible_v<_Tp, decltype(std::get<_Idx>(std::declval<_Tuple>()))...>>> = true;
1387
1388// Based on LWG3528(https://wg21.link/LWG3528) and http://eel.is/c++draft/description#structure.requirements-9,
1389// the standard allows to impose requirements, we constraint std::make_from_tuple to make std::make_from_tuple
1390// SFINAE friendly and also avoid worse diagnostic messages. We still keep the constraints of std::__make_from_tuple_impl
1391// so that std::__make_from_tuple_impl will have the same advantages when used alone.
1392#if _LIBCPP_STD_VER >= 20
1393template <class _Tp, class _Tuple>
1394  requires __can_make_from_tuple<_Tp, _Tuple> // strengthen
1395#else
1396template <class _Tp, class _Tuple, class = enable_if_t<__can_make_from_tuple<_Tp, _Tuple>>> // strengthen
1397#endif // _LIBCPP_STD_VER >= 20
1398inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp make_from_tuple(_Tuple&& __t)
1399    _LIBCPP_NOEXCEPT_RETURN(std::__make_from_tuple_impl<_Tp>(
1400        std::forward<_Tuple>(__t), typename __make_tuple_indices<tuple_size_v<remove_reference_t<_Tuple>>>::type{}))
1401#    undef _LIBCPP_NOEXCEPT_RETURN
1402
1403#  endif // _LIBCPP_STD_VER >= 17
1404
1405#endif // !defined(_LIBCPP_CXX03_LANG)
1406
1407_LIBCPP_END_NAMESPACE_STD
1408
1409_LIBCPP_POP_MACROS
1410
1411// clang-format on
1412
1413#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1414#  include <cstddef>
1415#  include <exception>
1416#  include <iosfwd>
1417#  include <new>
1418#  include <type_traits>
1419#  include <typeinfo>
1420#  include <utility>
1421#endif
1422
1423#endif // _LIBCPP_TUPLE
1424