2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___TUPLE
12 #define _LIBCPP___TUPLE
16 #include <type_traits>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
23 _LIBCPP_BEGIN_NAMESPACE_STD
25 template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_size;
28 class _LIBCPP_TYPE_VIS_ONLY tuple_size<const _Tp>
29 : public tuple_size<_Tp> {};
32 class _LIBCPP_TYPE_VIS_ONLY tuple_size<volatile _Tp>
33 : public tuple_size<_Tp> {};
36 class _LIBCPP_TYPE_VIS_ONLY tuple_size<const volatile _Tp>
37 : public tuple_size<_Tp> {};
39 template <size_t _Ip, class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_element;
41 template <size_t _Ip, class _Tp>
42 class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const _Tp>
45 typedef typename add_const<typename tuple_element<_Ip, _Tp>::type>::type type;
48 template <size_t _Ip, class _Tp>
49 class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, volatile _Tp>
52 typedef typename add_volatile<typename tuple_element<_Ip, _Tp>::type>::type type;
55 template <size_t _Ip, class _Tp>
56 class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const volatile _Tp>
59 typedef typename add_cv<typename tuple_element<_Ip, _Tp>::type>::type type;
62 template <class _Tp> struct __tuple_like : false_type {};
64 template <class _Tp> struct __tuple_like<const _Tp> : public __tuple_like<_Tp> {};
65 template <class _Tp> struct __tuple_like<volatile _Tp> : public __tuple_like<_Tp> {};
66 template <class _Tp> struct __tuple_like<const volatile _Tp> : public __tuple_like<_Tp> {};
68 // tuple specializations
70 #if !defined(_LIBCPP_HAS_NO_VARIADICS)
71 template <class ..._Tp> class _LIBCPP_TYPE_VIS_ONLY tuple;
73 template <class... _Tp> struct __tuple_like<tuple<_Tp...> > : true_type {};
75 template <size_t _Ip, class ..._Tp>
76 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
77 typename tuple_element<_Ip, tuple<_Tp...> >::type&
78 get(tuple<_Tp...>&) _NOEXCEPT;
80 template <size_t _Ip, class ..._Tp>
81 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
82 const typename tuple_element<_Ip, tuple<_Tp...> >::type&
83 get(const tuple<_Tp...>&) _NOEXCEPT;
85 template <size_t _Ip, class ..._Tp>
86 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
87 typename tuple_element<_Ip, tuple<_Tp...> >::type&&
88 get(tuple<_Tp...>&&) _NOEXCEPT;
91 // pair specializations
93 template <class _T1, class _T2> struct _LIBCPP_TYPE_VIS_ONLY pair;
95 template <class _T1, class _T2> struct __tuple_like<pair<_T1, _T2> > : true_type {};
97 template <size_t _Ip, class _T1, class _T2>
98 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
99 typename tuple_element<_Ip, pair<_T1, _T2> >::type&
100 get(pair<_T1, _T2>&) _NOEXCEPT;
102 template <size_t _Ip, class _T1, class _T2>
103 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
104 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
105 get(const pair<_T1, _T2>&) _NOEXCEPT;
107 #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
108 template <size_t _Ip, class _T1, class _T2>
109 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
110 typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
111 get(pair<_T1, _T2>&&) _NOEXCEPT;
114 // array specializations
116 template <class _Tp, size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY array;
118 template <class _Tp, size_t _Size> struct __tuple_like<array<_Tp, _Size> > : true_type {};
120 template <size_t _Ip, class _Tp, size_t _Size>
121 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
123 get(array<_Tp, _Size>&) _NOEXCEPT;
125 template <size_t _Ip, class _Tp, size_t _Size>
126 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
128 get(const array<_Tp, _Size>&) _NOEXCEPT;
130 #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
131 template <size_t _Ip, class _Tp, size_t _Size>
132 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
134 get(array<_Tp, _Size>&&) _NOEXCEPT;
137 #if !defined(_LIBCPP_HAS_NO_VARIADICS)
141 template <bool _Last, class ..._Preds>
142 struct __lazy_and_impl;
144 template <class ..._Preds>
145 struct __lazy_and_impl<false, _Preds...> : false_type {};
148 struct __lazy_and_impl<true> : true_type {};
150 template <class _Pred>
151 struct __lazy_and_impl<true, _Pred> : integral_constant<bool, _Pred::type::value> {};
153 template <class _Hp, class ..._Tp>
154 struct __lazy_and_impl<true, _Hp, _Tp...> : __lazy_and_impl<_Hp::type::value, _Tp...> {};
156 template <class _P1, class ..._Pr>
157 struct __lazy_and : __lazy_and_impl<_P1::type::value, _Pr...> {};
161 template <class _Pred>
162 struct __lazy_not : integral_constant<bool, !_Pred::type::value> {};
164 // __make_tuple_indices
166 template <size_t...> struct __tuple_indices {};
168 template <size_t _Sp, class _IntTuple, size_t _Ep>
169 struct __make_indices_imp;
171 template <size_t _Sp, size_t ..._Indices, size_t _Ep>
172 struct __make_indices_imp<_Sp, __tuple_indices<_Indices...>, _Ep>
174 typedef typename __make_indices_imp<_Sp+1, __tuple_indices<_Indices..., _Sp>, _Ep>::type type;
177 template <size_t _Ep, size_t ..._Indices>
178 struct __make_indices_imp<_Ep, __tuple_indices<_Indices...>, _Ep>
180 typedef __tuple_indices<_Indices...> type;
183 template <size_t _Ep, size_t _Sp = 0>
184 struct __make_tuple_indices
186 static_assert(_Sp <= _Ep, "__make_tuple_indices input error");
187 typedef typename __make_indices_imp<_Sp, __tuple_indices<>, _Ep>::type type;
192 template <class ..._Tp> struct __tuple_types {};
194 template <size_t _Ip>
195 class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<> >
198 static_assert(_Ip == 0, "tuple_element index out of range");
199 static_assert(_Ip != 0, "tuple_element index out of range");
202 template <class _Hp, class ..._Tp>
203 class _LIBCPP_TYPE_VIS_ONLY tuple_element<0, __tuple_types<_Hp, _Tp...> >
209 template <size_t _Ip, class _Hp, class ..._Tp>
210 class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<_Hp, _Tp...> >
213 typedef typename tuple_element<_Ip-1, __tuple_types<_Tp...> >::type type;
216 template <class ..._Tp>
217 class _LIBCPP_TYPE_VIS_ONLY tuple_size<__tuple_types<_Tp...> >
218 : public integral_constant<size_t, sizeof...(_Tp)>
222 template <class... _Tp> struct __tuple_like<__tuple_types<_Tp...> > : true_type {};
224 // __make_tuple_types
226 // __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a
227 // __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep).
228 // _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>. If _Tuple is a
229 // lvalue_reference type, then __tuple_types<_Types&...> is the result.
231 template <class _TupleTypes, class _Tp, size_t _Sp, size_t _Ep>
232 struct __make_tuple_types_imp;
234 template <class ..._Types, class _Tp, size_t _Sp, size_t _Ep>
235 struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Sp, _Ep>
237 typedef typename remove_reference<_Tp>::type _Tpr;
238 typedef typename __make_tuple_types_imp<__tuple_types<_Types...,
239 typename conditional<is_lvalue_reference<_Tp>::value,
240 typename tuple_element<_Sp, _Tpr>::type&,
241 typename tuple_element<_Sp, _Tpr>::type>::type>,
242 _Tp, _Sp+1, _Ep>::type type;
245 template <class ..._Types, class _Tp, size_t _Ep>
246 struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Ep, _Ep>
248 typedef __tuple_types<_Types...> type;
251 template <class _Tp, size_t _Ep = tuple_size<typename remove_reference<_Tp>::type>::value, size_t _Sp = 0>
252 struct __make_tuple_types
254 static_assert(_Sp <= _Ep, "__make_tuple_types input error");
255 typedef typename __make_tuple_types_imp<__tuple_types<>, _Tp, _Sp, _Ep>::type type;
258 // __tuple_convertible
260 template <class, class>
261 struct __tuple_convertible_imp : public false_type {};
263 template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
264 struct __tuple_convertible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
265 : public integral_constant<bool,
266 is_convertible<_Tp0, _Up0>::value &&
267 __tuple_convertible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
270 struct __tuple_convertible_imp<__tuple_types<>, __tuple_types<> >
271 : public true_type {};
273 template <bool, class, class>
274 struct __tuple_convertible_apply : public false_type {};
276 template <class _Tp, class _Up>
277 struct __tuple_convertible_apply<true, _Tp, _Up>
278 : public __tuple_convertible_imp<
279 typename __make_tuple_types<_Tp>::type
280 , typename __make_tuple_types<_Up>::type
284 template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
285 bool = __tuple_like<_Up>::value>
286 struct __tuple_convertible
287 : public false_type {};
289 template <class _Tp, class _Up>
290 struct __tuple_convertible<_Tp, _Up, true, true>
291 : public __tuple_convertible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
292 tuple_size<_Up>::value, _Tp, _Up>
295 // __tuple_constructible
297 template <class, class>
298 struct __tuple_constructible_imp : public false_type {};
300 template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
301 struct __tuple_constructible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
302 : public integral_constant<bool,
303 is_constructible<_Up0, _Tp0>::value &&
304 __tuple_constructible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
307 struct __tuple_constructible_imp<__tuple_types<>, __tuple_types<> >
308 : public true_type {};
310 template <bool _SameSize, class, class>
311 struct __tuple_constructible_apply : public false_type {};
313 template <class _Tp, class _Up>
314 struct __tuple_constructible_apply<true, _Tp, _Up>
315 : public __tuple_constructible_imp<
316 typename __make_tuple_types<_Tp>::type
317 , typename __make_tuple_types<_Up>::type
321 template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
322 bool = __tuple_like<_Up>::value>
323 struct __tuple_constructible
324 : public false_type {};
326 template <class _Tp, class _Up>
327 struct __tuple_constructible<_Tp, _Up, true, true>
328 : public __tuple_constructible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
329 tuple_size<_Up>::value, _Tp, _Up>
332 // __tuple_assignable
334 template <class, class>
335 struct __tuple_assignable_imp : public false_type {};
337 template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
338 struct __tuple_assignable_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
339 : public integral_constant<bool,
340 is_assignable<_Up0&, _Tp0>::value &&
341 __tuple_assignable_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
344 struct __tuple_assignable_imp<__tuple_types<>, __tuple_types<> >
345 : public true_type {};
347 template <bool, class, class>
348 struct __tuple_assignable_apply : public false_type {};
350 template <class _Tp, class _Up>
351 struct __tuple_assignable_apply<true, _Tp, _Up>
352 : __tuple_assignable_imp<
353 typename __make_tuple_types<_Tp>::type
354 , typename __make_tuple_types<_Up>::type
358 template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
359 bool = __tuple_like<_Up>::value>
360 struct __tuple_assignable
361 : public false_type {};
363 template <class _Tp, class _Up>
364 struct __tuple_assignable<_Tp, _Up, true, true>
365 : public __tuple_assignable_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
366 tuple_size<_Up>::value, _Tp, _Up>
369 #endif // _LIBCPP_HAS_NO_VARIADICS
371 _LIBCPP_END_NAMESPACE_STD
373 #endif // _LIBCPP___TUPLE