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