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