blob: a485d7d1d80159e33e36c92cfcaad90965561d0f [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- tuple ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
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&...);
25 template <class... U>
26 explicit tuple(U&&...);
27 tuple(const tuple&) = default;
Howard Hinnanta5e01212011-05-27 19:08:18 +000028 tuple(tuple&&) = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029 template <class... U>
30 tuple(const tuple<U...>&);
31 template <class... U>
32 tuple(tuple<U...>&&);
33 template <class U1, class U2>
34 tuple(const pair<U1, U2>&); // iff sizeof...(T) == 2
35 template <class U1, class U2>
36 tuple(pair<U1, U2>&&); // iff sizeof...(T) == 2
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&);
Howard Hinnanta5e01212011-05-27 19:08:18 +000059 tuple&
60 operator=(tuple&&) noexcept(AND(is_nothrow_move_assignable<T>::value ...));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000061 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
Howard Hinnanta5e01212011-05-27 19:08:18 +000070 void swap(tuple&) noexcept(AND(swap(declval<T&>(), declval<T&>())...));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071};
72
73const unspecified ignore;
74
75template <class... T> tuple<V...> make_tuple(T&&...);
Howard Hinnanta5e01212011-05-27 19:08:18 +000076template <class... T> tuple<ATypes...> forward_as_tuple(T&&...) noexcept;
77template <class... T> tuple<T&...> tie(T&...) noexcept;
Howard Hinnant0e1493e2010-12-11 20:47:50 +000078template <class... Tuples> tuple<CTypes...> tuple_cat(Tuples&&... tpls);
79
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080// 20.4.1.4, tuple helper classes:
81template <class T> class tuple_size; // undefined
82template <class... T> class tuple_size<tuple<T...>>;
83template <intsize_t I, class T> class tuple_element; // undefined
84template <intsize_t I, class... T> class tuple_element<I, tuple<T...>>;
85
86// 20.4.1.5, element access:
Howard Hinnanta5e01212011-05-27 19:08:18 +000087template <intsize_t I, class... T>
88 typename tuple_element<I, tuple<T...>>::type&
89 get(tuple<T...>&) noexcept;
90template <intsize_t I, class... T>
91 typename tuple_element<I, tuple<T...>>::type const&
92 get(const tuple<T...>&) noexcept;
93template <intsize_t I, class... T>
94 typename tuple_element<I, tuple<T...>>::type&&
95 get(tuple<T...>&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096
97// 20.4.1.6, relational operators:
98template<class... T, class... U> bool operator==(const tuple<T...>&, const tuple<U...>&);
99template<class... T, class... U> bool operator<(const tuple<T...>&, const tuple<U...>&);
100template<class... T, class... U> bool operator!=(const tuple<T...>&, const tuple<U...>&);
101template<class... T, class... U> bool operator>(const tuple<T...>&, const tuple<U...>&);
102template<class... T, class... U> bool operator<=(const tuple<T...>&, const tuple<U...>&);
103template<class... T, class... U> bool operator>=(const tuple<T...>&, const tuple<U...>&);
104
105template <class... Types, class Alloc>
106 struct uses_allocator<tuple<Types...>, Alloc>;
107
108template <class... Types>
Howard Hinnanta5e01212011-05-27 19:08:18 +0000109 void
110 swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112} // std
113
114*/
115
116#include <__config>
117#include <__tuple>
118#include <cstddef>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119#include <type_traits>
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000120#include <__functional_base>
121#include <utility>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122
Howard Hinnant08e17472011-10-17 20:05:10 +0000123#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000125#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126
127_LIBCPP_BEGIN_NAMESPACE_STD
128
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000129// allocator_arg_t
130
131struct _LIBCPP_VISIBLE allocator_arg_t { };
132
133extern const allocator_arg_t allocator_arg;
134
135// uses_allocator
136
137template <class _Tp>
138struct __has_allocator_type
139{
140private:
141 struct __two {char _; char __;};
142 template <class _Up> static __two __test(...);
143 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
144public:
145 static const bool value = sizeof(__test<_Tp>(0)) == 1;
146};
147
148template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
149struct __uses_allocator
150 : public integral_constant<bool,
151 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
152{
153};
154
155template <class _Tp, class _Alloc>
156struct __uses_allocator<_Tp, _Alloc, false>
157 : public false_type
158{
159};
160
161template <class _Tp, class _Alloc>
162struct _LIBCPP_VISIBLE uses_allocator
163 : public __uses_allocator<_Tp, _Alloc>
164{
165};
166
167#ifndef _LIBCPP_HAS_NO_VARIADICS
168
169// uses-allocator construction
170
171template <class _Tp, class _Alloc, class ..._Args>
172struct __uses_alloc_ctor_imp
173{
174 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
175 static const bool __ic =
176 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
177 static const int value = __ua ? 2 - __ic : 0;
178};
179
180template <class _Tp, class _Alloc, class ..._Args>
181struct __uses_alloc_ctor
182 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
183 {};
184
185#endif // _LIBCPP_HAS_NO_VARIADICS
186
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187#ifndef _LIBCPP_HAS_NO_VARIADICS
188
189// tuple_size
190
191template <class ..._Tp>
Howard Hinnantf83417b2011-01-24 16:07:25 +0000192class _LIBCPP_VISIBLE tuple_size<tuple<_Tp...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193 : public integral_constant<size_t, sizeof...(_Tp)>
194{
195};
196
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197// tuple_element
198
199template <size_t _Ip, class ..._Tp>
Howard Hinnantf83417b2011-01-24 16:07:25 +0000200class _LIBCPP_VISIBLE tuple_element<_Ip, tuple<_Tp...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201{
202public:
Howard Hinnantf83417b2011-01-24 16:07:25 +0000203 typedef typename tuple_element<_Ip, __tuple_types<_Tp...> >::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204};
205
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206// __tuple_leaf
207
Howard Hinnantd4cf2152011-12-11 20:31:33 +0000208template <size_t _Ip, class _Hp, bool=is_empty<_Hp>::value
209#if __has_feature(is_final)
210 && !__is_final(_Hp)
211#endif
212 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213class __tuple_leaf;
214
215template <size_t _Ip, class _Hp, bool _Ep>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000216inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217void swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y)
Howard Hinnanta5e01212011-05-27 19:08:18 +0000218 _NOEXCEPT_(__is_nothrow_swappable<_Hp>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219{
220 swap(__x.get(), __y.get());
221}
222
223template <size_t _Ip, class _Hp, bool>
224class __tuple_leaf
225{
226 _Hp value;
227
228 __tuple_leaf& operator=(const __tuple_leaf&);
229public:
Howard Hinnant5394c1e2012-07-06 20:39:45 +0000230 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __tuple_leaf() : value()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231 {static_assert(!is_reference<_Hp>::value,
232 "Attempted to default construct a reference element in a tuple");}
233
234 template <class _Alloc>
235 _LIBCPP_INLINE_VISIBILITY
236 __tuple_leaf(integral_constant<int, 0>, const _Alloc&)
237 : value()
238 {static_assert(!is_reference<_Hp>::value,
239 "Attempted to default construct a reference element in a tuple");}
240
241 template <class _Alloc>
242 _LIBCPP_INLINE_VISIBILITY
243 __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
244 : value(allocator_arg_t(), __a)
245 {static_assert(!is_reference<_Hp>::value,
246 "Attempted to default construct a reference element in a tuple");}
247
248 template <class _Alloc>
249 _LIBCPP_INLINE_VISIBILITY
250 __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
251 : value(__a)
252 {static_assert(!is_reference<_Hp>::value,
253 "Attempted to default construct a reference element in a tuple");}
254
Howard Hinnante049cc52010-09-27 17:54:17 +0000255 template <class _Tp,
256 class = typename enable_if<is_constructible<_Hp, _Tp>::value>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257 _LIBCPP_INLINE_VISIBILITY
258 explicit __tuple_leaf(_Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000259 : value(_VSTD::forward<_Tp>(__t))
Howard Hinnante049cc52010-09-27 17:54:17 +0000260 {static_assert(!is_reference<_Hp>::value ||
Howard Hinnantec3773c2011-12-01 20:21:04 +0000261 (is_lvalue_reference<_Hp>::value &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 (is_lvalue_reference<_Tp>::value ||
263 is_same<typename remove_reference<_Tp>::type,
264 reference_wrapper<
265 typename remove_reference<_Hp>::type
266 >
Howard Hinnantec3773c2011-12-01 20:21:04 +0000267 >::value)) ||
Howard Hinnante049cc52010-09-27 17:54:17 +0000268 (is_rvalue_reference<_Hp>::value &&
269 !is_lvalue_reference<_Tp>::value),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270 "Attempted to construct a reference element in a tuple with an rvalue");}
271
272 template <class _Tp, class _Alloc>
273 _LIBCPP_INLINE_VISIBILITY
274 explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000275 : value(_VSTD::forward<_Tp>(__t))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276 {static_assert(!is_lvalue_reference<_Hp>::value ||
Howard Hinnantec3773c2011-12-01 20:21:04 +0000277 (is_lvalue_reference<_Hp>::value &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278 (is_lvalue_reference<_Tp>::value ||
279 is_same<typename remove_reference<_Tp>::type,
280 reference_wrapper<
281 typename remove_reference<_Hp>::type
282 >
Howard Hinnantec3773c2011-12-01 20:21:04 +0000283 >::value)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284 "Attempted to construct a reference element in a tuple with an rvalue");}
285
286 template <class _Tp, class _Alloc>
287 _LIBCPP_INLINE_VISIBILITY
288 explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000289 : value(allocator_arg_t(), __a, _VSTD::forward<_Tp>(__t))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290 {static_assert(!is_lvalue_reference<_Hp>::value ||
Howard Hinnantec3773c2011-12-01 20:21:04 +0000291 (is_lvalue_reference<_Hp>::value &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 (is_lvalue_reference<_Tp>::value ||
293 is_same<typename remove_reference<_Tp>::type,
294 reference_wrapper<
295 typename remove_reference<_Hp>::type
296 >
Howard Hinnantec3773c2011-12-01 20:21:04 +0000297 >::value)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 "Attempted to construct a reference element in a tuple with an rvalue");}
299
300 template <class _Tp, class _Alloc>
301 _LIBCPP_INLINE_VISIBILITY
302 explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000303 : value(_VSTD::forward<_Tp>(__t), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304 {static_assert(!is_lvalue_reference<_Hp>::value ||
Howard Hinnantec3773c2011-12-01 20:21:04 +0000305 (is_lvalue_reference<_Hp>::value &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 (is_lvalue_reference<_Tp>::value ||
307 is_same<typename remove_reference<_Tp>::type,
308 reference_wrapper<
309 typename remove_reference<_Hp>::type
310 >
Howard Hinnantec3773c2011-12-01 20:21:04 +0000311 >::value)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000312 "Attempted to construct a reference element in a tuple with an rvalue");}
313
Howard Hinnante049cc52010-09-27 17:54:17 +0000314 __tuple_leaf(const __tuple_leaf& __t)
315 : value(__t.get())
316 {static_assert(!is_rvalue_reference<_Hp>::value, "Can not copy a tuple with rvalue reference member");}
317
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318 template <class _Tp>
319 _LIBCPP_INLINE_VISIBILITY
320 explicit __tuple_leaf(const __tuple_leaf<_Ip, _Tp>& __t)
321 : value(__t.get()) {}
322
323 template <class _Tp>
324 _LIBCPP_INLINE_VISIBILITY
325 __tuple_leaf&
326 operator=(_Tp&& __t)
327 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000328 value = _VSTD::forward<_Tp>(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329 return *this;
330 }
331
332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta5e01212011-05-27 19:08:18 +0000333 int swap(__tuple_leaf& __t) _NOEXCEPT_(__is_nothrow_swappable<__tuple_leaf>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000335 _VSTD::swap(*this, __t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000336 return 0;
337 }
338
339 _LIBCPP_INLINE_VISIBILITY _Hp& get() {return value;}
340 _LIBCPP_INLINE_VISIBILITY const _Hp& get() const {return value;}
341};
342
343template <size_t _Ip, class _Hp>
344class __tuple_leaf<_Ip, _Hp, true>
345 : private _Hp
346{
347
348 __tuple_leaf& operator=(const __tuple_leaf&);
349public:
Howard Hinnant5394c1e2012-07-06 20:39:45 +0000350 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __tuple_leaf() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
352 template <class _Alloc>
353 _LIBCPP_INLINE_VISIBILITY
354 __tuple_leaf(integral_constant<int, 0>, const _Alloc&) {}
355
356 template <class _Alloc>
357 _LIBCPP_INLINE_VISIBILITY
358 __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
359 : _Hp(allocator_arg_t(), __a) {}
360
361 template <class _Alloc>
362 _LIBCPP_INLINE_VISIBILITY
363 __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
364 : _Hp(__a) {}
365
Howard Hinnante049cc52010-09-27 17:54:17 +0000366 template <class _Tp,
367 class = typename enable_if<is_constructible<_Hp, _Tp>::value>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 _LIBCPP_INLINE_VISIBILITY
369 explicit __tuple_leaf(_Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000370 : _Hp(_VSTD::forward<_Tp>(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372 template <class _Tp, class _Alloc>
373 _LIBCPP_INLINE_VISIBILITY
374 explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000375 : _Hp(_VSTD::forward<_Tp>(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
377 template <class _Tp, class _Alloc>
378 _LIBCPP_INLINE_VISIBILITY
379 explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000380 : _Hp(allocator_arg_t(), __a, _VSTD::forward<_Tp>(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382 template <class _Tp, class _Alloc>
383 _LIBCPP_INLINE_VISIBILITY
384 explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000385 : _Hp(_VSTD::forward<_Tp>(__t), __a) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386
387 template <class _Tp>
388 _LIBCPP_INLINE_VISIBILITY
389 explicit __tuple_leaf(const __tuple_leaf<_Ip, _Tp>& __t)
390 : _Hp(__t.get()) {}
391
392 template <class _Tp>
393 _LIBCPP_INLINE_VISIBILITY
394 __tuple_leaf&
395 operator=(_Tp&& __t)
396 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000397 _Hp::operator=(_VSTD::forward<_Tp>(__t));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398 return *this;
399 }
400
Howard Hinnanta5e01212011-05-27 19:08:18 +0000401 _LIBCPP_INLINE_VISIBILITY
402 int
403 swap(__tuple_leaf& __t) _NOEXCEPT_(__is_nothrow_swappable<__tuple_leaf>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000405 _VSTD::swap(*this, __t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406 return 0;
407 }
408
409 _LIBCPP_INLINE_VISIBILITY _Hp& get() {return static_cast<_Hp&>(*this);}
410 _LIBCPP_INLINE_VISIBILITY const _Hp& get() const {return static_cast<const _Hp&>(*this);}
411};
412
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000413template <class ..._Tp>
414_LIBCPP_INLINE_VISIBILITY
415void __swallow(_Tp&&...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416
Howard Hinnanta5e01212011-05-27 19:08:18 +0000417template <bool ...> struct __all;
418
419template <>
420struct __all<>
421{
422 static const bool value = true;
423};
424
Howard Hinnant99968442011-11-29 18:15:50 +0000425template <bool _B0, bool ... _Bp>
426struct __all<_B0, _Bp...>
Howard Hinnanta5e01212011-05-27 19:08:18 +0000427{
Howard Hinnant99968442011-11-29 18:15:50 +0000428 static const bool value = _B0 && __all<_Bp...>::value;
Howard Hinnanta5e01212011-05-27 19:08:18 +0000429};
430
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431// __tuple_impl
432
433template<class _Indx, class ..._Tp> struct __tuple_impl;
434
435template<size_t ..._Indx, class ..._Tp>
436struct __tuple_impl<__tuple_indices<_Indx...>, _Tp...>
437 : public __tuple_leaf<_Indx, _Tp>...
438{
Howard Hinnant5394c1e2012-07-06 20:39:45 +0000439 _LIBCPP_INLINE_VISIBILITY
440 _LIBCPP_CONSTEXPR __tuple_impl() {}
441
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442 template <size_t ..._Uf, class ..._Tf,
443 size_t ..._Ul, class ..._Tl, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445 explicit
446 __tuple_impl(__tuple_indices<_Uf...>, __tuple_types<_Tf...>,
447 __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
448 _Up&&... __u) :
Howard Hinnant0949eed2011-06-30 21:18:19 +0000449 __tuple_leaf<_Uf, _Tf>(_VSTD::forward<_Up>(__u))...,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450 __tuple_leaf<_Ul, _Tl>()...
451 {}
452
453 template <class _Alloc, size_t ..._Uf, class ..._Tf,
454 size_t ..._Ul, class ..._Tl, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456 explicit
457 __tuple_impl(allocator_arg_t, const _Alloc& __a,
458 __tuple_indices<_Uf...>, __tuple_types<_Tf...>,
459 __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
460 _Up&&... __u) :
461 __tuple_leaf<_Uf, _Tf>(__uses_alloc_ctor<_Tf, _Alloc, _Up>(), __a,
Howard Hinnant0949eed2011-06-30 21:18:19 +0000462 _VSTD::forward<_Up>(__u))...,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463 __tuple_leaf<_Ul, _Tl>(__uses_alloc_ctor<_Tl, _Alloc>(), __a)...
464 {}
465
466 template <class _Tuple,
467 class = typename enable_if
468 <
Howard Hinnantf83417b2011-01-24 16:07:25 +0000469 __tuple_convertible<_Tuple, tuple<_Tp...> >::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470 >::type
471 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473 __tuple_impl(_Tuple&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000474 : __tuple_leaf<_Indx, _Tp>(_VSTD::forward<typename tuple_element<_Indx,
475 typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476 {}
477
478 template <class _Alloc, class _Tuple,
479 class = typename enable_if
480 <
Howard Hinnantf83417b2011-01-24 16:07:25 +0000481 __tuple_convertible<_Tuple, tuple<_Tp...> >::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482 >::type
483 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485 __tuple_impl(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
486 : __tuple_leaf<_Indx, _Tp>(__uses_alloc_ctor<_Tp, _Alloc, typename tuple_element<_Indx,
Howard Hinnant324bb032010-08-22 00:02:43 +0000487 typename __make_tuple_types<_Tuple>::type>::type>(), __a,
Howard Hinnant0949eed2011-06-30 21:18:19 +0000488 _VSTD::forward<typename tuple_element<_Indx,
489 typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490 {}
491
492 template <class _Tuple>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494 typename enable_if
495 <
Howard Hinnantf83417b2011-01-24 16:07:25 +0000496 __tuple_assignable<_Tuple, tuple<_Tp...> >::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497 __tuple_impl&
498 >::type
499 operator=(_Tuple&& __t)
500 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000501 __swallow(__tuple_leaf<_Indx, _Tp>::operator=(_VSTD::forward<typename tuple_element<_Indx,
502 typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<_Indx>(__t)))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503 return *this;
504 }
505
Howard Hinnant28484442012-02-15 20:13:52 +0000506 _LIBCPP_INLINE_VISIBILITY
507 __tuple_impl&
508 operator=(const __tuple_impl& __t)
509 {
510 __swallow(__tuple_leaf<_Indx, _Tp>::operator=(static_cast<const __tuple_leaf<_Indx, _Tp>&>(__t).get())...);
511 return *this;
512 }
513
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515 void swap(__tuple_impl& __t)
Howard Hinnanta5e01212011-05-27 19:08:18 +0000516 _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 {
518 __swallow(__tuple_leaf<_Indx, _Tp>::swap(static_cast<__tuple_leaf<_Indx, _Tp>&>(__t))...);
519 }
520};
521
522template <class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000523class _LIBCPP_VISIBLE tuple
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524{
525 typedef __tuple_impl<typename __make_tuple_indices<sizeof...(_Tp)>::type, _Tp...> base;
526
527 base base_;
528
529 template <size_t _Jp, class ..._Up> friend
Howard Hinnantec3773c2011-12-01 20:21:04 +0000530 typename tuple_element<_Jp, tuple<_Up...> >::type& get(tuple<_Up...>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000531 template <size_t _Jp, class ..._Up> friend
Howard Hinnantec3773c2011-12-01 20:21:04 +0000532 const typename tuple_element<_Jp, tuple<_Up...> >::type& get(const tuple<_Up...>&) _NOEXCEPT;
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000533 template <size_t _Jp, class ..._Up> friend
Howard Hinnantec3773c2011-12-01 20:21:04 +0000534 typename tuple_element<_Jp, tuple<_Up...> >::type&& get(tuple<_Up...>&&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000535public:
536
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5394c1e2012-07-06 20:39:45 +0000538 _LIBCPP_CONSTEXPR tuple() {}
539
540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 explicit tuple(const _Tp& ... __t)
542 : base_(typename __make_tuple_indices<sizeof...(_Tp)>::type(),
543 typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
544 typename __make_tuple_indices<0>::type(),
545 typename __make_tuple_types<tuple, 0>::type(),
546 __t...
547 ) {}
548
549 template <class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551 tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
552 : base_(allocator_arg_t(), __a,
553 typename __make_tuple_indices<sizeof...(_Tp)>::type(),
554 typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
555 typename __make_tuple_indices<0>::type(),
556 typename __make_tuple_types<tuple, 0>::type(),
557 __t...
558 ) {}
559
560 template <class ..._Up,
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000561 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 <
563 sizeof...(_Up) <= sizeof...(_Tp) &&
564 __tuple_convertible
565 <
566 tuple<_Up...>,
567 typename __make_tuple_types<tuple,
568 sizeof...(_Up) < sizeof...(_Tp) ?
569 sizeof...(_Up) :
570 sizeof...(_Tp)>::type
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000571 >::value,
572 bool
573 >::type = false
574 >
575 _LIBCPP_INLINE_VISIBILITY
576 tuple(_Up&&... __u)
577 : base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
578 typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
579 typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
580 typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
581 _VSTD::forward<_Up>(__u)...) {}
582
583 template <class ..._Up,
584 typename enable_if
585 <
586 sizeof...(_Up) <= sizeof...(_Tp) &&
587 __tuple_constructible
588 <
589 tuple<_Up...>,
590 typename __make_tuple_types<tuple,
591 sizeof...(_Up) < sizeof...(_Tp) ?
592 sizeof...(_Up) :
593 sizeof...(_Tp)>::type
594 >::value &&
595 !__tuple_convertible
596 <
597 tuple<_Up...>,
598 typename __make_tuple_types<tuple,
599 sizeof...(_Up) < sizeof...(_Tp) ?
600 sizeof...(_Up) :
601 sizeof...(_Tp)>::type
602 >::value,
603 bool
604 >::type =false
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607 explicit
608 tuple(_Up&&... __u)
609 : base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
610 typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
611 typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
612 typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000613 _VSTD::forward<_Up>(__u)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614
615 template <class _Alloc, class ..._Up,
616 class = typename enable_if
617 <
618 sizeof...(_Up) <= sizeof...(_Tp) &&
619 __tuple_convertible
620 <
621 tuple<_Up...>,
622 typename __make_tuple_types<tuple,
623 sizeof...(_Up) < sizeof...(_Tp) ?
624 sizeof...(_Up) :
625 sizeof...(_Tp)>::type
626 >::value
627 >::type
628 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 tuple(allocator_arg_t, const _Alloc& __a, _Up&&... __u)
631 : base_(allocator_arg_t(), __a,
632 typename __make_tuple_indices<sizeof...(_Up)>::type(),
633 typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
634 typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
635 typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000636 _VSTD::forward<_Up>(__u)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637
638 template <class _Tuple,
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000639 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 <
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000641 __tuple_convertible<_Tuple, tuple>::value,
642 bool
643 >::type = false
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646 tuple(_Tuple&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000647 : base_(_VSTD::forward<_Tuple>(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000649 template <class _Tuple,
650 typename enable_if
651 <
652 __tuple_constructible<_Tuple, tuple>::value &&
653 !__tuple_convertible<_Tuple, tuple>::value,
654 bool
655 >::type = false
656 >
657 _LIBCPP_INLINE_VISIBILITY
658 explicit
659 tuple(_Tuple&& __t)
660 : base_(_VSTD::forward<_Tuple>(__t)) {}
661
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 template <class _Alloc, class _Tuple,
663 class = typename enable_if
664 <
665 __tuple_convertible<_Tuple, tuple>::value
666 >::type
667 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669 tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000670 : base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671
672 template <class _Tuple,
673 class = typename enable_if
674 <
675 __tuple_assignable<_Tuple, tuple>::value
676 >::type
677 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679 tuple&
680 operator=(_Tuple&& __t)
681 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000682 base_.operator=(_VSTD::forward<_Tuple>(__t));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683 return *this;
684 }
685
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta5e01212011-05-27 19:08:18 +0000687 void swap(tuple& __t) _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
688 {base_.swap(__t.base_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689};
690
691template <>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000692class _LIBCPP_VISIBLE tuple<>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693{
694public:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5394c1e2012-07-06 20:39:45 +0000696 _LIBCPP_CONSTEXPR tuple() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 template <class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 tuple(allocator_arg_t, const _Alloc&) {}
700 template <class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702 tuple(allocator_arg_t, const _Alloc&, const tuple&) {}
Howard Hinnant99968442011-11-29 18:15:50 +0000703 template <class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000705 tuple(array<_Up, 0>) {}
706 template <class _Alloc, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000708 tuple(allocator_arg_t, const _Alloc&, array<_Up, 0>) {}
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta5e01212011-05-27 19:08:18 +0000710 void swap(tuple&) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711};
712
713template <class ..._Tp>
714inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaabf2872011-06-01 19:59:32 +0000715typename enable_if
716<
717 __all<__is_swappable<_Tp>::value...>::value,
718 void
719>::type
Howard Hinnanta5e01212011-05-27 19:08:18 +0000720swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u)
721 _NOEXCEPT_(__all<__is_nothrow_swappable<_Tp>::value...>::value)
722 {__t.swap(__u);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
724// get
725
726template <size_t _Ip, class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000727inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf83417b2011-01-24 16:07:25 +0000728typename tuple_element<_Ip, tuple<_Tp...> >::type&
Howard Hinnantec3773c2011-12-01 20:21:04 +0000729get(tuple<_Tp...>& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730{
Howard Hinnantf83417b2011-01-24 16:07:25 +0000731 typedef typename tuple_element<_Ip, tuple<_Tp...> >::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732 return static_cast<__tuple_leaf<_Ip, type>&>(__t.base_).get();
733}
734
735template <size_t _Ip, class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000736inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf83417b2011-01-24 16:07:25 +0000737const typename tuple_element<_Ip, tuple<_Tp...> >::type&
Howard Hinnantec3773c2011-12-01 20:21:04 +0000738get(const tuple<_Tp...>& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739{
Howard Hinnantf83417b2011-01-24 16:07:25 +0000740 typedef typename tuple_element<_Ip, tuple<_Tp...> >::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741 return static_cast<const __tuple_leaf<_Ip, type>&>(__t.base_).get();
742}
743
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000744template <size_t _Ip, class ..._Tp>
745inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf83417b2011-01-24 16:07:25 +0000746typename tuple_element<_Ip, tuple<_Tp...> >::type&&
Howard Hinnantec3773c2011-12-01 20:21:04 +0000747get(tuple<_Tp...>&& __t) _NOEXCEPT
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000748{
Howard Hinnantf83417b2011-01-24 16:07:25 +0000749 typedef typename tuple_element<_Ip, tuple<_Tp...> >::type type;
Howard Hinnant56a85ca2011-01-25 16:31:30 +0000750 return static_cast<type&&>(
Douglas Gregor6c669942011-01-25 16:14:21 +0000751 static_cast<__tuple_leaf<_Ip, type>&&>(__t.base_).get());
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000752}
753
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754// tie
755
756template <class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000757inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758tuple<_Tp&...>
759tie(_Tp&... __t)
760{
761 return tuple<_Tp&...>(__t...);
762}
763
764template <class _Up>
765struct __ignore_t
766{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 template <class _Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000769 const __ignore_t& operator=(_Tp&&) const {return *this;}
770};
771
772namespace { const __ignore_t<unsigned char> ignore = __ignore_t<unsigned char>(); }
773
774template <class _Tp> class reference_wrapper;
775
776template <class _Tp>
777struct ___make_tuple_return
778{
779 typedef _Tp type;
780};
781
782template <class _Tp>
Howard Hinnantf83417b2011-01-24 16:07:25 +0000783struct ___make_tuple_return<reference_wrapper<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784{
785 typedef _Tp& type;
786};
787
788template <class _Tp>
789struct __make_tuple_return
790{
791 typedef typename ___make_tuple_return<typename decay<_Tp>::type>::type type;
792};
793
794template <class... _Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000795inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796tuple<typename __make_tuple_return<_Tp>::type...>
797make_tuple(_Tp&&... __t)
798{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000799 return tuple<typename __make_tuple_return<_Tp>::type...>(_VSTD::forward<_Tp>(__t)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800}
801
Howard Hinnant3c1ffba2010-08-19 18:59:38 +0000802template <class... _Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000803inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3c1ffba2010-08-19 18:59:38 +0000804tuple<_Tp&&...>
805forward_as_tuple(_Tp&&... __t)
806{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000807 return tuple<_Tp&&...>(_VSTD::forward<_Tp>(__t)...);
Howard Hinnant3c1ffba2010-08-19 18:59:38 +0000808}
809
Howard Hinnant99968442011-11-29 18:15:50 +0000810template <size_t _Ip>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811struct __tuple_equal
812{
813 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815 bool operator()(const _Tp& __x, const _Up& __y)
816 {
Howard Hinnant99968442011-11-29 18:15:50 +0000817 return __tuple_equal<_Ip - 1>()(__x, __y) && get<_Ip-1>(__x) == get<_Ip-1>(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818 }
819};
820
821template <>
822struct __tuple_equal<0>
823{
824 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826 bool operator()(const _Tp&, const _Up&)
827 {
828 return true;
829 }
830};
831
832template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000833inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834bool
835operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
836{
837 return __tuple_equal<sizeof...(_Tp)>()(__x, __y);
838}
839
840template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842bool
843operator!=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
844{
845 return !(__x == __y);
846}
847
Howard Hinnant99968442011-11-29 18:15:50 +0000848template <size_t _Ip>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000849struct __tuple_less
850{
851 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853 bool operator()(const _Tp& __x, const _Up& __y)
854 {
Howard Hinnant99968442011-11-29 18:15:50 +0000855 return __tuple_less<_Ip-1>()(__x, __y) ||
856 (!__tuple_less<_Ip-1>()(__y, __x) && get<_Ip-1>(__x) < get<_Ip-1>(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 }
858};
859
860template <>
861struct __tuple_less<0>
862{
863 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000865 bool operator()(const _Tp&, const _Up&)
866 {
867 return false;
868 }
869};
870
871template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000872inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873bool
874operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
875{
876 return __tuple_less<sizeof...(_Tp)>()(__x, __y);
877}
878
879template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000880inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881bool
882operator>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
883{
884 return __y < __x;
885}
886
887template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000888inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889bool
890operator>=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
891{
892 return !(__x < __y);
893}
894
895template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000896inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000897bool
898operator<=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
899{
900 return !(__y < __x);
901}
902
903// tuple_cat
904
Howard Hinnant0e1493e2010-12-11 20:47:50 +0000905template <class _Tp, class _Up> struct __tuple_cat_type;
906
907template <class ..._Ttypes, class ..._Utypes>
Howard Hinnantf83417b2011-01-24 16:07:25 +0000908struct __tuple_cat_type<tuple<_Ttypes...>, __tuple_types<_Utypes...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909{
Howard Hinnant0e1493e2010-12-11 20:47:50 +0000910 typedef tuple<_Ttypes..., _Utypes...> type;
911};
912
913template <class _ResultTuple, bool _Is_Tuple0TupleLike, class ..._Tuples>
914struct __tuple_cat_return_1
915{
916};
917
918template <class ..._Types, class _Tuple0>
919struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0>
920{
921 typedef typename __tuple_cat_type<tuple<_Types...>,
922 typename __make_tuple_types<typename remove_reference<_Tuple0>::type>::type>::type
923 type;
924};
925
926template <class ..._Types, class _Tuple0, class _Tuple1, class ..._Tuples>
927struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0, _Tuple1, _Tuples...>
928 : public __tuple_cat_return_1<
929 typename __tuple_cat_type<
930 tuple<_Types...>,
931 typename __make_tuple_types<typename remove_reference<_Tuple0>::type>::type
932 >::type,
933 __tuple_like<typename remove_reference<_Tuple1>::type>::value,
934 _Tuple1, _Tuples...>
935{
936};
937
938template <class ..._Tuples> struct __tuple_cat_return;
939
940template <class _Tuple0, class ..._Tuples>
941struct __tuple_cat_return<_Tuple0, _Tuples...>
942 : public __tuple_cat_return_1<tuple<>,
943 __tuple_like<typename remove_reference<_Tuple0>::type>::value, _Tuple0,
944 _Tuples...>
945{
946};
947
948template <>
949struct __tuple_cat_return<>
950{
951 typedef tuple<> type;
952};
953
954inline _LIBCPP_INLINE_VISIBILITY
955tuple<>
956tuple_cat()
957{
958 return tuple<>();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959}
960
Howard Hinnant99968442011-11-29 18:15:50 +0000961template <class _Rp, class _Indices, class _Tuple0, class ..._Tuples>
Howard Hinnante48e3662010-12-12 23:04:37 +0000962struct __tuple_cat_return_ref_imp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963
Howard Hinnante48e3662010-12-12 23:04:37 +0000964template <class ..._Types, size_t ..._I0, class _Tuple0>
965struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>, _Tuple0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966{
Howard Hinnant0e1493e2010-12-11 20:47:50 +0000967 typedef typename remove_reference<_Tuple0>::type _T0;
Howard Hinnante48e3662010-12-12 23:04:37 +0000968 typedef tuple<_Types..., typename __apply_cv<_Tuple0,
969 typename tuple_element<_I0, _T0>::type>::type&&...> type;
970};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971
Howard Hinnante48e3662010-12-12 23:04:37 +0000972template <class ..._Types, size_t ..._I0, class _Tuple0, class _Tuple1, class ..._Tuples>
973struct __tuple_cat_return_ref_imp<tuple<_Types...>, __tuple_indices<_I0...>,
974 _Tuple0, _Tuple1, _Tuples...>
975 : public __tuple_cat_return_ref_imp<
976 tuple<_Types..., typename __apply_cv<_Tuple0,
977 typename tuple_element<_I0,
978 typename remove_reference<_Tuple0>::type>::type>::type&&...>,
979 typename __make_tuple_indices<tuple_size<typename
980 remove_reference<_Tuple1>::type>::value>::type,
981 _Tuple1, _Tuples...>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982{
Howard Hinnante48e3662010-12-12 23:04:37 +0000983};
984
985template <class _Tuple0, class ..._Tuples>
986struct __tuple_cat_return_ref
987 : public __tuple_cat_return_ref_imp<tuple<>,
988 typename __make_tuple_indices<
989 tuple_size<typename remove_reference<_Tuple0>::type>::value
990 >::type, _Tuple0, _Tuples...>
991{
992};
993
994template <class _Types, class _I0, class _J0>
995struct __tuple_cat;
996
997template <class ..._Types, size_t ..._I0, size_t ..._J0>
Howard Hinnantf83417b2011-01-24 16:07:25 +0000998struct __tuple_cat<tuple<_Types...>, __tuple_indices<_I0...>, __tuple_indices<_J0...> >
Howard Hinnante48e3662010-12-12 23:04:37 +0000999{
1000 template <class _Tuple0>
1001 _LIBCPP_INLINE_VISIBILITY
1002 typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&>::type
1003 operator()(tuple<_Types...> __t, _Tuple0&& __t0)
1004 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001005 return _VSTD::forward_as_tuple(_VSTD::forward<_Types>(get<_I0>(__t))...,
1006 get<_J0>(_VSTD::forward<_Tuple0>(__t0))...);
Howard Hinnante48e3662010-12-12 23:04:37 +00001007 }
1008
1009 template <class _Tuple0, class _Tuple1, class ..._Tuples>
1010 _LIBCPP_INLINE_VISIBILITY
1011 typename __tuple_cat_return_ref<tuple<_Types...>&&, _Tuple0&&, _Tuple1&&, _Tuples&&...>::type
1012 operator()(tuple<_Types...> __t, _Tuple0&& __t0, _Tuple1&& __t1, _Tuples&& ...__tpls)
1013 {
1014 typedef typename remove_reference<_Tuple0>::type _T0;
1015 typedef typename remove_reference<_Tuple1>::type _T1;
1016 return __tuple_cat<
1017 tuple<_Types..., typename __apply_cv<_Tuple0, typename tuple_element<_J0, _T0>::type>::type&&...>,
1018 typename __make_tuple_indices<sizeof ...(_Types) + tuple_size<_T0>::value>::type,
1019 typename __make_tuple_indices<tuple_size<_T1>::value>::type>()
Howard Hinnant0949eed2011-06-30 21:18:19 +00001020 (_VSTD::forward_as_tuple(
1021 _VSTD::forward<_Types>(get<_I0>(__t))...,
1022 get<_J0>(_VSTD::forward<_Tuple0>(__t0))...
Howard Hinnante48e3662010-12-12 23:04:37 +00001023 ),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001024 _VSTD::forward<_Tuple1>(__t1),
1025 _VSTD::forward<_Tuples>(__tpls)...);
Howard Hinnante48e3662010-12-12 23:04:37 +00001026 }
1027};
1028
1029template <class _Tuple0, class... _Tuples>
1030inline _LIBCPP_INLINE_VISIBILITY
1031typename __tuple_cat_return<_Tuple0, _Tuples...>::type
1032tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls)
1033{
1034 typedef typename remove_reference<_Tuple0>::type _T0;
1035 return __tuple_cat<tuple<>, __tuple_indices<>,
1036 typename __make_tuple_indices<tuple_size<_T0>::value>::type>()
Howard Hinnant0949eed2011-06-30 21:18:19 +00001037 (tuple<>(), _VSTD::forward<_Tuple0>(__t0),
1038 _VSTD::forward<_Tuples>(__tpls)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039}
1040
1041template <class ..._Tp, class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +00001042struct _LIBCPP_VISIBLE uses_allocator<tuple<_Tp...>, _Alloc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043 : true_type {};
1044
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045template <class _T1, class _T2>
1046template <class... _Args1, class... _Args2, size_t ..._I1, size_t ..._I2>
1047inline _LIBCPP_INLINE_VISIBILITY
1048pair<_T1, _T2>::pair(piecewise_construct_t,
1049 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
1050 __tuple_indices<_I1...>, __tuple_indices<_I2...>)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001051 : first(_VSTD::forward<_Args1>(get<_I1>( __first_args))...),
1052 second(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053{
1054}
1055
Howard Hinnant324bb032010-08-22 00:02:43 +00001056#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057
1058_LIBCPP_END_NAMESPACE_STD
1059
1060#endif // _LIBCPP_TUPLE