blob: 2c294953f4cfdbebd0c5196dcf2b12ea8aafd683 [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;
28 tuple(tuple&&);
29 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&);
59 tuple& operator=(tuple&&);
60 template <class... U>
61 tuple& operator=(const tuple<U...>&);
62 template <class... U>
63 tuple& operator=(tuple<U...>&&);
64 template <class U1, class U2>
65 tuple& operator=(const pair<U1, U2>&); // iff sizeof...(T) == 2
66 template <class U1, class U2>
67 tuple& operator=(pair<U1, U2>&&); //iffsizeof...(T) == 2
68
69 void swap(tuple&);
70};
71
72const unspecified ignore;
73
74template <class... T> tuple<V...> make_tuple(T&&...);
Howard Hinnant3c1ffba2010-08-19 18:59:38 +000075template <class... T> tuple<ATypes...> forward_as_tuple(T&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076template <class... T> tuple<T&...> tie(T&...);
77template <class... T, class... U> tuple<T..., U...> tuple_cat(const tuple<T...>&, const tuple<U...>&);
78template <class... T, class... U> tuple<T..., U...> tuple_cat(tuple<T...>&&, const tuple<U...>&);
79template <class... T, class... U> tuple<T..., U...> tuple_cat(const tuple<T...>&, tuple<U...>&&);
80template <class... T, class... U> tuple<T..., U...> tuple_cat(tuple<T...>&&, tuple<U...>&&);
81
82// 20.4.1.4, tuple helper classes:
83template <class T> class tuple_size; // undefined
84template <class... T> class tuple_size<tuple<T...>>;
85template <intsize_t I, class T> class tuple_element; // undefined
86template <intsize_t I, class... T> class tuple_element<I, tuple<T...>>;
87
88// 20.4.1.5, element access:
89template <intsize_t I, class... T> typename tuple_element<I, tuple<T...>>::type& get(tuple<T...>&);
90template <intsize_t I, class... T> typename tuple_element<I, tuple<T...>>::type const& get(const tuple<T...>&);
91
92// 20.4.1.6, relational operators:
93template<class... T, class... U> bool operator==(const tuple<T...>&, const tuple<U...>&);
94template<class... T, class... U> bool operator<(const tuple<T...>&, const tuple<U...>&);
95template<class... T, class... U> bool operator!=(const tuple<T...>&, const tuple<U...>&);
96template<class... T, class... U> bool operator>(const tuple<T...>&, const tuple<U...>&);
97template<class... T, class... U> bool operator<=(const tuple<T...>&, const tuple<U...>&);
98template<class... T, class... U> bool operator>=(const tuple<T...>&, const tuple<U...>&);
99
100template <class... Types, class Alloc>
101 struct uses_allocator<tuple<Types...>, Alloc>;
102
103template <class... Types>
104 void swap(tuple<Types...>& x, tuple<Types...>& y);
105
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106} // std
107
108*/
109
110#include <__config>
111#include <__tuple>
112#include <cstddef>
113#include <memory>
114#include <type_traits>
115
116#pragma GCC system_header
117
118_LIBCPP_BEGIN_NAMESPACE_STD
119
120#ifndef _LIBCPP_HAS_NO_VARIADICS
121
122// tuple_size
123
124template <class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000125class _LIBCPP_VISIBLE tuple_size<tuple<_Tp...>>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126 : public integral_constant<size_t, sizeof...(_Tp)>
127{
128};
129
130template <class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000131class _LIBCPP_VISIBLE tuple_size<const tuple<_Tp...>>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000132 : public integral_constant<size_t, sizeof...(_Tp)>
133{
134};
135
136// tuple_element
137
138template <size_t _Ip, class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000139class _LIBCPP_VISIBLE tuple_element<_Ip, tuple<_Tp...>>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000140{
141public:
142 typedef typename tuple_element<_Ip, __tuple_types<_Tp...>>::type type;
143};
144
145template <size_t _Ip, class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000146class _LIBCPP_VISIBLE tuple_element<_Ip, const tuple<_Tp...>>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147{
148public:
149 typedef const typename tuple_element<_Ip, __tuple_types<_Tp...>>::type type;
150};
151
152// __tuple_leaf
153
154template <size_t _Ip, class _Hp, bool=is_empty<_Hp>::value>
155class __tuple_leaf;
156
157template <size_t _Ip, class _Hp, bool _Ep>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000158inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159void swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y)
160{
161 swap(__x.get(), __y.get());
162}
163
164template <size_t _Ip, class _Hp, bool>
165class __tuple_leaf
166{
167 _Hp value;
168
169 __tuple_leaf& operator=(const __tuple_leaf&);
170public:
171 _LIBCPP_INLINE_VISIBILITY __tuple_leaf() : value()
172 {static_assert(!is_reference<_Hp>::value,
173 "Attempted to default construct a reference element in a tuple");}
174
175 template <class _Alloc>
176 _LIBCPP_INLINE_VISIBILITY
177 __tuple_leaf(integral_constant<int, 0>, const _Alloc&)
178 : value()
179 {static_assert(!is_reference<_Hp>::value,
180 "Attempted to default construct a reference element in a tuple");}
181
182 template <class _Alloc>
183 _LIBCPP_INLINE_VISIBILITY
184 __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
185 : value(allocator_arg_t(), __a)
186 {static_assert(!is_reference<_Hp>::value,
187 "Attempted to default construct a reference element in a tuple");}
188
189 template <class _Alloc>
190 _LIBCPP_INLINE_VISIBILITY
191 __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
192 : value(__a)
193 {static_assert(!is_reference<_Hp>::value,
194 "Attempted to default construct a reference element in a tuple");}
195
Howard Hinnante049cc52010-09-27 17:54:17 +0000196 template <class _Tp,
197 class = typename enable_if<is_constructible<_Hp, _Tp>::value>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198 _LIBCPP_INLINE_VISIBILITY
199 explicit __tuple_leaf(_Tp&& __t)
200 : value(_STD::forward<_Tp>(__t))
Howard Hinnante049cc52010-09-27 17:54:17 +0000201 {static_assert(!is_reference<_Hp>::value ||
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 is_lvalue_reference<_Hp>::value &&
203 (is_lvalue_reference<_Tp>::value ||
204 is_same<typename remove_reference<_Tp>::type,
205 reference_wrapper<
206 typename remove_reference<_Hp>::type
207 >
Howard Hinnante049cc52010-09-27 17:54:17 +0000208 >::value) ||
209 (is_rvalue_reference<_Hp>::value &&
210 !is_lvalue_reference<_Tp>::value),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211 "Attempted to construct a reference element in a tuple with an rvalue");}
212
213 template <class _Tp, class _Alloc>
214 _LIBCPP_INLINE_VISIBILITY
215 explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
216 : value(_STD::forward<_Tp>(__t))
217 {static_assert(!is_lvalue_reference<_Hp>::value ||
218 is_lvalue_reference<_Hp>::value &&
219 (is_lvalue_reference<_Tp>::value ||
220 is_same<typename remove_reference<_Tp>::type,
221 reference_wrapper<
222 typename remove_reference<_Hp>::type
223 >
224 >::value),
225 "Attempted to construct a reference element in a tuple with an rvalue");}
226
227 template <class _Tp, class _Alloc>
228 _LIBCPP_INLINE_VISIBILITY
229 explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
230 : value(allocator_arg_t(), __a, _STD::forward<_Tp>(__t))
231 {static_assert(!is_lvalue_reference<_Hp>::value ||
232 is_lvalue_reference<_Hp>::value &&
233 (is_lvalue_reference<_Tp>::value ||
234 is_same<typename remove_reference<_Tp>::type,
235 reference_wrapper<
236 typename remove_reference<_Hp>::type
237 >
238 >::value),
239 "Attempted to construct a reference element in a tuple with an rvalue");}
240
241 template <class _Tp, class _Alloc>
242 _LIBCPP_INLINE_VISIBILITY
243 explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
244 : value(_STD::forward<_Tp>(__t), __a)
245 {static_assert(!is_lvalue_reference<_Hp>::value ||
246 is_lvalue_reference<_Hp>::value &&
247 (is_lvalue_reference<_Tp>::value ||
248 is_same<typename remove_reference<_Tp>::type,
249 reference_wrapper<
250 typename remove_reference<_Hp>::type
251 >
252 >::value),
253 "Attempted to construct a reference element in a tuple with an rvalue");}
254
Howard Hinnante049cc52010-09-27 17:54:17 +0000255 __tuple_leaf(const __tuple_leaf& __t)
256 : value(__t.get())
257 {static_assert(!is_rvalue_reference<_Hp>::value, "Can not copy a tuple with rvalue reference member");}
258
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259 template <class _Tp>
260 _LIBCPP_INLINE_VISIBILITY
261 explicit __tuple_leaf(const __tuple_leaf<_Ip, _Tp>& __t)
262 : value(__t.get()) {}
263
264 template <class _Tp>
265 _LIBCPP_INLINE_VISIBILITY
266 __tuple_leaf&
267 operator=(_Tp&& __t)
268 {
269 value = _STD::forward<_Tp>(__t);
270 return *this;
271 }
272
273 _LIBCPP_INLINE_VISIBILITY
274 int swap(__tuple_leaf& __t)
275 {
276 _STD::swap(*this, __t);
277 return 0;
278 }
279
280 _LIBCPP_INLINE_VISIBILITY _Hp& get() {return value;}
281 _LIBCPP_INLINE_VISIBILITY const _Hp& get() const {return value;}
282};
283
284template <size_t _Ip, class _Hp>
285class __tuple_leaf<_Ip, _Hp, true>
286 : private _Hp
287{
288
289 __tuple_leaf& operator=(const __tuple_leaf&);
290public:
291 _LIBCPP_INLINE_VISIBILITY __tuple_leaf() {}
292
293 template <class _Alloc>
294 _LIBCPP_INLINE_VISIBILITY
295 __tuple_leaf(integral_constant<int, 0>, const _Alloc&) {}
296
297 template <class _Alloc>
298 _LIBCPP_INLINE_VISIBILITY
299 __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a)
300 : _Hp(allocator_arg_t(), __a) {}
301
302 template <class _Alloc>
303 _LIBCPP_INLINE_VISIBILITY
304 __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a)
305 : _Hp(__a) {}
306
Howard Hinnante049cc52010-09-27 17:54:17 +0000307 template <class _Tp,
308 class = typename enable_if<is_constructible<_Hp, _Tp>::value>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309 _LIBCPP_INLINE_VISIBILITY
310 explicit __tuple_leaf(_Tp&& __t)
311 : _Hp(_STD::forward<_Tp>(__t)) {}
312
313 template <class _Tp, class _Alloc>
314 _LIBCPP_INLINE_VISIBILITY
315 explicit __tuple_leaf(integral_constant<int, 0>, const _Alloc&, _Tp&& __t)
316 : _Hp(_STD::forward<_Tp>(__t)) {}
317
318 template <class _Tp, class _Alloc>
319 _LIBCPP_INLINE_VISIBILITY
320 explicit __tuple_leaf(integral_constant<int, 1>, const _Alloc& __a, _Tp&& __t)
321 : _Hp(allocator_arg_t(), __a, _STD::forward<_Tp>(__t)) {}
322
323 template <class _Tp, class _Alloc>
324 _LIBCPP_INLINE_VISIBILITY
325 explicit __tuple_leaf(integral_constant<int, 2>, const _Alloc& __a, _Tp&& __t)
326 : _Hp(_STD::forward<_Tp>(__t), __a) {}
327
328 template <class _Tp>
329 _LIBCPP_INLINE_VISIBILITY
330 explicit __tuple_leaf(const __tuple_leaf<_Ip, _Tp>& __t)
331 : _Hp(__t.get()) {}
332
333 template <class _Tp>
334 _LIBCPP_INLINE_VISIBILITY
335 __tuple_leaf&
336 operator=(_Tp&& __t)
337 {
338 _Hp::operator=(_STD::forward<_Tp>(__t));
339 return *this;
340 }
341
342 _LIBCPP_INLINE_VISIBILITY int swap(__tuple_leaf& __t)
343 {
344 _STD::swap(*this, __t);
345 return 0;
346 }
347
348 _LIBCPP_INLINE_VISIBILITY _Hp& get() {return static_cast<_Hp&>(*this);}
349 _LIBCPP_INLINE_VISIBILITY const _Hp& get() const {return static_cast<const _Hp&>(*this);}
350};
351
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000352template <class ..._Tp>
353_LIBCPP_INLINE_VISIBILITY
354void __swallow(_Tp&&...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355
356// __tuple_impl
357
358template<class _Indx, class ..._Tp> struct __tuple_impl;
359
360template<size_t ..._Indx, class ..._Tp>
361struct __tuple_impl<__tuple_indices<_Indx...>, _Tp...>
362 : public __tuple_leaf<_Indx, _Tp>...
363{
364 template <size_t ..._Uf, class ..._Tf,
365 size_t ..._Ul, class ..._Tl, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367 explicit
368 __tuple_impl(__tuple_indices<_Uf...>, __tuple_types<_Tf...>,
369 __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
370 _Up&&... __u) :
371 __tuple_leaf<_Uf, _Tf>(_STD::forward<_Up>(__u))...,
372 __tuple_leaf<_Ul, _Tl>()...
373 {}
374
375 template <class _Alloc, size_t ..._Uf, class ..._Tf,
376 size_t ..._Ul, class ..._Tl, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378 explicit
379 __tuple_impl(allocator_arg_t, const _Alloc& __a,
380 __tuple_indices<_Uf...>, __tuple_types<_Tf...>,
381 __tuple_indices<_Ul...>, __tuple_types<_Tl...>,
382 _Up&&... __u) :
383 __tuple_leaf<_Uf, _Tf>(__uses_alloc_ctor<_Tf, _Alloc, _Up>(), __a,
384 _STD::forward<_Up>(__u))...,
385 __tuple_leaf<_Ul, _Tl>(__uses_alloc_ctor<_Tl, _Alloc>(), __a)...
386 {}
387
388 template <class _Tuple,
389 class = typename enable_if
390 <
391 __tuple_convertible<_Tuple, tuple<_Tp...>>::value
392 >::type
393 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395 __tuple_impl(_Tuple&& __t)
396 : __tuple_leaf<_Indx, _Tp>(_STD::forward<typename tuple_element<_Indx,
397 typename __make_tuple_types<_Tuple>::type>::type>(_STD::get<_Indx>(__t)))...
398 {}
399
400 template <class _Alloc, class _Tuple,
401 class = typename enable_if
402 <
403 __tuple_convertible<_Tuple, tuple<_Tp...>>::value
404 >::type
405 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 __tuple_impl(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
408 : __tuple_leaf<_Indx, _Tp>(__uses_alloc_ctor<_Tp, _Alloc, typename tuple_element<_Indx,
Howard Hinnant324bb032010-08-22 00:02:43 +0000409 typename __make_tuple_types<_Tuple>::type>::type>(), __a,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 _STD::forward<typename tuple_element<_Indx,
411 typename __make_tuple_types<_Tuple>::type>::type>(_STD::get<_Indx>(__t)))...
412 {}
413
414 template <class _Tuple>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416 typename enable_if
417 <
418 __tuple_assignable<_Tuple, tuple<_Tp...>>::value,
419 __tuple_impl&
420 >::type
421 operator=(_Tuple&& __t)
422 {
423 __swallow(__tuple_leaf<_Indx, _Tp>::operator=(_STD::forward<typename tuple_element<_Indx,
424 typename __make_tuple_types<_Tuple>::type>::type>(_STD::get<_Indx>(__t)))...);
425 return *this;
426 }
427
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 void swap(__tuple_impl& __t)
430 {
431 __swallow(__tuple_leaf<_Indx, _Tp>::swap(static_cast<__tuple_leaf<_Indx, _Tp>&>(__t))...);
432 }
433};
434
435template <class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000436class _LIBCPP_VISIBLE tuple
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437{
438 typedef __tuple_impl<typename __make_tuple_indices<sizeof...(_Tp)>::type, _Tp...> base;
439
440 base base_;
441
442 template <size_t _Jp, class ..._Up> friend
443 typename tuple_element<_Jp, tuple<_Up...>>::type& get(tuple<_Up...>&);
444 template <size_t _Jp, class ..._Up> friend
445 const typename tuple_element<_Jp, tuple<_Up...>>::type& get(const tuple<_Up...>&);
446public:
447
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 explicit tuple(const _Tp& ... __t)
450 : base_(typename __make_tuple_indices<sizeof...(_Tp)>::type(),
451 typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
452 typename __make_tuple_indices<0>::type(),
453 typename __make_tuple_types<tuple, 0>::type(),
454 __t...
455 ) {}
456
457 template <class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 tuple(allocator_arg_t, const _Alloc& __a, const _Tp& ... __t)
460 : base_(allocator_arg_t(), __a,
461 typename __make_tuple_indices<sizeof...(_Tp)>::type(),
462 typename __make_tuple_types<tuple, sizeof...(_Tp)>::type(),
463 typename __make_tuple_indices<0>::type(),
464 typename __make_tuple_types<tuple, 0>::type(),
465 __t...
466 ) {}
467
468 template <class ..._Up,
469 class = typename enable_if
470 <
471 sizeof...(_Up) <= sizeof...(_Tp) &&
472 __tuple_convertible
473 <
474 tuple<_Up...>,
475 typename __make_tuple_types<tuple,
476 sizeof...(_Up) < sizeof...(_Tp) ?
477 sizeof...(_Up) :
478 sizeof...(_Tp)>::type
479 >::value
480 >::type
481 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483 explicit
484 tuple(_Up&&... __u)
485 : base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
486 typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
487 typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
488 typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
489 _STD::forward<_Up>(__u)...) {}
490
491 template <class _Alloc, class ..._Up,
492 class = typename enable_if
493 <
494 sizeof...(_Up) <= sizeof...(_Tp) &&
495 __tuple_convertible
496 <
497 tuple<_Up...>,
498 typename __make_tuple_types<tuple,
499 sizeof...(_Up) < sizeof...(_Tp) ?
500 sizeof...(_Up) :
501 sizeof...(_Tp)>::type
502 >::value
503 >::type
504 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506 tuple(allocator_arg_t, const _Alloc& __a, _Up&&... __u)
507 : base_(allocator_arg_t(), __a,
508 typename __make_tuple_indices<sizeof...(_Up)>::type(),
509 typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
510 typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
511 typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
512 _STD::forward<_Up>(__u)...) {}
513
514 template <class _Tuple,
515 class = typename enable_if
516 <
517 __tuple_convertible<_Tuple, tuple>::value
518 >::type
519 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521 tuple(_Tuple&& __t)
522 : base_(_STD::forward<_Tuple>(__t)) {}
523
524 template <class _Alloc, class _Tuple,
525 class = typename enable_if
526 <
527 __tuple_convertible<_Tuple, tuple>::value
528 >::type
529 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000531 tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
532 : base_(allocator_arg_t(), __a, _STD::forward<_Tuple>(__t)) {}
533
534 template <class _Tuple,
535 class = typename enable_if
536 <
537 __tuple_assignable<_Tuple, tuple>::value
538 >::type
539 >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 tuple&
542 operator=(_Tuple&& __t)
543 {
544 base_.operator=(_STD::forward<_Tuple>(__t));
545 return *this;
546 }
547
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 void swap(tuple& __t) {base_.swap(__t.base_);}
550};
551
552template <>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000553class _LIBCPP_VISIBLE tuple<>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554{
555public:
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557 tuple() {}
558 template <class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560 tuple(allocator_arg_t, const _Alloc&) {}
561 template <class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563 tuple(allocator_arg_t, const _Alloc&, const tuple&) {}
564 template <class _U>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566 tuple(array<_U, 0>) {}
567 template <class _Alloc, class _U>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569 tuple(allocator_arg_t, const _Alloc&, array<_U, 0>) {}
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571 void swap(tuple&) {}
572};
573
574template <class ..._Tp>
575inline _LIBCPP_INLINE_VISIBILITY
576void
577swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u) {__t.swap(__u);}
578
579// get
580
581template <size_t _Ip, class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000582inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583typename tuple_element<_Ip, tuple<_Tp...>>::type&
584get(tuple<_Tp...>& __t)
585{
586 typedef typename tuple_element<_Ip, tuple<_Tp...>>::type type;
587 return static_cast<__tuple_leaf<_Ip, type>&>(__t.base_).get();
588}
589
590template <size_t _Ip, class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592const typename tuple_element<_Ip, tuple<_Tp...>>::type&
593get(const tuple<_Tp...>& __t)
594{
595 typedef typename tuple_element<_Ip, tuple<_Tp...>>::type type;
596 return static_cast<const __tuple_leaf<_Ip, type>&>(__t.base_).get();
597}
598
599// tie
600
601template <class ..._Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000602inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603tuple<_Tp&...>
604tie(_Tp&... __t)
605{
606 return tuple<_Tp&...>(__t...);
607}
608
609template <class _Up>
610struct __ignore_t
611{
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 __ignore_t() {}
614 template <class _Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616 __ignore_t(_Tp&&) {}
617 template <class _Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619 const __ignore_t& operator=(_Tp&&) const {return *this;}
620};
621
622namespace { const __ignore_t<unsigned char> ignore = __ignore_t<unsigned char>(); }
623
624template <class _Tp> class reference_wrapper;
625
626template <class _Tp>
627struct ___make_tuple_return
628{
629 typedef _Tp type;
630};
631
632template <class _Tp>
633struct ___make_tuple_return<reference_wrapper<_Tp>>
634{
635 typedef _Tp& type;
636};
637
638template <class _Tp>
639struct __make_tuple_return
640{
641 typedef typename ___make_tuple_return<typename decay<_Tp>::type>::type type;
642};
643
644template <class... _Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000645inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646tuple<typename __make_tuple_return<_Tp>::type...>
647make_tuple(_Tp&&... __t)
648{
649 return tuple<typename __make_tuple_return<_Tp>::type...>(_STD::forward<_Tp>(__t)...);
650}
651
Howard Hinnant3c1ffba2010-08-19 18:59:38 +0000652template <class... _Tp>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000653inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3c1ffba2010-08-19 18:59:38 +0000654tuple<_Tp&&...>
655forward_as_tuple(_Tp&&... __t)
656{
657 return tuple<_Tp&&...>(_STD::forward<_Tp>(__t)...);
658}
659
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660template <size_t _I>
661struct __tuple_equal
662{
663 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000665 bool operator()(const _Tp& __x, const _Up& __y)
666 {
667 return __tuple_equal<_I - 1>()(__x, __y) && get<_I-1>(__x) == get<_I-1>(__y);
668 }
669};
670
671template <>
672struct __tuple_equal<0>
673{
674 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676 bool operator()(const _Tp&, const _Up&)
677 {
678 return true;
679 }
680};
681
682template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000683inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684bool
685operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
686{
687 return __tuple_equal<sizeof...(_Tp)>()(__x, __y);
688}
689
690template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000691inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692bool
693operator!=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
694{
695 return !(__x == __y);
696}
697
698template <size_t _I>
699struct __tuple_less
700{
701 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703 bool operator()(const _Tp& __x, const _Up& __y)
704 {
705 return __tuple_less<_I-1>()(__x, __y) ||
706 (!__tuple_less<_I-1>()(__y, __x) && get<_I-1>(__x) < get<_I-1>(__y));
707 }
708};
709
710template <>
711struct __tuple_less<0>
712{
713 template <class _Tp, class _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 bool operator()(const _Tp&, const _Up&)
716 {
717 return false;
718 }
719};
720
721template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000722inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723bool
724operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
725{
726 return __tuple_less<sizeof...(_Tp)>()(__x, __y);
727}
728
729template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000730inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731bool
732operator>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
733{
734 return __y < __x;
735}
736
737template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000738inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739bool
740operator>=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
741{
742 return !(__x < __y);
743}
744
745template <class ..._Tp, class ..._Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000746inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747bool
748operator<=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
749{
750 return !(__y < __x);
751}
752
753// tuple_cat
754
755template <class... _Tp, size_t ..._I1, class... _Up, size_t ..._I2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000756inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757tuple<_Tp..., _Up...>
758__tuple_cat(const tuple<_Tp...>& __x, __tuple_indices<_I1...>, const tuple<_Up...>& __y, __tuple_indices<_I2...>)
759{
760 return tuple<_Tp..., _Up...>(get<_I1>(__x)..., get<_I2>(__y)...);
761}
762
763template <class... _Tp, class... _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000764inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765tuple<_Tp..., _Up...>
766tuple_cat(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
767{
768 return __tuple_cat(__x, typename __make_tuple_indices<sizeof...(_Tp)>::type(),
769 __y, typename __make_tuple_indices<sizeof...(_Up)>::type());
770}
771
772template <class... _Tp, size_t ..._I1, class... _Up, size_t ..._I2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000773inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774tuple<_Tp..., _Up...>
775__tuple_cat(tuple<_Tp...>&& __x, __tuple_indices<_I1...>, const tuple<_Up...>& __y, __tuple_indices<_I2...>)
776{
777 return tuple<_Tp..., _Up...>(_STD::forward<_Tp>(get<_I1>(__x))..., get<_I2>(__y)...);
778}
779
780template <class... _Tp, class... _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000781inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000782tuple<_Tp..., _Up...>
783tuple_cat(tuple<_Tp...>&& __x, const tuple<_Up...>& __y)
784{
785 return __tuple_cat(_STD::move(__x), typename __make_tuple_indices<sizeof...(_Tp)>::type(),
786 __y, typename __make_tuple_indices<sizeof...(_Up)>::type());
787}
788
789template <class... _Tp, size_t ..._I1, class... _Up, size_t ..._I2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000790inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791tuple<_Tp..., _Up...>
792__tuple_cat(const tuple<_Tp...>& __x, __tuple_indices<_I1...>, tuple<_Up...>&& __y, __tuple_indices<_I2...>)
793{
794 return tuple<_Tp..., _Up...>(get<_I1>(__x)..., _STD::forward<_Up>(get<_I2>(__y))...);
795}
796
797template <class... _Tp, class... _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000798inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000799tuple<_Tp..., _Up...>
800tuple_cat(const tuple<_Tp...>& __x, tuple<_Up...>&& __y)
801{
802 return __tuple_cat(__x, typename __make_tuple_indices<sizeof...(_Tp)>::type(),
803 _STD::move(__y), typename __make_tuple_indices<sizeof...(_Up)>::type());
804}
805
806template <class... _Tp, size_t ..._I1, class... _Up, size_t ..._I2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000807inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808tuple<_Tp..., _Up...>
809__tuple_cat(tuple<_Tp...>&& __x, __tuple_indices<_I1...>, tuple<_Up...>&& __y, __tuple_indices<_I2...>)
810{
811 return tuple<_Tp..., _Up...>(_STD::forward<_Tp>(get<_I1>(__x))..., _STD::forward<_Up>(get<_I2>(__y))...);
812}
813
814template <class... _Tp, class... _Up>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000815inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816tuple<_Tp..., _Up...>
817tuple_cat(tuple<_Tp...>&& __x, tuple<_Up...>&& __y)
818{
819 return __tuple_cat(_STD::move(__x), typename __make_tuple_indices<sizeof...(_Tp)>::type(),
820 _STD::move(__y), typename __make_tuple_indices<sizeof...(_Up)>::type());
821}
822
823template <class ..._Tp, class _Alloc>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000824struct _LIBCPP_VISIBLE uses_allocator<tuple<_Tp...>, _Alloc>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825 : true_type {};
826
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000827template <class _T1, class _T2>
828template <class... _Args1, class... _Args2, size_t ..._I1, size_t ..._I2>
829inline _LIBCPP_INLINE_VISIBILITY
830pair<_T1, _T2>::pair(piecewise_construct_t,
831 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
832 __tuple_indices<_I1...>, __tuple_indices<_I2...>)
833 : first(_STD::forward<_Args1>(get<_I1>( __first_args))...),
834 second(_STD::forward<_Args2>(get<_I2>(__second_args))...)
835{
836}
837
Howard Hinnant324bb032010-08-22 00:02:43 +0000838#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839
840_LIBCPP_END_NAMESPACE_STD
841
842#endif // _LIBCPP_TUPLE