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)
139 // __make_tuple_indices
141 template <size_t...> struct __tuple_indices {};
143 template <size_t _Sp, class _IntTuple, size_t _Ep>
144 struct __make_indices_imp;
146 template <size_t _Sp, size_t ..._Indices, size_t _Ep>
147 struct __make_indices_imp<_Sp, __tuple_indices<_Indices...>, _Ep>
149 typedef typename __make_indices_imp<_Sp+1, __tuple_indices<_Indices..., _Sp>, _Ep>::type type;
152 template <size_t _Ep, size_t ..._Indices>
153 struct __make_indices_imp<_Ep, __tuple_indices<_Indices...>, _Ep>
155 typedef __tuple_indices<_Indices...> type;
158 template <size_t _Ep, size_t _Sp = 0>
159 struct __make_tuple_indices
161 static_assert(_Sp <= _Ep, "__make_tuple_indices input error");
162 typedef typename __make_indices_imp<_Sp, __tuple_indices<>, _Ep>::type type;
167 template <class ..._Tp> struct __tuple_types {};
169 template <size_t _Ip>
170 class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<> >
173 static_assert(_Ip == 0, "tuple_element index out of range");
174 static_assert(_Ip != 0, "tuple_element index out of range");
177 template <class _Hp, class ..._Tp>
178 class _LIBCPP_TYPE_VIS_ONLY tuple_element<0, __tuple_types<_Hp, _Tp...> >
184 template <size_t _Ip, class _Hp, class ..._Tp>
185 class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<_Hp, _Tp...> >
188 typedef typename tuple_element<_Ip-1, __tuple_types<_Tp...> >::type type;
191 template <class ..._Tp>
192 class _LIBCPP_TYPE_VIS_ONLY tuple_size<__tuple_types<_Tp...> >
193 : public integral_constant<size_t, sizeof...(_Tp)>
197 template <class... _Tp> struct __tuple_like<__tuple_types<_Tp...> > : true_type {};
199 // __make_tuple_types
201 // __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a
202 // __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep).
203 // _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>. If _Tuple is a
204 // lvalue_reference type, then __tuple_types<_Types&...> is the result.
206 template <class _TupleTypes, class _Tp, size_t _Sp, size_t _Ep>
207 struct __make_tuple_types_imp;
209 template <class ..._Types, class _Tp, size_t _Sp, size_t _Ep>
210 struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Sp, _Ep>
212 typedef typename remove_reference<_Tp>::type _Tpr;
213 typedef typename __make_tuple_types_imp<__tuple_types<_Types...,
214 typename conditional<is_lvalue_reference<_Tp>::value,
215 typename tuple_element<_Sp, _Tpr>::type&,
216 typename tuple_element<_Sp, _Tpr>::type>::type>,
217 _Tp, _Sp+1, _Ep>::type type;
220 template <class ..._Types, class _Tp, size_t _Ep>
221 struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Ep, _Ep>
223 typedef __tuple_types<_Types...> type;
226 template <class _Tp, size_t _Ep = tuple_size<typename remove_reference<_Tp>::type>::value, size_t _Sp = 0>
227 struct __make_tuple_types
229 static_assert(_Sp <= _Ep, "__make_tuple_types input error");
230 typedef typename __make_tuple_types_imp<__tuple_types<>, _Tp, _Sp, _Ep>::type type;
233 // __tuple_convertible
235 template <class, class>
236 struct __tuple_convertible_imp : public false_type {};
238 template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
239 struct __tuple_convertible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
240 : public integral_constant<bool,
241 is_convertible<_Tp0, _Up0>::value &&
242 __tuple_convertible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
245 struct __tuple_convertible_imp<__tuple_types<>, __tuple_types<> >
246 : public true_type {};
248 template <bool, class, class>
249 struct __tuple_convertible_apply : public false_type {};
251 template <class _Tp, class _Up>
252 struct __tuple_convertible_apply<true, _Tp, _Up>
253 : public __tuple_convertible_imp<
254 typename __make_tuple_types<_Tp>::type
255 , typename __make_tuple_types<_Up>::type
259 template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
260 bool = __tuple_like<_Up>::value>
261 struct __tuple_convertible
262 : public false_type {};
264 template <class _Tp, class _Up>
265 struct __tuple_convertible<_Tp, _Up, true, true>
266 : public __tuple_convertible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
267 tuple_size<_Up>::value, _Tp, _Up>
270 // __tuple_constructible
272 template <class, class>
273 struct __tuple_constructible_imp : public false_type {};
275 template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
276 struct __tuple_constructible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
277 : public integral_constant<bool,
278 is_constructible<_Up0, _Tp0>::value &&
279 __tuple_constructible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
282 struct __tuple_constructible_imp<__tuple_types<>, __tuple_types<> >
283 : public true_type {};
285 template <bool _SameSize, class, class>
286 struct __tuple_constructible_apply : public false_type {};
288 template <class _Tp, class _Up>
289 struct __tuple_constructible_apply<true, _Tp, _Up>
290 : public __tuple_constructible_imp<
291 typename __make_tuple_types<_Tp>::type
292 , typename __make_tuple_types<_Up>::type
296 template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
297 bool = __tuple_like<_Up>::value>
298 struct __tuple_constructible
299 : public false_type {};
301 template <class _Tp, class _Up>
302 struct __tuple_constructible<_Tp, _Up, true, true>
303 : public __tuple_constructible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
304 tuple_size<_Up>::value, _Tp, _Up>
307 // __tuple_assignable
309 template <class, class>
310 struct __tuple_assignable_imp : public false_type {};
312 template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
313 struct __tuple_assignable_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
314 : public integral_constant<bool,
315 is_assignable<_Up0&, _Tp0>::value &&
316 __tuple_assignable_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
319 struct __tuple_assignable_imp<__tuple_types<>, __tuple_types<> >
320 : public true_type {};
322 template <bool, class, class>
323 struct __tuple_assignable_apply : public false_type {};
325 template <class _Tp, class _Up>
326 struct __tuple_assignable_apply<true, _Tp, _Up>
327 : __tuple_assignable_imp<
328 typename __make_tuple_types<_Tp>::type
329 , typename __make_tuple_types<_Up>::type
333 template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
334 bool = __tuple_like<_Up>::value>
335 struct __tuple_assignable
336 : public false_type {};
338 template <class _Tp, class _Up>
339 struct __tuple_assignable<_Tp, _Up, true, true>
340 : public __tuple_assignable_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
341 tuple_size<_Up>::value, _Tp, _Up>
344 #endif // _LIBCPP_HAS_NO_VARIADICS
346 _LIBCPP_END_NAMESPACE_STD
348 #endif // _LIBCPP___TUPLE