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