xref: /freebsd/contrib/llvm-project/libcxx/include/tuple (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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/*
14    tuple synopsis
15
16namespace std
17{
18
19template <class... T>
20class tuple {
21public:
22    explicit(see-below) constexpr tuple();
23    explicit(see-below) tuple(const T&...);  // constexpr in C++14
24    template <class... U>
25        explicit(see-below) tuple(U&&...);  // constexpr in C++14
26    tuple(const tuple&) = default;
27    tuple(tuple&&) = default;
28
29    template<class... UTypes>
30        constexpr explicit(see-below) tuple(tuple<UTypes...>&);  // C++23
31    template <class... U>
32        explicit(see-below) tuple(const tuple<U...>&);  // constexpr in C++14
33    template <class... U>
34        explicit(see-below) tuple(tuple<U...>&&);  // constexpr in C++14
35    template<class... UTypes>
36        constexpr explicit(see-below) tuple(const tuple<UTypes...>&&); // C++23
37
38    template<class U1, class U2>
39        constexpr explicit(see-below) tuple(pair<U1, U2>&);  // iff sizeof...(Types) == 2 // C++23
40    template <class U1, class U2>
41        explicit(see-below) tuple(const pair<U1, U2>&); // iff sizeof...(T) == 2 // constexpr in C++14
42    template <class U1, class U2>
43        explicit(see-below) tuple(pair<U1, U2>&&); // iff sizeof...(T) == 2  // constexpr in C++14
44    template<class U1, class U2>
45        constexpr explicit(see-below) tuple(const pair<U1, U2>&&);  // iff sizeof...(Types) == 2 // C++23
46
47    // allocator-extended constructors
48    template <class Alloc>
49        tuple(allocator_arg_t, const Alloc& a);
50    template <class Alloc>
51        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, const T&...);          // constexpr in C++20
52    template <class Alloc, class... U>
53        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, U&&...);               // constexpr in C++20
54    template <class Alloc>
55        tuple(allocator_arg_t, const Alloc& a, const tuple&);                             // constexpr in C++20
56    template <class Alloc>
57        tuple(allocator_arg_t, const Alloc& a, tuple&&);                                  // constexpr in C++20
58    template<class Alloc, class... UTypes>
59        constexpr explicit(see-below)
60          tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&);                      // C++23
61    template <class Alloc, class... U>
62        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, const tuple<U...>&);   // constexpr in C++20
63    template <class Alloc, class... U>
64        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, tuple<U...>&&);        // constexpr in C++20
65    template<class Alloc, class... UTypes>
66        constexpr explicit(see-below)
67          tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&&);               // C++23
68    template<class Alloc, class U1, class U2>
69        constexpr explicit(see-below)
70          tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&);                          // C++23
71    template <class Alloc, class U1, class U2>
72        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&);  // constexpr in C++20
73    template <class Alloc, class U1, class U2>
74        explicit(see-below) tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&&);       // constexpr in C++20
75    template<class Alloc, class U1, class U2>
76        constexpr explicit(see-below)
77          tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&&);                   // C++23
78
79    tuple& operator=(const tuple&);                                                       // constexpr in C++20
80    constexpr const tuple& operator=(const tuple&) const;                                 // C++23
81    tuple& operator=(tuple&&) noexcept(is_nothrow_move_assignable_v<T> && ...);           // constexpr in C++20
82    constexpr const tuple& operator=(tuple&&) const;                                      // C++23
83    template <class... U>
84        tuple& operator=(const tuple<U...>&);                                             // constexpr in C++20
85    template<class... UTypes>
86        constexpr const tuple& operator=(const tuple<UTypes...>&) const;                  // C++23
87    template <class... U>
88        tuple& operator=(tuple<U...>&&);                                                  // constexpr in C++20
89    template<class... UTypes>
90        constexpr const tuple& operator=(tuple<UTypes...>&&) const;                       // C++23
91    template <class U1, class U2>
92        tuple& operator=(const pair<U1, U2>&); // iff sizeof...(T) == 2                   // constexpr in C++20
93    template<class U1, class U2>
94        constexpr const tuple& operator=(const pair<U1, U2>&) const;   // iff sizeof...(Types) == 2 // C++23
95    template <class U1, class U2>
96        tuple& operator=(pair<U1, U2>&&); // iff sizeof...(T) == 2                        // constexpr in C++20
97    template<class U1, class U2>
98        constexpr const tuple& operator=(pair<U1, U2>&&) const;  // iff sizeof...(Types) == 2 // C++23
99
100    template<class U, size_t N>
101        tuple& operator=(array<U, N> const&) // iff sizeof...(T) == N, EXTENSION
102    template<class U, size_t N>
103        tuple& operator=(array<U, N>&&) // iff sizeof...(T) == N, EXTENSION
104
105    void swap(tuple&) noexcept(AND(swap(declval<T&>(), declval<T&>())...));               // constexpr in C++20
106    constexpr void swap(const tuple&) const noexcept(see-below);                          // C++23
107};
108
109
110template<class... TTypes, class... UTypes, template<class> class TQual, template<class> class UQual> // since C++23
111  requires requires { typename tuple<common_reference_t<TQual<TTypes>, UQual<UTypes>>...>; }
112struct basic_common_reference<tuple<TTypes...>, tuple<UTypes...>, TQual, UQual> {
113  using type = tuple<common_reference_t<TQual<TTypes>, UQual<UTypes>>...>;
114};
115
116template<class... TTypes, class... UTypes>                                // since C++23
117  requires requires { typename tuple<common_type_t<TTypes, UTypes>...>; }
118struct common_type<tuple<TTypes...>, tuple<UTypes...>> {
119  using type = tuple<common_type_t<TTypes, UTypes>...>;
120};
121
122template <class ...T>
123tuple(T...) -> tuple<T...>;                                         // since C++17
124template <class T1, class T2>
125tuple(pair<T1, T2>) -> tuple<T1, T2>;                               // since C++17
126template <class Alloc, class ...T>
127tuple(allocator_arg_t, Alloc, T...) -> tuple<T...>;                 // since C++17
128template <class Alloc, class T1, class T2>
129tuple(allocator_arg_t, Alloc, pair<T1, T2>) -> tuple<T1, T2>;       // since C++17
130template <class Alloc, class ...T>
131tuple(allocator_arg_t, Alloc, tuple<T...>) -> tuple<T...>;          // since C++17
132
133inline constexpr unspecified ignore;
134
135template <class... T> tuple<V...>  make_tuple(T&&...); // constexpr in C++14
136template <class... T> tuple<ATypes...> forward_as_tuple(T&&...) noexcept; // constexpr in C++14
137template <class... T> tuple<T&...> tie(T&...) noexcept; // constexpr in C++14
138template <class... Tuples> tuple<CTypes...> tuple_cat(Tuples&&... tpls); // constexpr in C++14
139
140// [tuple.apply], calling a function with a tuple of arguments:
141template <class F, class Tuple>
142  constexpr decltype(auto) apply(F&& f, Tuple&& t); // C++17
143template <class T, class Tuple>
144  constexpr T make_from_tuple(Tuple&& t); // C++17
145
146// 20.4.1.4, tuple helper classes:
147template <class T> struct tuple_size; // undefined
148template <class... T> struct tuple_size<tuple<T...>>;
149template <class T>
150 inline constexpr size_t tuple_size_v = tuple_size<T>::value; // C++17
151template <size_t I, class T> struct tuple_element; // undefined
152template <size_t I, class... T> struct tuple_element<I, tuple<T...>>;
153template <size_t I, class T>
154  using tuple_element_t = typename tuple_element <I, T>::type; // C++14
155
156// 20.4.1.5, element access:
157template <size_t I, class... T>
158    typename tuple_element<I, tuple<T...>>::type&
159    get(tuple<T...>&) noexcept; // constexpr in C++14
160template <size_t I, class... T>
161    const typename tuple_element<I, tuple<T...>>::type&
162    get(const tuple<T...>&) noexcept; // constexpr in C++14
163template <size_t I, class... T>
164    typename tuple_element<I, tuple<T...>>::type&&
165    get(tuple<T...>&&) noexcept; // constexpr in C++14
166template <size_t I, class... T>
167    const typename tuple_element<I, tuple<T...>>::type&&
168    get(const tuple<T...>&&) noexcept; // constexpr in C++14
169
170template <class T1, class... T>
171    constexpr T1& get(tuple<T...>&) noexcept;  // C++14
172template <class T1, class... T>
173    constexpr const T1& get(const tuple<T...>&) noexcept;   // C++14
174template <class T1, class... T>
175    constexpr T1&& get(tuple<T...>&&) noexcept;   // C++14
176template <class T1, class... T>
177    constexpr const T1&& get(const tuple<T...>&&) noexcept;   // C++14
178
179// 20.4.1.6, relational operators:
180template<class... T, class... U> bool operator==(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14
181template<class... T, class... U> bool operator<(const tuple<T...>&, const tuple<U...>&);  // constexpr in C++14, removed in C++20
182template<class... T, class... U> bool operator!=(const tuple<T...>&, const tuple<U...>&); // constexpr in C++14, removed in C++20
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>
187  constexpr common_comparison_category_t<synth-three-way-result<T, U>...>
188    operator<=>(const tuple<T...>&, const tuple<U...>&);                                  // since C++20
189
190template <class... Types, class Alloc>
191  struct uses_allocator<tuple<Types...>, Alloc>;
192
193template <class... Types>
194  void
195  swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(noexcept(x.swap(y)));
196
197template <class... Types>
198  constexpr void swap(const tuple<Types...>& x, const tuple<Types...>& y) noexcept(see-below);   // C++23
199
200}  // std
201
202*/
203
204#include <__assert> // all public C++ headers provide the assertion handler
205#include <__compare/common_comparison_category.h>
206#include <__compare/synth_three_way.h>
207#include <__config>
208#include <__functional/unwrap_ref.h>
209#include <__memory/allocator_arg_t.h>
210#include <__memory/uses_allocator.h>
211#include <__tuple>
212#include <__utility/forward.h>
213#include <__utility/integer_sequence.h>
214#include <__utility/move.h>
215#include <__utility/pair.h>
216#include <__utility/piecewise_construct.h>
217#include <__utility/swap.h>
218#include <cstddef>
219#include <type_traits>
220#include <version>
221
222#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
223#  include <exception>
224#  include <iosfwd>
225#  include <new>
226#  include <typeinfo>
227#  include <utility>
228#endif
229
230// standard-mandated includes
231#include <compare>
232
233#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
234#  pragma GCC system_header
235#endif
236
237_LIBCPP_BEGIN_NAMESPACE_STD
238
239#ifndef _LIBCPP_CXX03_LANG
240
241
242// __tuple_leaf
243
244template <size_t _Ip, class _Hp,
245          bool=is_empty<_Hp>::value && !__libcpp_is_final<_Hp>::value
246         >
247class __tuple_leaf;
248
249template <size_t _Ip, class _Hp, bool _Ep>
250inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
251void swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y)
252    _NOEXCEPT_(__is_nothrow_swappable<_Hp>::value)
253{
254    swap(__x.get(), __y.get());
255}
256
257template <size_t _Ip, class _Hp, bool _Ep>
258_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
259void swap(const __tuple_leaf<_Ip, _Hp, _Ep>& __x, const __tuple_leaf<_Ip, _Hp, _Ep>& __y)
260     _NOEXCEPT_(__is_nothrow_swappable<const _Hp>::value) {
261  swap(__x.get(), __y.get());
262}
263
264template <size_t _Ip, class _Hp, bool>
265class __tuple_leaf
266{
267    _Hp __value_;
268
269    template <class _Tp>
270    static constexpr bool __can_bind_reference() {
271#if __has_keyword(__reference_binds_to_temporary)
272      return !__reference_binds_to_temporary(_Hp, _Tp);
273#else
274      return true;
275#endif
276    }
277
278    _LIBCPP_CONSTEXPR_AFTER_CXX11
279    __tuple_leaf& operator=(const __tuple_leaf&);
280public:
281    _LIBCPP_INLINE_VISIBILITY constexpr __tuple_leaf()
282             _NOEXCEPT_(is_nothrow_default_constructible<_Hp>::value) : __value_()
283       {static_assert(!is_reference<_Hp>::value,
284              "Attempted to default construct a reference element in a tuple");}
285
286    template <class _Alloc>
287        _LIBCPP_INLINE_VISIBILITY constexpr
288        __tuple_leaf(integral_constant<int, 0>, const _Alloc&)
289            : __value_()
290        {static_assert(!is_reference<_Hp>::value,
291              "Attempted to default construct a reference element in a tuple");}
292
293    template <class _Alloc>
294        _LIBCPP_INLINE_VISIBILITY constexpr
295        __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
296            : __value_(allocator_arg_t(), __a)
297        {static_assert(!is_reference<_Hp>::value,
298              "Attempted to default construct a reference element in a tuple");}
299
300    template <class _Alloc>
301        _LIBCPP_INLINE_VISIBILITY constexpr
302        __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
303            : __value_(__a)
304        {static_assert(!is_reference<_Hp>::value,
305              "Attempted to default construct a reference element in a tuple");}
306
307    template <class _Tp,
308              class = __enable_if_t<
309                  _And<
310                      _IsNotSame<__uncvref_t<_Tp>, __tuple_leaf>,
311                      is_constructible<_Hp, _Tp>
312                    >::value
313                >
314            >
315        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
316        explicit __tuple_leaf(_Tp&& __t) _NOEXCEPT_((is_nothrow_constructible<_Hp, _Tp>::value))
317            : __value_(_VSTD::forward<_Tp>(__t))
318        {static_assert(__can_bind_reference<_Tp&&>(),
319       "Attempted construction of reference element binds to a temporary whose lifetime has ended");}
320
321    template <class _Tp, class _Alloc>
322        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
323        explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
324            : __value_(_VSTD::forward<_Tp>(__t))
325        {static_assert(__can_bind_reference<_Tp&&>(),
326       "Attempted construction of reference element binds to a temporary whose lifetime has ended");}
327
328    template <class _Tp, class _Alloc>
329        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
330        explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
331            : __value_(allocator_arg_t(), __a, _VSTD::forward<_Tp>(__t))
332        {static_assert(!is_reference<_Hp>::value,
333            "Attempted to uses-allocator construct a reference element in a tuple");}
334
335    template <class _Tp, class _Alloc>
336        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
337        explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
338            : __value_(_VSTD::forward<_Tp>(__t), __a)
339        {static_assert(!is_reference<_Hp>::value,
340           "Attempted to uses-allocator construct a reference element in a tuple");}
341
342    __tuple_leaf(const __tuple_leaf& __t) = default;
343    __tuple_leaf(__tuple_leaf&& __t) = default;
344
345    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
346    int swap(__tuple_leaf& __t) _NOEXCEPT_(__is_nothrow_swappable<__tuple_leaf>::value)
347    {
348        _VSTD::swap(*this, __t);
349        return 0;
350    }
351
352    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
353    int swap(const __tuple_leaf& __t) const _NOEXCEPT_(__is_nothrow_swappable<const __tuple_leaf>::value) {
354        _VSTD::swap(*this, __t);
355        return 0;
356    }
357
358    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11       _Hp& get()       _NOEXCEPT {return __value_;}
359    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const _Hp& get() const _NOEXCEPT {return __value_;}
360};
361
362template <size_t _Ip, class _Hp>
363class __tuple_leaf<_Ip, _Hp, true>
364    : private _Hp
365{
366    _LIBCPP_CONSTEXPR_AFTER_CXX11
367    __tuple_leaf& operator=(const __tuple_leaf&);
368public:
369    _LIBCPP_INLINE_VISIBILITY constexpr __tuple_leaf()
370             _NOEXCEPT_(is_nothrow_default_constructible<_Hp>::value) {}
371
372    template <class _Alloc>
373        _LIBCPP_INLINE_VISIBILITY constexpr
374        __tuple_leaf(integral_constant<int, 0>, const _Alloc&) {}
375
376    template <class _Alloc>
377        _LIBCPP_INLINE_VISIBILITY constexpr
378        __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
379            : _Hp(allocator_arg_t(), __a) {}
380
381    template <class _Alloc>
382        _LIBCPP_INLINE_VISIBILITY constexpr
383        __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
384            : _Hp(__a) {}
385
386    template <class _Tp,
387              class = __enable_if_t<
388                  _And<
389                    _IsNotSame<__uncvref_t<_Tp>, __tuple_leaf>,
390                    is_constructible<_Hp, _Tp>
391                  >::value
392                >
393            >
394        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
395        explicit __tuple_leaf(_Tp&& __t) _NOEXCEPT_((is_nothrow_constructible<_Hp, _Tp>::value))
396            : _Hp(_VSTD::forward<_Tp>(__t)) {}
397
398    template <class _Tp, class _Alloc>
399        _LIBCPP_INLINE_VISIBILITY constexpr
400        explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
401            : _Hp(_VSTD::forward<_Tp>(__t)) {}
402
403    template <class _Tp, class _Alloc>
404        _LIBCPP_INLINE_VISIBILITY constexpr
405        explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
406            : _Hp(allocator_arg_t(), __a, _VSTD::forward<_Tp>(__t)) {}
407
408    template <class _Tp, class _Alloc>
409        _LIBCPP_INLINE_VISIBILITY constexpr
410        explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
411            : _Hp(_VSTD::forward<_Tp>(__t), __a) {}
412
413    __tuple_leaf(__tuple_leaf const &) = default;
414    __tuple_leaf(__tuple_leaf &&) = default;
415
416    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
417    int
418    swap(__tuple_leaf& __t) _NOEXCEPT_(__is_nothrow_swappable<__tuple_leaf>::value)
419    {
420        _VSTD::swap(*this, __t);
421        return 0;
422    }
423
424    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
425    int swap(const __tuple_leaf& __rhs) const _NOEXCEPT_(__is_nothrow_swappable<const __tuple_leaf>::value) {
426        _VSTD::swap(*this, __rhs);
427        return 0;
428    }
429
430    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11       _Hp& get()       _NOEXCEPT {return static_cast<_Hp&>(*this);}
431    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const _Hp& get() const _NOEXCEPT {return static_cast<const _Hp&>(*this);}
432};
433
434template <class ..._Tp>
435_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
436void __swallow(_Tp&&...) _NOEXCEPT {}
437
438template <class _Tp>
439struct __all_default_constructible;
440
441template <class ..._Tp>
442struct __all_default_constructible<__tuple_types<_Tp...>>
443    : __all<is_default_constructible<_Tp>::value...>
444{ };
445
446// __tuple_impl
447
448template<class _Indx, class ..._Tp> struct __tuple_impl;
449
450template<size_t ..._Indx, class ..._Tp>
451struct _LIBCPP_DECLSPEC_EMPTY_BASES __tuple_impl<__tuple_indices<_Indx...>, _Tp...>
452    : public __tuple_leaf<_Indx, _Tp>...
453{
454    _LIBCPP_INLINE_VISIBILITY
455    constexpr __tuple_impl()
456        _NOEXCEPT_(__all<is_nothrow_default_constructible<_Tp>::value...>::value) {}
457
458    template <size_t ..._Uf, class ..._Tf,
459              size_t ..._Ul, class ..._Tl, class ..._Up>
460        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
461        explicit
462        __tuple_impl(__tuple_indices<_Uf...>, __tuple_types<_Tf...>,
463                     __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
464                     _Up&&... __u)
465                     _NOEXCEPT_((__all<is_nothrow_constructible<_Tf, _Up>::value...>::value &&
466                                 __all<is_nothrow_default_constructible<_Tl>::value...>::value)) :
467            __tuple_leaf<_Uf, _Tf>(_VSTD::forward<_Up>(__u))...,
468            __tuple_leaf<_Ul, _Tl>()...
469            {}
470
471    template <class _Alloc, size_t ..._Uf, class ..._Tf,
472              size_t ..._Ul, class ..._Tl, class ..._Up>
473        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
474        explicit
475        __tuple_impl(allocator_arg_t, const _Alloc& __a,
476                     __tuple_indices<_Uf...>, __tuple_types<_Tf...>,
477                     __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
478                     _Up&&... __u) :
479            __tuple_leaf<_Uf, _Tf>(__uses_alloc_ctor<_Tf, _Alloc, _Up>(), __a,
480            _VSTD::forward<_Up>(__u))...,
481            __tuple_leaf<_Ul, _Tl>(__uses_alloc_ctor<_Tl, _Alloc>(), __a)...
482            {}
483
484    template <class _Tuple,
485              class = typename enable_if
486                      <
487                         __tuple_constructible<_Tuple, tuple<_Tp...> >::value
488                      >::type
489             >
490        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
491        __tuple_impl(_Tuple&& __t) _NOEXCEPT_((__all<is_nothrow_constructible<_Tp, typename tuple_element<_Indx,
492                                       typename __make_tuple_types<_Tuple>::type>::type>::value...>::value))
493            : __tuple_leaf<_Indx, _Tp>(_VSTD::forward<typename tuple_element<_Indx,
494                                       typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...
495            {}
496
497    template <class _Alloc, class _Tuple,
498              class = typename enable_if
499                      <
500                         __tuple_constructible<_Tuple, tuple<_Tp...> >::value
501                      >::type
502             >
503        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
504        __tuple_impl(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
505            : __tuple_leaf<_Indx, _Tp>(__uses_alloc_ctor<_Tp, _Alloc, typename tuple_element<_Indx,
506                                       typename __make_tuple_types<_Tuple>::type>::type>(), __a,
507                                       _VSTD::forward<typename tuple_element<_Indx,
508                                       typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...
509            {}
510
511    __tuple_impl(const __tuple_impl&) = default;
512    __tuple_impl(__tuple_impl&&) = default;
513
514    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
515    void swap(__tuple_impl& __t)
516        _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
517    {
518        _VSTD::__swallow(__tuple_leaf<_Indx, _Tp>::swap(static_cast<__tuple_leaf<_Indx, _Tp>&>(__t))...);
519    }
520
521    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
522    void swap(const __tuple_impl& __t) const
523        _NOEXCEPT_(__all<__is_nothrow_swappable<const _Tp>::value...>::value)
524    {
525        _VSTD::__swallow(__tuple_leaf<_Indx, _Tp>::swap(static_cast<const __tuple_leaf<_Indx, _Tp>&>(__t))...);
526    }
527};
528
529template<class _Dest, class _Source, size_t ..._Np>
530_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
531void __memberwise_copy_assign(_Dest& __dest, _Source const& __source, __tuple_indices<_Np...>) {
532    _VSTD::__swallow(((_VSTD::get<_Np>(__dest) = _VSTD::get<_Np>(__source)), void(), 0)...);
533}
534
535template<class _Dest, class _Source, class ..._Up, size_t ..._Np>
536_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
537void __memberwise_forward_assign(_Dest& __dest, _Source&& __source, __tuple_types<_Up...>, __tuple_indices<_Np...>) {
538    _VSTD::__swallow(((
539        _VSTD::get<_Np>(__dest) = _VSTD::forward<_Up>(_VSTD::get<_Np>(__source))
540    ), void(), 0)...);
541}
542
543template <class ..._Tp>
544class _LIBCPP_TEMPLATE_VIS tuple
545{
546    typedef __tuple_impl<typename __make_tuple_indices<sizeof...(_Tp)>::type, _Tp...> _BaseT;
547
548    _BaseT __base_;
549
550    template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
551        typename tuple_element<_Jp, tuple<_Up...> >::type& get(tuple<_Up...>&) _NOEXCEPT;
552    template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
553        const typename tuple_element<_Jp, tuple<_Up...> >::type& get(const tuple<_Up...>&) _NOEXCEPT;
554    template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
555        typename tuple_element<_Jp, tuple<_Up...> >::type&& get(tuple<_Up...>&&) _NOEXCEPT;
556    template <size_t _Jp, class ..._Up> friend _LIBCPP_CONSTEXPR_AFTER_CXX11
557        const typename tuple_element<_Jp, tuple<_Up...> >::type&& get(const tuple<_Up...>&&) _NOEXCEPT;
558public:
559    // [tuple.cnstr]
560
561    // tuple() constructors (including allocator_arg_t variants)
562    template <template<class...> class _IsImpDefault = __is_implicitly_default_constructible, __enable_if_t<
563        _And<
564            _IsImpDefault<_Tp>... // explicit check
565        >::value
566    , int> = 0>
567    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
568    tuple()
569        _NOEXCEPT_(_And<is_nothrow_default_constructible<_Tp>...>::value)
570    { }
571
572    template <template<class...> class _IsImpDefault = __is_implicitly_default_constructible,
573              template<class...> class _IsDefault = is_default_constructible, __enable_if_t<
574        _And<
575            _IsDefault<_Tp>...,
576            _Not<_Lazy<_And, _IsImpDefault<_Tp>...> > // explicit check
577        >::value
578    , int> = 0>
579    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
580    explicit tuple()
581        _NOEXCEPT_(_And<is_nothrow_default_constructible<_Tp>...>::value)
582    { }
583
584    template <class _Alloc, template<class...> class _IsImpDefault = __is_implicitly_default_constructible, __enable_if_t<
585        _And<
586            _IsImpDefault<_Tp>... // explicit check
587        >::value
588    , int> = 0>
589    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
590    tuple(allocator_arg_t, _Alloc const& __a)
591      : __base_(allocator_arg_t(), __a,
592                    __tuple_indices<>(), __tuple_types<>(),
593                    typename __make_tuple_indices<sizeof...(_Tp), 0>::type(),
594                    __tuple_types<_Tp...>()) {}
595
596    template <class _Alloc,
597              template<class...> class _IsImpDefault = __is_implicitly_default_constructible,
598              template<class...> class _IsDefault = is_default_constructible, __enable_if_t<
599        _And<
600            _IsDefault<_Tp>...,
601            _Not<_Lazy<_And, _IsImpDefault<_Tp>...> > // explicit check
602        >::value
603    , int> = 0>
604    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
605    explicit tuple(allocator_arg_t, _Alloc const& __a)
606      : __base_(allocator_arg_t(), __a,
607                    __tuple_indices<>(), __tuple_types<>(),
608                    typename __make_tuple_indices<sizeof...(_Tp), 0>::type(),
609                    __tuple_types<_Tp...>()) {}
610
611    // tuple(const T&...) constructors (including allocator_arg_t variants)
612    template <template<class...> class _And = _And, __enable_if_t<
613        _And<
614            _BoolConstant<sizeof...(_Tp) >= 1>,
615            is_copy_constructible<_Tp>...,
616            is_convertible<const _Tp&, _Tp>... // explicit check
617        >::value
618    , int> = 0>
619    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
620    tuple(const _Tp& ... __t)
621        _NOEXCEPT_(_And<is_nothrow_copy_constructible<_Tp>...>::value)
622        : __base_(typename __make_tuple_indices<sizeof...(_Tp)>::type(),
623                typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
624                typename __make_tuple_indices<0>::type(),
625                typename __make_tuple_types<tuple, 0>::type(),
626                __t...
627               ) {}
628
629    template <template<class...> class _And = _And, __enable_if_t<
630        _And<
631            _BoolConstant<sizeof...(_Tp) >= 1>,
632            is_copy_constructible<_Tp>...,
633            _Not<_Lazy<_And, is_convertible<const _Tp&, _Tp>...> > // explicit check
634        >::value
635    , int> = 0>
636    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
637    explicit tuple(const _Tp& ... __t)
638        _NOEXCEPT_(_And<is_nothrow_copy_constructible<_Tp>...>::value)
639        : __base_(typename __make_tuple_indices<sizeof...(_Tp)>::type(),
640                typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
641                typename __make_tuple_indices<0>::type(),
642                typename __make_tuple_types<tuple, 0>::type(),
643                __t...
644               ) {}
645
646    template <class _Alloc, template<class...> class _And = _And, __enable_if_t<
647        _And<
648            _BoolConstant<sizeof...(_Tp) >= 1>,
649            is_copy_constructible<_Tp>...,
650            is_convertible<const _Tp&, _Tp>... // explicit check
651        >::value
652    , int> = 0>
653    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
654    tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
655        : __base_(allocator_arg_t(), __a,
656                typename __make_tuple_indices<sizeof...(_Tp)>::type(),
657                typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
658                typename __make_tuple_indices<0>::type(),
659                typename __make_tuple_types<tuple, 0>::type(),
660                __t...
661               ) {}
662
663    template <class _Alloc, template<class...> class _And = _And, __enable_if_t<
664        _And<
665            _BoolConstant<sizeof...(_Tp) >= 1>,
666            is_copy_constructible<_Tp>...,
667            _Not<_Lazy<_And, is_convertible<const _Tp&, _Tp>...> > // explicit check
668        >::value
669    , int> = 0>
670    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
671    explicit tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
672        : __base_(allocator_arg_t(), __a,
673                typename __make_tuple_indices<sizeof...(_Tp)>::type(),
674                typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
675                typename __make_tuple_indices<0>::type(),
676                typename __make_tuple_types<tuple, 0>::type(),
677                __t...
678               ) {}
679
680    // tuple(U&& ...) constructors (including allocator_arg_t variants)
681    template <class ..._Up> struct _IsThisTuple : false_type { };
682    template <class _Up> struct _IsThisTuple<_Up> : is_same<__uncvref_t<_Up>, tuple> { };
683
684    template <class ..._Up>
685    struct _EnableUTypesCtor : _And<
686        _BoolConstant<sizeof...(_Tp) >= 1>,
687        _Not<_IsThisTuple<_Up...> >, // extension to allow mis-behaved user constructors
688        is_constructible<_Tp, _Up>...
689    > { };
690
691    template <class ..._Up, __enable_if_t<
692        _And<
693            _BoolConstant<sizeof...(_Up) == sizeof...(_Tp)>,
694            _EnableUTypesCtor<_Up...>,
695            is_convertible<_Up, _Tp>... // explicit check
696        >::value
697    , int> = 0>
698    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
699    tuple(_Up&&... __u)
700        _NOEXCEPT_((_And<is_nothrow_constructible<_Tp, _Up>...>::value))
701        : __base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
702                    typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
703                    typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
704                    typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
705                    _VSTD::forward<_Up>(__u)...) {}
706
707    template <class ..._Up, __enable_if_t<
708        _And<
709            _BoolConstant<sizeof...(_Up) == sizeof...(_Tp)>,
710            _EnableUTypesCtor<_Up...>,
711            _Not<_Lazy<_And, is_convertible<_Up, _Tp>...> > // explicit check
712        >::value
713    , int> = 0>
714    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715    explicit tuple(_Up&&... __u)
716        _NOEXCEPT_((_And<is_nothrow_constructible<_Tp, _Up>...>::value))
717        : __base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
718                    typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
719                    typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
720                    typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
721                    _VSTD::forward<_Up>(__u)...) {}
722
723    template <class _Alloc, class ..._Up, __enable_if_t<
724        _And<
725            _BoolConstant<sizeof...(_Up) == sizeof...(_Tp)>,
726            _EnableUTypesCtor<_Up...>,
727            is_convertible<_Up, _Tp>... // explicit check
728        >::value
729    , int> = 0>
730    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
731    tuple(allocator_arg_t, const _Alloc& __a, _Up&&... __u)
732        : __base_(allocator_arg_t(), __a,
733                    typename __make_tuple_indices<sizeof...(_Up)>::type(),
734                    typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
735                    typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
736                    typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
737                    _VSTD::forward<_Up>(__u)...) {}
738
739    template <class _Alloc, class ..._Up, __enable_if_t<
740        _And<
741            _BoolConstant<sizeof...(_Up) == sizeof...(_Tp)>,
742            _EnableUTypesCtor<_Up...>,
743            _Not<_Lazy<_And, is_convertible<_Up, _Tp>...> > // explicit check
744        >::value
745    , int> = 0>
746    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
747    explicit tuple(allocator_arg_t, const _Alloc& __a, _Up&&... __u)
748        : __base_(allocator_arg_t(), __a,
749                    typename __make_tuple_indices<sizeof...(_Up)>::type(),
750                    typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
751                    typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
752                    typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
753                    _VSTD::forward<_Up>(__u)...) {}
754
755    // Copy and move constructors (including the allocator_arg_t variants)
756    tuple(const tuple&) = default;
757    tuple(tuple&&) = default;
758
759    template <class _Alloc, template<class...> class _And = _And, __enable_if_t<
760        _And<is_copy_constructible<_Tp>...>::value
761    , int> = 0>
762    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
763    tuple(allocator_arg_t, const _Alloc& __alloc, const tuple& __t)
764        : __base_(allocator_arg_t(), __alloc, __t)
765    { }
766
767    template <class _Alloc, template<class...> class _And = _And, __enable_if_t<
768        _And<is_move_constructible<_Tp>...>::value
769    , int> = 0>
770    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
771    tuple(allocator_arg_t, const _Alloc& __alloc, tuple&& __t)
772        : __base_(allocator_arg_t(), __alloc, _VSTD::move(__t))
773    { }
774
775    // tuple(const tuple<U...>&) constructors (including allocator_arg_t variants)
776
777    template <class _OtherTuple, class _DecayedOtherTuple = __uncvref_t<_OtherTuple>, class = void>
778    struct _EnableCtorFromUTypesTuple : false_type {};
779
780    template <class _OtherTuple, class... _Up>
781    struct _EnableCtorFromUTypesTuple<_OtherTuple, tuple<_Up...>,
782              // the length of the packs needs to checked first otherwise the 2 packs cannot be expanded simultaneously below
783               __enable_if_t<sizeof...(_Up) == sizeof...(_Tp)>> : _And<
784        // the two conditions below are not in spec. The purpose is to disable the UTypes Ctor when copy/move Ctor can work.
785        // Otherwise, is_constructible can trigger hard error in those cases https://godbolt.org/z/M94cGdKcE
786        _Not<is_same<_OtherTuple, const tuple&> >,
787        _Not<is_same<_OtherTuple, tuple&&> >,
788        is_constructible<_Tp, __copy_cvref_t<_OtherTuple, _Up> >...,
789        _Lazy<_Or, _BoolConstant<sizeof...(_Tp) != 1>,
790            // _Tp and _Up are 1-element packs - the pack expansions look
791            // weird to avoid tripping up the type traits in degenerate cases
792            _Lazy<_And,
793                _Not<is_same<_Tp, _Up> >...,
794                _Not<is_convertible<_OtherTuple, _Tp> >...,
795                _Not<is_constructible<_Tp, _OtherTuple> >...
796            >
797        >
798    > {};
799
800    template <class ..._Up, __enable_if_t<
801        _And<
802            _EnableCtorFromUTypesTuple<const tuple<_Up...>&>,
803            is_convertible<const _Up&, _Tp>... // explicit check
804        >::value
805    , int> = 0>
806    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
807    tuple(const tuple<_Up...>& __t)
808        _NOEXCEPT_((_And<is_nothrow_constructible<_Tp, const _Up&>...>::value))
809        : __base_(__t)
810    { }
811
812    template <class ..._Up, __enable_if_t<
813        _And<
814            _EnableCtorFromUTypesTuple<const tuple<_Up...>&>,
815            _Not<_Lazy<_And, is_convertible<const _Up&, _Tp>...> > // explicit check
816        >::value
817    , int> = 0>
818    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
819    explicit tuple(const tuple<_Up...>& __t)
820        _NOEXCEPT_((_And<is_nothrow_constructible<_Tp, const _Up&>...>::value))
821        : __base_(__t)
822    { }
823
824    template <class ..._Up, class _Alloc, __enable_if_t<
825        _And<
826            _EnableCtorFromUTypesTuple<const tuple<_Up...>&>,
827            is_convertible<const _Up&, _Tp>... // explicit check
828        >::value
829    , int> = 0>
830    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
831    tuple(allocator_arg_t, const _Alloc& __a, const tuple<_Up...>& __t)
832        : __base_(allocator_arg_t(), __a, __t)
833    { }
834
835    template <class ..._Up, class _Alloc, __enable_if_t<
836        _And<
837            _EnableCtorFromUTypesTuple<const tuple<_Up...>&>,
838            _Not<_Lazy<_And, is_convertible<const _Up&, _Tp>...> > // explicit check
839        >::value
840    , int> = 0>
841    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
842    explicit tuple(allocator_arg_t, const _Alloc& __a, const tuple<_Up...>& __t)
843        : __base_(allocator_arg_t(), __a, __t)
844    { }
845
846#if _LIBCPP_STD_VER > 20
847    // tuple(tuple<U...>&) constructors (including allocator_arg_t variants)
848
849    template <class... _Up, enable_if_t<
850        _EnableCtorFromUTypesTuple<tuple<_Up...>&>::value>* = nullptr>
851    _LIBCPP_HIDE_FROM_ABI constexpr
852        explicit(!(is_convertible_v<_Up&, _Tp> && ...))
853    tuple(tuple<_Up...>& __t) : __base_(__t) {}
854
855    template <class _Alloc, class... _Up, enable_if_t<
856        _EnableCtorFromUTypesTuple<tuple<_Up...>&>::value>* = nullptr>
857    _LIBCPP_HIDE_FROM_ABI constexpr
858        explicit(!(is_convertible_v<_Up&, _Tp> && ...))
859    tuple(allocator_arg_t, const _Alloc& __alloc, tuple<_Up...>& __t) : __base_(allocator_arg_t(), __alloc, __t) {}
860#endif // _LIBCPP_STD_VER > 20
861
862    // tuple(tuple<U...>&&) constructors (including allocator_arg_t variants)
863
864    template <class ..._Up, __enable_if_t<
865        _And<
866            _EnableCtorFromUTypesTuple<tuple<_Up...>&&>,
867            is_convertible<_Up, _Tp>... // explicit check
868        >::value
869    , int> = 0>
870    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
871    tuple(tuple<_Up...>&& __t)
872        _NOEXCEPT_((_And<is_nothrow_constructible<_Tp, _Up>...>::value))
873        : __base_(_VSTD::move(__t))
874    { }
875
876    template <class ..._Up, __enable_if_t<
877        _And<
878            _EnableCtorFromUTypesTuple<tuple<_Up...>&&>,
879            _Not<_Lazy<_And, is_convertible<_Up, _Tp>...> > // explicit check
880        >::value
881    , int> = 0>
882    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
883    explicit tuple(tuple<_Up...>&& __t)
884        _NOEXCEPT_((_And<is_nothrow_constructible<_Tp, _Up>...>::value))
885        : __base_(_VSTD::move(__t))
886    { }
887
888    template <class _Alloc, class ..._Up, __enable_if_t<
889        _And<
890            _EnableCtorFromUTypesTuple<tuple<_Up...>&&>,
891            is_convertible<_Up, _Tp>... // explicit check
892        >::value
893    , int> = 0>
894    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
895    tuple(allocator_arg_t, const _Alloc& __a, tuple<_Up...>&& __t)
896        : __base_(allocator_arg_t(), __a, _VSTD::move(__t))
897    { }
898
899    template <class _Alloc, class ..._Up, __enable_if_t<
900        _And<
901            _EnableCtorFromUTypesTuple<tuple<_Up...>&&>,
902            _Not<_Lazy<_And, is_convertible<_Up, _Tp>...> > // explicit check
903        >::value
904    , int> = 0>
905    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
906    explicit tuple(allocator_arg_t, const _Alloc& __a, tuple<_Up...>&& __t)
907        : __base_(allocator_arg_t(), __a, _VSTD::move(__t))
908    { }
909
910#if _LIBCPP_STD_VER > 20
911    // tuple(const tuple<U...>&&) constructors (including allocator_arg_t variants)
912
913    template <class... _Up, enable_if_t<
914        _EnableCtorFromUTypesTuple<const tuple<_Up...>&&>::value>* = nullptr>
915    _LIBCPP_HIDE_FROM_ABI constexpr
916        explicit(!(is_convertible_v<const _Up&&, _Tp> && ...))
917    tuple(const tuple<_Up...>&& __t) : __base_(std::move(__t)) {}
918
919    template <class _Alloc, class... _Up, enable_if_t<
920        _EnableCtorFromUTypesTuple<const tuple<_Up...>&&>::value>* = nullptr>
921    _LIBCPP_HIDE_FROM_ABI constexpr
922        explicit(!(is_convertible_v<const _Up&&, _Tp> && ...))
923    tuple(allocator_arg_t, const _Alloc& __alloc, const tuple<_Up...>&& __t)
924        : __base_(allocator_arg_t(), __alloc, std::move(__t)) {}
925#endif // _LIBCPP_STD_VER > 20
926
927    // tuple(const pair<U1, U2>&) constructors (including allocator_arg_t variants)
928
929    template <template <class...> class Pred, class _Pair, class _DecayedPair = __uncvref_t<_Pair>, class _Tuple = tuple>
930    struct _CtorPredicateFromPair : false_type{};
931
932    template <template <class...> class Pred, class _Pair, class _Up1, class _Up2, class _Tp1, class _Tp2>
933    struct _CtorPredicateFromPair<Pred, _Pair, pair<_Up1, _Up2>, tuple<_Tp1, _Tp2> > : _And<
934        Pred<_Tp1, __copy_cvref_t<_Pair, _Up1> >,
935        Pred<_Tp2, __copy_cvref_t<_Pair, _Up2> >
936    > {};
937
938    template <class _Pair>
939    struct _EnableCtorFromPair : _CtorPredicateFromPair<is_constructible, _Pair>{};
940
941    template <class _Pair>
942    struct _NothrowConstructibleFromPair : _CtorPredicateFromPair<is_nothrow_constructible, _Pair>{};
943
944    template <class _Pair, class _DecayedPair = __uncvref_t<_Pair>, class _Tuple = tuple>
945    struct _BothImplicitlyConvertible : false_type{};
946
947    template <class _Pair, class _Up1, class _Up2, class _Tp1, class _Tp2>
948    struct _BothImplicitlyConvertible<_Pair, pair<_Up1, _Up2>, tuple<_Tp1, _Tp2> > : _And<
949        is_convertible<__copy_cvref_t<_Pair, _Up1>, _Tp1>,
950        is_convertible<__copy_cvref_t<_Pair, _Up2>, _Tp2>
951    > {};
952
953    template <class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
954        _And<
955            _EnableCtorFromPair<const pair<_Up1, _Up2>&>,
956            _BothImplicitlyConvertible<const pair<_Up1, _Up2>&> // explicit check
957        >::value
958    , int> = 0>
959    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
960    tuple(const pair<_Up1, _Up2>& __p)
961        _NOEXCEPT_((_NothrowConstructibleFromPair<const pair<_Up1, _Up2>&>::value))
962        : __base_(__p)
963    { }
964
965    template <class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
966        _And<
967            _EnableCtorFromPair<const pair<_Up1, _Up2>&>,
968            _Not<_BothImplicitlyConvertible<const pair<_Up1, _Up2>&> > // explicit check
969        >::value
970    , int> = 0>
971    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
972    explicit tuple(const pair<_Up1, _Up2>& __p)
973        _NOEXCEPT_((_NothrowConstructibleFromPair<const pair<_Up1, _Up2>&>::value))
974        : __base_(__p)
975    { }
976
977    template <class _Alloc, class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
978        _And<
979            _EnableCtorFromPair<const pair<_Up1, _Up2>&>,
980            _BothImplicitlyConvertible<const pair<_Up1, _Up2>&> // explicit check
981        >::value
982    , int> = 0>
983    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
984    tuple(allocator_arg_t, const _Alloc& __a, const pair<_Up1, _Up2>& __p)
985        : __base_(allocator_arg_t(), __a, __p)
986    { }
987
988    template <class _Alloc, class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
989        _And<
990            _EnableCtorFromPair<const pair<_Up1, _Up2>&>,
991            _Not<_BothImplicitlyConvertible<const pair<_Up1, _Up2>&> > // explicit check
992        >::value
993    , int> = 0>
994    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
995    explicit tuple(allocator_arg_t, const _Alloc& __a, const pair<_Up1, _Up2>& __p)
996        : __base_(allocator_arg_t(), __a, __p)
997    { }
998
999#if _LIBCPP_STD_VER > 20
1000    // tuple(pair<U1, U2>&) constructors (including allocator_arg_t variants)
1001
1002    template <class _U1, class _U2, enable_if_t<
1003        _EnableCtorFromPair<pair<_U1, _U2>&>::value>* = nullptr>
1004    _LIBCPP_HIDE_FROM_ABI constexpr
1005        explicit(!_BothImplicitlyConvertible<pair<_U1, _U2>&>::value)
1006    tuple(pair<_U1, _U2>& __p) : __base_(__p) {}
1007
1008    template <class _Alloc, class _U1, class _U2, enable_if_t<
1009        _EnableCtorFromPair<std::pair<_U1, _U2>&>::value>* = nullptr>
1010    _LIBCPP_HIDE_FROM_ABI constexpr
1011        explicit(!_BothImplicitlyConvertible<pair<_U1, _U2>&>::value)
1012    tuple(allocator_arg_t, const _Alloc& __alloc, pair<_U1, _U2>& __p) : __base_(allocator_arg_t(), __alloc, __p) {}
1013#endif
1014
1015    // tuple(pair<U1, U2>&&) constructors (including allocator_arg_t variants)
1016
1017    template <class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
1018        _And<
1019            _EnableCtorFromPair<pair<_Up1, _Up2>&&>,
1020            _BothImplicitlyConvertible<pair<_Up1, _Up2>&&> // explicit check
1021        >::value
1022    , int> = 0>
1023    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1024    tuple(pair<_Up1, _Up2>&& __p)
1025        _NOEXCEPT_((_NothrowConstructibleFromPair<pair<_Up1, _Up2>&&>::value))
1026        : __base_(_VSTD::move(__p))
1027    { }
1028
1029    template <class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
1030        _And<
1031            _EnableCtorFromPair<pair<_Up1, _Up2>&&>,
1032            _Not<_BothImplicitlyConvertible<pair<_Up1, _Up2>&&> > // explicit check
1033        >::value
1034    , int> = 0>
1035    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1036    explicit tuple(pair<_Up1, _Up2>&& __p)
1037        _NOEXCEPT_((_NothrowConstructibleFromPair<pair<_Up1, _Up2>&&>::value))
1038        : __base_(_VSTD::move(__p))
1039    { }
1040
1041    template <class _Alloc, class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
1042        _And<
1043            _EnableCtorFromPair<pair<_Up1, _Up2>&&>,
1044            _BothImplicitlyConvertible<pair<_Up1, _Up2>&&> // explicit check
1045        >::value
1046    , int> = 0>
1047    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1048    tuple(allocator_arg_t, const _Alloc& __a, pair<_Up1, _Up2>&& __p)
1049        : __base_(allocator_arg_t(), __a, _VSTD::move(__p))
1050    { }
1051
1052    template <class _Alloc, class _Up1, class _Up2, template<class...> class _And = _And, __enable_if_t<
1053        _And<
1054            _EnableCtorFromPair<pair<_Up1, _Up2>&&>,
1055            _Not<_BothImplicitlyConvertible<pair<_Up1, _Up2>&&> > // explicit check
1056        >::value
1057    , int> = 0>
1058    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1059    explicit tuple(allocator_arg_t, const _Alloc& __a, pair<_Up1, _Up2>&& __p)
1060        : __base_(allocator_arg_t(), __a, _VSTD::move(__p))
1061    { }
1062
1063#if _LIBCPP_STD_VER > 20
1064    // tuple(const pair<U1, U2>&&) constructors (including allocator_arg_t variants)
1065
1066    template <class _U1, class _U2, enable_if_t<
1067        _EnableCtorFromPair<const pair<_U1, _U2>&&>::value>* = nullptr>
1068    _LIBCPP_HIDE_FROM_ABI constexpr
1069        explicit(!_BothImplicitlyConvertible<const pair<_U1, _U2>&&>::value)
1070    tuple(const pair<_U1, _U2>&& __p) : __base_(std::move(__p)) {}
1071
1072    template <class _Alloc, class _U1, class _U2, enable_if_t<
1073        _EnableCtorFromPair<const pair<_U1, _U2>&&>::value>* = nullptr>
1074    _LIBCPP_HIDE_FROM_ABI constexpr
1075        explicit(!_BothImplicitlyConvertible<const pair<_U1, _U2>&&>::value)
1076    tuple(allocator_arg_t, const _Alloc& __alloc, const pair<_U1, _U2>&& __p)
1077        : __base_(allocator_arg_t(), __alloc, std::move(__p)) {}
1078#endif // _LIBCPP_STD_VER > 20
1079
1080    // [tuple.assign]
1081    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1082    tuple& operator=(_If<_And<is_copy_assignable<_Tp>...>::value, tuple, __nat> const& __tuple)
1083        _NOEXCEPT_((_And<is_nothrow_copy_assignable<_Tp>...>::value))
1084    {
1085        _VSTD::__memberwise_copy_assign(*this, __tuple,
1086            typename __make_tuple_indices<sizeof...(_Tp)>::type());
1087        return *this;
1088    }
1089
1090#if _LIBCPP_STD_VER > 20
1091    _LIBCPP_HIDE_FROM_ABI constexpr
1092    const tuple& operator=(tuple const& __tuple) const
1093      requires (_And<is_copy_assignable<const _Tp>...>::value) {
1094        std::__memberwise_copy_assign(*this, __tuple, typename __make_tuple_indices<sizeof...(_Tp)>::type());
1095        return *this;
1096    }
1097
1098    _LIBCPP_HIDE_FROM_ABI constexpr
1099    const tuple& operator=(tuple&& __tuple) const
1100      requires (_And<is_assignable<const _Tp&, _Tp>...>::value) {
1101        std::__memberwise_forward_assign(*this,
1102                                         std::move(__tuple),
1103                                         __tuple_types<_Tp...>(),
1104                                         typename __make_tuple_indices<sizeof...(_Tp)>::type());
1105        return *this;
1106    }
1107#endif // _LIBCPP_STD_VER > 20
1108
1109    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1110    tuple& operator=(_If<_And<is_move_assignable<_Tp>...>::value, tuple, __nat>&& __tuple)
1111        _NOEXCEPT_((_And<is_nothrow_move_assignable<_Tp>...>::value))
1112    {
1113        _VSTD::__memberwise_forward_assign(*this, _VSTD::move(__tuple),
1114            __tuple_types<_Tp...>(),
1115            typename __make_tuple_indices<sizeof...(_Tp)>::type());
1116        return *this;
1117    }
1118
1119    template<class... _Up, __enable_if_t<
1120        _And<
1121            _BoolConstant<sizeof...(_Tp) == sizeof...(_Up)>,
1122            is_assignable<_Tp&, _Up const&>...
1123        >::value
1124    ,int> = 0>
1125    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1126    tuple& operator=(tuple<_Up...> const& __tuple)
1127        _NOEXCEPT_((_And<is_nothrow_assignable<_Tp&, _Up const&>...>::value))
1128    {
1129        _VSTD::__memberwise_copy_assign(*this, __tuple,
1130            typename __make_tuple_indices<sizeof...(_Tp)>::type());
1131        return *this;
1132    }
1133
1134    template<class... _Up, __enable_if_t<
1135        _And<
1136            _BoolConstant<sizeof...(_Tp) == sizeof...(_Up)>,
1137            is_assignable<_Tp&, _Up>...
1138        >::value
1139    ,int> = 0>
1140    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1141    tuple& operator=(tuple<_Up...>&& __tuple)
1142        _NOEXCEPT_((_And<is_nothrow_assignable<_Tp&, _Up>...>::value))
1143    {
1144        _VSTD::__memberwise_forward_assign(*this, _VSTD::move(__tuple),
1145            __tuple_types<_Up...>(),
1146            typename __make_tuple_indices<sizeof...(_Tp)>::type());
1147        return *this;
1148    }
1149
1150
1151#if _LIBCPP_STD_VER > 20
1152    template <class... _UTypes, enable_if_t<
1153        _And<_BoolConstant<sizeof...(_Tp) == sizeof...(_UTypes)>,
1154             is_assignable<const _Tp&, const _UTypes&>...>::value>* = nullptr>
1155    _LIBCPP_HIDE_FROM_ABI constexpr
1156    const tuple& operator=(const tuple<_UTypes...>& __u) const {
1157        std::__memberwise_copy_assign(*this,
1158                                      __u,
1159                                      typename __make_tuple_indices<sizeof...(_Tp)>::type());
1160        return *this;
1161    }
1162
1163    template <class... _UTypes, enable_if_t<
1164        _And<_BoolConstant<sizeof...(_Tp) == sizeof...(_UTypes)>,
1165             is_assignable<const _Tp&, _UTypes>...>::value>* = nullptr>
1166    _LIBCPP_HIDE_FROM_ABI constexpr
1167    const tuple& operator=(tuple<_UTypes...>&& __u) const {
1168        std::__memberwise_forward_assign(*this,
1169                                         __u,
1170                                         __tuple_types<_UTypes...>(),
1171                                         typename __make_tuple_indices<sizeof...(_Tp)>::type());
1172        return *this;
1173    }
1174#endif // _LIBCPP_STD_VER > 20
1175
1176    template <template<class...> class Pred, bool _Const,
1177              class _Pair, class _DecayedPair = __uncvref_t<_Pair>, class _Tuple = tuple>
1178    struct _AssignPredicateFromPair : false_type {};
1179
1180    template <template<class...> class Pred, bool _Const,
1181              class _Pair, class _Up1, class _Up2, class _Tp1, class _Tp2>
1182    struct _AssignPredicateFromPair<Pred, _Const, _Pair, pair<_Up1, _Up2>, tuple<_Tp1, _Tp2> > :
1183        _And<Pred<__maybe_const<_Const, _Tp1>&, __copy_cvref_t<_Pair, _Up1> >,
1184             Pred<__maybe_const<_Const, _Tp2>&, __copy_cvref_t<_Pair, _Up2> >
1185            > {};
1186
1187    template <bool _Const, class _Pair>
1188    struct _EnableAssignFromPair : _AssignPredicateFromPair<is_assignable, _Const, _Pair> {};
1189
1190    template <bool _Const, class _Pair>
1191    struct _NothrowAssignFromPair : _AssignPredicateFromPair<is_nothrow_assignable, _Const, _Pair> {};
1192
1193#if _LIBCPP_STD_VER > 20
1194    template <class _U1, class _U2, enable_if_t<
1195        _EnableAssignFromPair<true, const pair<_U1, _U2>&>::value>* = nullptr>
1196    _LIBCPP_HIDE_FROM_ABI constexpr
1197    const tuple& operator=(const pair<_U1, _U2>& __pair) const
1198      noexcept(_NothrowAssignFromPair<true, const pair<_U1, _U2>&>::value) {
1199        std::get<0>(*this) = __pair.first;
1200        std::get<1>(*this) = __pair.second;
1201        return *this;
1202    }
1203
1204    template <class _U1, class _U2, enable_if_t<
1205        _EnableAssignFromPair<true, pair<_U1, _U2>&&>::value>* = nullptr>
1206    _LIBCPP_HIDE_FROM_ABI constexpr
1207    const tuple& operator=(pair<_U1, _U2>&& __pair) const
1208      noexcept(_NothrowAssignFromPair<true, pair<_U1, _U2>&&>::value) {
1209        std::get<0>(*this) = std::move(__pair.first);
1210        std::get<1>(*this) = std::move(__pair.second);
1211        return *this;
1212    }
1213#endif // _LIBCPP_STD_VER > 20
1214
1215    template<class _Up1, class _Up2, __enable_if_t<
1216        _EnableAssignFromPair<false, pair<_Up1, _Up2> const&>::value
1217    ,int> = 0>
1218    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1219    tuple& operator=(pair<_Up1, _Up2> const& __pair)
1220        _NOEXCEPT_((_NothrowAssignFromPair<false, pair<_Up1, _Up2> const&>::value))
1221    {
1222        _VSTD::get<0>(*this) = __pair.first;
1223        _VSTD::get<1>(*this) = __pair.second;
1224        return *this;
1225    }
1226
1227    template<class _Up1, class _Up2, __enable_if_t<
1228        _EnableAssignFromPair<false, pair<_Up1, _Up2>&&>::value
1229    ,int> = 0>
1230    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1231    tuple& operator=(pair<_Up1, _Up2>&& __pair)
1232        _NOEXCEPT_((_NothrowAssignFromPair<false, pair<_Up1, _Up2>&&>::value))
1233    {
1234        _VSTD::get<0>(*this) = _VSTD::forward<_Up1>(__pair.first);
1235        _VSTD::get<1>(*this) = _VSTD::forward<_Up2>(__pair.second);
1236        return *this;
1237    }
1238
1239    // EXTENSION
1240    template<class _Up, size_t _Np, class = __enable_if_t<
1241        _And<
1242            _BoolConstant<_Np == sizeof...(_Tp)>,
1243            is_assignable<_Tp&, _Up const&>...
1244        >::value
1245    > >
1246    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1247    tuple& operator=(array<_Up, _Np> const& __array)
1248        _NOEXCEPT_((_And<is_nothrow_assignable<_Tp&, _Up const&>...>::value))
1249    {
1250        _VSTD::__memberwise_copy_assign(*this, __array,
1251            typename __make_tuple_indices<sizeof...(_Tp)>::type());
1252        return *this;
1253    }
1254
1255    // EXTENSION
1256    template<class _Up, size_t _Np, class = void, class = __enable_if_t<
1257        _And<
1258            _BoolConstant<_Np == sizeof...(_Tp)>,
1259            is_assignable<_Tp&, _Up>...
1260        >::value
1261    > >
1262    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1263    tuple& operator=(array<_Up, _Np>&& __array)
1264        _NOEXCEPT_((_And<is_nothrow_assignable<_Tp&, _Up>...>::value))
1265    {
1266        _VSTD::__memberwise_forward_assign(*this, _VSTD::move(__array),
1267            __tuple_types<_If<true, _Up, _Tp>...>(),
1268            typename __make_tuple_indices<sizeof...(_Tp)>::type());
1269        return *this;
1270    }
1271
1272    // [tuple.swap]
1273    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1274    void swap(tuple& __t) _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
1275        {__base_.swap(__t.__base_);}
1276
1277#if _LIBCPP_STD_VER > 20
1278    _LIBCPP_HIDE_FROM_ABI constexpr
1279    void swap(const tuple& __t) const noexcept(__all<is_nothrow_swappable_v<const _Tp&>...>::value) {
1280        __base_.swap(__t.__base_);
1281    }
1282#endif // _LIBCPP_STD_VER > 20
1283};
1284
1285template <>
1286class _LIBCPP_TEMPLATE_VIS tuple<>
1287{
1288public:
1289    _LIBCPP_INLINE_VISIBILITY constexpr
1290        tuple() _NOEXCEPT = default;
1291    template <class _Alloc>
1292    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1293        tuple(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1294    template <class _Alloc>
1295    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1296        tuple(allocator_arg_t, const _Alloc&, const tuple&) _NOEXCEPT {}
1297    template <class _Up>
1298    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1299        tuple(array<_Up, 0>) _NOEXCEPT {}
1300    template <class _Alloc, class _Up>
1301    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1302        tuple(allocator_arg_t, const _Alloc&, array<_Up, 0>) _NOEXCEPT {}
1303    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1304    void swap(tuple&) _NOEXCEPT {}
1305#if _LIBCPP_STD_VER > 20
1306    _LIBCPP_HIDE_FROM_ABI constexpr void swap(const tuple&) const noexcept {}
1307#endif
1308};
1309
1310#if _LIBCPP_STD_VER > 20
1311template <class... _TTypes, class... _UTypes, template<class> class _TQual, template<class> class _UQual>
1312    requires requires { typename tuple<common_reference_t<_TQual<_TTypes>, _UQual<_UTypes>>...>; }
1313struct basic_common_reference<tuple<_TTypes...>, tuple<_UTypes...>, _TQual, _UQual> {
1314    using type = tuple<common_reference_t<_TQual<_TTypes>, _UQual<_UTypes>>...>;
1315};
1316
1317template <class... _TTypes, class... _UTypes>
1318    requires requires { typename tuple<common_type_t<_TTypes, _UTypes>...>; }
1319struct common_type<tuple<_TTypes...>, tuple<_UTypes...>> {
1320    using type = tuple<common_type_t<_TTypes, _UTypes>...>;
1321};
1322#endif // _LIBCPP_STD_VER > 20
1323
1324#if _LIBCPP_STD_VER > 14
1325template <class ..._Tp>
1326tuple(_Tp...) -> tuple<_Tp...>;
1327template <class _Tp1, class _Tp2>
1328tuple(pair<_Tp1, _Tp2>) -> tuple<_Tp1, _Tp2>;
1329template <class _Alloc, class ..._Tp>
1330tuple(allocator_arg_t, _Alloc, _Tp...) -> tuple<_Tp...>;
1331template <class _Alloc, class _Tp1, class _Tp2>
1332tuple(allocator_arg_t, _Alloc, pair<_Tp1, _Tp2>) -> tuple<_Tp1, _Tp2>;
1333template <class _Alloc, class ..._Tp>
1334tuple(allocator_arg_t, _Alloc, tuple<_Tp...>) -> tuple<_Tp...>;
1335#endif
1336
1337template <class ..._Tp>
1338inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1339typename enable_if
1340<
1341    __all<__is_swappable<_Tp>::value...>::value,
1342    void
1343>::type
1344swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u)
1345                 _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
1346    {__t.swap(__u);}
1347
1348#if _LIBCPP_STD_VER > 20
1349template <class... _Tp>
1350_LIBCPP_HIDE_FROM_ABI constexpr
1351enable_if_t<__all<is_swappable_v<const _Tp>...>::value, void>
1352swap(const tuple<_Tp...>& __lhs, const tuple<_Tp...>& __rhs)
1353        noexcept(__all<is_nothrow_swappable_v<const _Tp>...>::value) {
1354    __lhs.swap(__rhs);
1355}
1356#endif
1357
1358// get
1359
1360template <size_t _Ip, class ..._Tp>
1361inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1362typename tuple_element<_Ip, tuple<_Tp...> >::type&
1363get(tuple<_Tp...>& __t) _NOEXCEPT
1364{
1365    typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1366    return static_cast<__tuple_leaf<_Ip, type>&>(__t.__base_).get();
1367}
1368
1369template <size_t _Ip, class ..._Tp>
1370inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1371const typename tuple_element<_Ip, tuple<_Tp...> >::type&
1372get(const tuple<_Tp...>& __t) _NOEXCEPT
1373{
1374    typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1375    return static_cast<const __tuple_leaf<_Ip, type>&>(__t.__base_).get();
1376}
1377
1378template <size_t _Ip, class ..._Tp>
1379inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1380typename tuple_element<_Ip, tuple<_Tp...> >::type&&
1381get(tuple<_Tp...>&& __t) _NOEXCEPT
1382{
1383    typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1384    return static_cast<type&&>(
1385             static_cast<__tuple_leaf<_Ip, type>&&>(__t.__base_).get());
1386}
1387
1388template <size_t _Ip, class ..._Tp>
1389inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1390const typename tuple_element<_Ip, tuple<_Tp...> >::type&&
1391get(const tuple<_Tp...>&& __t) _NOEXCEPT
1392{
1393    typedef _LIBCPP_NODEBUG typename tuple_element<_Ip, tuple<_Tp...> >::type type;
1394    return static_cast<const type&&>(
1395             static_cast<const __tuple_leaf<_Ip, type>&&>(__t.__base_).get());
1396}
1397
1398#if _LIBCPP_STD_VER > 11
1399
1400namespace __find_detail {
1401
1402static constexpr size_t __not_found = static_cast<size_t>(-1);
1403static constexpr size_t __ambiguous = __not_found - 1;
1404
1405inline _LIBCPP_INLINE_VISIBILITY
1406constexpr size_t __find_idx_return(size_t __curr_i, size_t __res, bool __matches) {
1407    return !__matches ? __res :
1408        (__res == __not_found ? __curr_i : __ambiguous);
1409}
1410
1411template <size_t _Nx>
1412inline _LIBCPP_INLINE_VISIBILITY
1413constexpr size_t __find_idx(size_t __i, const bool (&__matches)[_Nx]) {
1414  return __i == _Nx ? __not_found :
1415      __find_idx_return(__i, __find_idx(__i + 1, __matches), __matches[__i]);
1416}
1417
1418template <class _T1, class ..._Args>
1419struct __find_exactly_one_checked {
1420    static constexpr bool __matches[sizeof...(_Args)] = {is_same<_T1, _Args>::value...};
1421    static constexpr size_t value = __find_detail::__find_idx(0, __matches);
1422    static_assert(value != __not_found, "type not found in type list" );
1423    static_assert(value != __ambiguous, "type occurs more than once in type list");
1424};
1425
1426template <class _T1>
1427struct __find_exactly_one_checked<_T1> {
1428    static_assert(!is_same<_T1, _T1>::value, "type not in empty type list");
1429};
1430
1431} // namespace __find_detail
1432
1433template <typename _T1, typename... _Args>
1434struct __find_exactly_one_t
1435    : public __find_detail::__find_exactly_one_checked<_T1, _Args...> {
1436};
1437
1438template <class _T1, class... _Args>
1439inline _LIBCPP_INLINE_VISIBILITY
1440constexpr _T1& get(tuple<_Args...>& __tup) noexcept
1441{
1442    return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(__tup);
1443}
1444
1445template <class _T1, class... _Args>
1446inline _LIBCPP_INLINE_VISIBILITY
1447constexpr _T1 const& get(tuple<_Args...> const& __tup) noexcept
1448{
1449    return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(__tup);
1450}
1451
1452template <class _T1, class... _Args>
1453inline _LIBCPP_INLINE_VISIBILITY
1454constexpr _T1&& get(tuple<_Args...>&& __tup) noexcept
1455{
1456    return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(_VSTD::move(__tup));
1457}
1458
1459template <class _T1, class... _Args>
1460inline _LIBCPP_INLINE_VISIBILITY
1461constexpr _T1 const&& get(tuple<_Args...> const&& __tup) noexcept
1462{
1463    return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(_VSTD::move(__tup));
1464}
1465
1466#endif
1467
1468// tie
1469
1470template <class ..._Tp>
1471inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1472tuple<_Tp&...>
1473tie(_Tp&... __t) _NOEXCEPT
1474{
1475    return tuple<_Tp&...>(__t...);
1476}
1477
1478template <class _Up>
1479struct __ignore_t
1480{
1481    template <class _Tp>
1482    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1483    const __ignore_t& operator=(_Tp&&) const {return *this;}
1484};
1485
1486namespace {
1487  constexpr __ignore_t<unsigned char> ignore = __ignore_t<unsigned char>();
1488} // namespace
1489
1490template <class... _Tp>
1491inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1492tuple<typename __unwrap_ref_decay<_Tp>::type...>
1493make_tuple(_Tp&&... __t)
1494{
1495    return tuple<typename __unwrap_ref_decay<_Tp>::type...>(_VSTD::forward<_Tp>(__t)...);
1496}
1497
1498template <class... _Tp>
1499inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1500tuple<_Tp&&...>
1501forward_as_tuple(_Tp&&... __t) _NOEXCEPT
1502{
1503    return tuple<_Tp&&...>(_VSTD::forward<_Tp>(__t)...);
1504}
1505
1506template <size_t _Ip>
1507struct __tuple_equal
1508{
1509    template <class _Tp, class _Up>
1510    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1511    bool operator()(const _Tp& __x, const _Up& __y)
1512    {
1513        return __tuple_equal<_Ip - 1>()(__x, __y) && _VSTD::get<_Ip-1>(__x) == _VSTD::get<_Ip-1>(__y);
1514    }
1515};
1516
1517template <>
1518struct __tuple_equal<0>
1519{
1520    template <class _Tp, class _Up>
1521    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1522    bool operator()(const _Tp&, const _Up&)
1523    {
1524        return true;
1525    }
1526};
1527
1528template <class ..._Tp, class ..._Up>
1529inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1530bool
1531operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
1532{
1533    static_assert (sizeof...(_Tp) == sizeof...(_Up), "Can't compare tuples of different sizes");
1534    return __tuple_equal<sizeof...(_Tp)>()(__x, __y);
1535}
1536
1537#if _LIBCPP_STD_VER > 17
1538
1539// operator<=>
1540
1541template <class ..._Tp, class ..._Up, size_t ..._Is>
1542_LIBCPP_HIDE_FROM_ABI constexpr
1543auto
1544__tuple_compare_three_way(const tuple<_Tp...>& __x, const tuple<_Up...>& __y, index_sequence<_Is...>) {
1545    common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...> __result = strong_ordering::equal;
1546    static_cast<void>(((__result = _VSTD::__synth_three_way(_VSTD::get<_Is>(__x), _VSTD::get<_Is>(__y)), __result != 0) || ...));
1547    return __result;
1548}
1549
1550template <class ..._Tp, class ..._Up>
1551requires (sizeof...(_Tp) == sizeof...(_Up))
1552_LIBCPP_HIDE_FROM_ABI constexpr
1553common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...>
1554operator<=>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
1555{
1556    return _VSTD::__tuple_compare_three_way(__x, __y, index_sequence_for<_Tp...>{});
1557}
1558
1559#else // _LIBCPP_STD_VER > 17
1560
1561template <class ..._Tp, class ..._Up>
1562inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1563bool
1564operator!=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
1565{
1566    return !(__x == __y);
1567}
1568
1569template <size_t _Ip>
1570struct __tuple_less
1571{
1572    template <class _Tp, class _Up>
1573    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1574    bool operator()(const _Tp& __x, const _Up& __y)
1575    {
1576        const size_t __idx = tuple_size<_Tp>::value - _Ip;
1577        if (_VSTD::get<__idx>(__x) < _VSTD::get<__idx>(__y))
1578            return true;
1579        if (_VSTD::get<__idx>(__y) < _VSTD::get<__idx>(__x))
1580            return false;
1581        return __tuple_less<_Ip-1>()(__x, __y);
1582    }
1583};
1584
1585template <>
1586struct __tuple_less<0>
1587{
1588    template <class _Tp, class _Up>
1589    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1590    bool operator()(const _Tp&, const _Up&)
1591    {
1592        return false;
1593    }
1594};
1595
1596template <class ..._Tp, class ..._Up>
1597inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1598bool
1599operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
1600{
1601    static_assert (sizeof...(_Tp) == sizeof...(_Up), "Can't compare tuples of different sizes");
1602    return __tuple_less<sizeof...(_Tp)>()(__x, __y);
1603}
1604
1605template <class ..._Tp, class ..._Up>
1606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1607bool
1608operator>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
1609{
1610    return __y < __x;
1611}
1612
1613template <class ..._Tp, class ..._Up>
1614inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1615bool
1616operator>=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
1617{
1618    return !(__x < __y);
1619}
1620
1621template <class ..._Tp, class ..._Up>
1622inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1623bool
1624operator<=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
1625{
1626    return !(__y < __x);
1627}
1628
1629#endif // _LIBCPP_STD_VER > 17
1630
1631// tuple_cat
1632
1633template <class _Tp, class _Up> struct __tuple_cat_type;
1634
1635template <class ..._Ttypes, class ..._Utypes>
1636struct __tuple_cat_type<tuple<_Ttypes...>, __tuple_types<_Utypes...> >
1637{
1638    typedef _LIBCPP_NODEBUG tuple<_Ttypes..., _Utypes...> type;
1639};
1640
1641template <class _ResultTuple, bool _Is_Tuple0TupleLike, class ..._Tuples>
1642struct __tuple_cat_return_1
1643{
1644};
1645
1646template <class ..._Types, class _Tuple0>
1647struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0>
1648{
1649  using type _LIBCPP_NODEBUG = typename __tuple_cat_type<
1650      tuple<_Types...>,
1651      typename __make_tuple_types<__uncvref_t<_Tuple0> >::type
1652    >::type;
1653};
1654
1655template <class ..._Types, class _Tuple0, class _Tuple1, class ..._Tuples>
1656struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0, _Tuple1, _Tuples...>
1657    : public __tuple_cat_return_1<
1658                 typename __tuple_cat_type<
1659                     tuple<_Types...>,
1660                     typename __make_tuple_types<__uncvref_t<_Tuple0> >::type
1661                 >::type,
1662                 __tuple_like<typename remove_reference<_Tuple1>::type>::value,
1663                 _Tuple1, _Tuples...>
1664{
1665};
1666
1667template <class ..._Tuples> struct __tuple_cat_return;
1668
1669template <class _Tuple0, class ..._Tuples>
1670struct __tuple_cat_return<_Tuple0, _Tuples...>
1671    : public __tuple_cat_return_1<tuple<>,
1672         __tuple_like<typename remove_reference<_Tuple0>::type>::value, _Tuple0,
1673                                                                     _Tuples...>
1674{
1675};
1676
1677template <>
1678struct __tuple_cat_return<>
1679{
1680    typedef _LIBCPP_NODEBUG tuple<> type;
1681};
1682
1683inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1684tuple<>
1685tuple_cat()
1686{
1687    return tuple<>();
1688}
1689
1690template <class _Rp, class _Indices, class _Tuple0, class ..._Tuples>
1691struct __tuple_cat_return_ref_imp;
1692
1693template <class ..._Types, size_t ..._I0, class _Tuple0>
1694struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>, _Tuple0>
1695{
1696    typedef _LIBCPP_NODEBUG typename remove_reference<_Tuple0>::type _T0;
1697    typedef tuple<_Types..., typename __apply_cv<_Tuple0,
1698                          typename tuple_element<_I0, _T0>::type>::type&&...> type;
1699};
1700
1701template <class ..._Types, size_t ..._I0, class _Tuple0, class _Tuple1, class ..._Tuples>
1702struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>,
1703                                  _Tuple0, _Tuple1, _Tuples...>
1704    : public __tuple_cat_return_ref_imp<
1705         tuple<_Types..., typename __apply_cv<_Tuple0,
1706               typename tuple_element<_I0,
1707                  typename remove_reference<_Tuple0>::type>::type>::type&&...>,
1708         typename __make_tuple_indices<tuple_size<typename
1709                                 remove_reference<_Tuple1>::type>::value>::type,
1710         _Tuple1, _Tuples...>
1711{
1712};
1713
1714template <class _Tuple0, class ..._Tuples>
1715struct __tuple_cat_return_ref
1716    : public __tuple_cat_return_ref_imp<tuple<>,
1717               typename __make_tuple_indices<
1718                        tuple_size<typename remove_reference<_Tuple0>::type>::value
1719               >::type, _Tuple0, _Tuples...>
1720{
1721};
1722
1723template <class _Types, class _I0, class _J0>
1724struct __tuple_cat;
1725
1726template <class ..._Types, size_t ..._I0, size_t ..._J0>
1727struct __tuple_cat<tuple<_Types...>, __tuple_indices<_I0...>, __tuple_indices<_J0...> >
1728{
1729    template <class _Tuple0>
1730    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1731    typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&>::type
1732    operator()(tuple<_Types...> __t, _Tuple0&& __t0)
1733    {
1734        (void)__t; // avoid unused parameter warning on GCC when _I0 is empty
1735        return _VSTD::forward_as_tuple(
1736            _VSTD::forward<_Types>(_VSTD::get<_I0>(__t))...,
1737            _VSTD::get<_J0>(_VSTD::forward<_Tuple0>(__t0))...);
1738    }
1739
1740    template <class _Tuple0, class _Tuple1, class ..._Tuples>
1741    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1742    typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&, _Tuple1&&, _Tuples&&...>::type
1743    operator()(tuple<_Types...> __t, _Tuple0&& __t0, _Tuple1&& __t1, _Tuples&& ...__tpls)
1744    {
1745        (void)__t; // avoid unused parameter warning on GCC when _I0 is empty
1746        typedef _LIBCPP_NODEBUG typename remove_reference<_Tuple0>::type _T0;
1747        typedef _LIBCPP_NODEBUG typename remove_reference<_Tuple1>::type _T1;
1748        return __tuple_cat<
1749            tuple<_Types...,
1750                  typename __apply_cv<_Tuple0, typename tuple_element<
1751                                                   _J0, _T0>::type>::type&&...>,
1752            typename __make_tuple_indices<sizeof...(_Types) +
1753                                          tuple_size<_T0>::value>::type,
1754            typename __make_tuple_indices<tuple_size<_T1>::value>::type>()(
1755            _VSTD::forward_as_tuple(
1756                _VSTD::forward<_Types>(_VSTD::get<_I0>(__t))...,
1757                _VSTD::get<_J0>(_VSTD::forward<_Tuple0>(__t0))...),
1758            _VSTD::forward<_Tuple1>(__t1), _VSTD::forward<_Tuples>(__tpls)...);
1759    }
1760};
1761
1762template <class _Tuple0, class... _Tuples>
1763inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1764typename __tuple_cat_return<_Tuple0, _Tuples...>::type
1765tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls)
1766{
1767    typedef _LIBCPP_NODEBUG typename remove_reference<_Tuple0>::type _T0;
1768    return __tuple_cat<tuple<>, __tuple_indices<>,
1769                  typename __make_tuple_indices<tuple_size<_T0>::value>::type>()
1770                  (tuple<>(), _VSTD::forward<_Tuple0>(__t0),
1771                                            _VSTD::forward<_Tuples>(__tpls)...);
1772}
1773
1774template <class ..._Tp, class _Alloc>
1775struct _LIBCPP_TEMPLATE_VIS uses_allocator<tuple<_Tp...>, _Alloc>
1776    : true_type {};
1777
1778template <class _T1, class _T2>
1779template <class... _Args1, class... _Args2, size_t ..._I1, size_t ..._I2>
1780inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1781pair<_T1, _T2>::pair(piecewise_construct_t,
1782                     tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
1783                     __tuple_indices<_I1...>, __tuple_indices<_I2...>)
1784    :  first(_VSTD::forward<_Args1>(_VSTD::get<_I1>( __first_args))...),
1785      second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
1786{
1787}
1788
1789#if _LIBCPP_STD_VER > 14
1790template <class _Tp>
1791inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
1792
1793#define _LIBCPP_NOEXCEPT_RETURN(...) noexcept(noexcept(__VA_ARGS__)) { return __VA_ARGS__; }
1794
1795template <class _Fn, class _Tuple, size_t ..._Id>
1796inline _LIBCPP_INLINE_VISIBILITY
1797constexpr decltype(auto) __apply_tuple_impl(_Fn && __f, _Tuple && __t,
1798                                            __tuple_indices<_Id...>)
1799_LIBCPP_NOEXCEPT_RETURN(
1800    _VSTD::__invoke(
1801        _VSTD::forward<_Fn>(__f),
1802        _VSTD::get<_Id>(_VSTD::forward<_Tuple>(__t))...)
1803)
1804
1805template <class _Fn, class _Tuple>
1806inline _LIBCPP_INLINE_VISIBILITY
1807constexpr decltype(auto) apply(_Fn && __f, _Tuple && __t)
1808_LIBCPP_NOEXCEPT_RETURN(
1809    _VSTD::__apply_tuple_impl(
1810        _VSTD::forward<_Fn>(__f), _VSTD::forward<_Tuple>(__t),
1811        typename __make_tuple_indices<tuple_size_v<remove_reference_t<_Tuple>>>::type{})
1812)
1813
1814template <class _Tp, class _Tuple, size_t... _Idx>
1815inline _LIBCPP_INLINE_VISIBILITY
1816constexpr _Tp __make_from_tuple_impl(_Tuple&& __t, __tuple_indices<_Idx...>)
1817_LIBCPP_NOEXCEPT_RETURN(
1818    _Tp(_VSTD::get<_Idx>(_VSTD::forward<_Tuple>(__t))...)
1819)
1820
1821template <class _Tp, class _Tuple>
1822inline _LIBCPP_INLINE_VISIBILITY
1823constexpr _Tp make_from_tuple(_Tuple&& __t)
1824_LIBCPP_NOEXCEPT_RETURN(
1825    _VSTD::__make_from_tuple_impl<_Tp>(_VSTD::forward<_Tuple>(__t),
1826        typename __make_tuple_indices<tuple_size_v<remove_reference_t<_Tuple>>>::type{})
1827)
1828
1829#undef _LIBCPP_NOEXCEPT_RETURN
1830
1831#endif // _LIBCPP_STD_VER > 14
1832
1833#endif // !defined(_LIBCPP_CXX03_LANG)
1834
1835_LIBCPP_END_NAMESPACE_STD
1836
1837#endif // _LIBCPP_TUPLE
1838