3 // Copyright (C) 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file include/tuple
31 * This is a Standard C++ Library header.
34 #ifndef _GLIBCXX_CXX0X_TUPLE
35 #define _GLIBCXX_CXX0X_TUPLE 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
47 // Adds a const reference to a non-reference type.
48 template<typename _Tp>
50 { typedef const _Tp& type; };
52 template<typename _Tp>
53 struct __add_c_ref<_Tp&>
54 { typedef _Tp& type; };
56 // Adds a reference to a non-reference type.
57 template<typename _Tp>
59 { typedef _Tp& type; };
61 template<typename _Tp>
62 struct __add_ref<_Tp&>
63 { typedef _Tp& type; };
65 template<int _Idx, typename _Head, bool _IsEmpty>
68 template<int _Idx, typename _Head>
69 struct _Head_base<_Idx, _Head, true>
75 _Head_base(typename __add_c_ref<_Head>::type __h)
78 _Head_base(typename std::remove_reference<_Head>::type&& __h)
79 : _Head(std::forward<_Head>(__h)) { }
81 _Head& _M_head() { return *this; }
82 const _Head& _M_head() const { return *this; }
85 template<int _Idx, typename _Head>
86 struct _Head_base<_Idx, _Head, false>
91 _Head_base(typename __add_c_ref<_Head>::type __h)
92 : _M_head_impl(__h) { }
94 _Head_base(typename std::remove_reference<_Head>::type&& __h)
95 : _M_head_impl(std::move(__h)) { }
97 _Head& _M_head() { return _M_head_impl; }
98 const _Head& _M_head() const { return _M_head_impl; }
105 * Contains the actual implementation of the @c tuple template, stored
106 * as a recursive inheritance hierarchy from the first element (most
107 * derived class) to the last (least derived class). The @c Idx
108 * parameter gives the 0-based index of the element stored at this
109 * point in the hierarchy; we use it to implement a constant-time
113 template<int _Idx, typename... _Elements>
118 * Zero-element tuple implementation. This is the basis case for the
119 * inheritance recursion.
123 struct _Tuple_impl<_Idx> { };
127 * Recursive tuple implementation. Here we store the @c Head element
128 * and derive from a @c Tuple_impl containing the remaining elements
129 * (which contains the @c Tail).
132 template<int _Idx, typename _Head, typename... _Tail>
133 struct _Tuple_impl<_Idx, _Head, _Tail...>
134 : public _Tuple_impl<_Idx + 1, _Tail...>,
135 private _Head_base<_Idx, _Head, std::is_empty<_Head>::value>
137 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
138 typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
140 _Head& _M_head() { return _Base::_M_head(); }
141 const _Head& _M_head() const { return _Base::_M_head(); }
143 _Inherited& _M_tail() { return *this; }
144 const _Inherited& _M_tail() const { return *this; }
147 : _Inherited(), _Base() { }
150 _Tuple_impl(typename __add_c_ref<_Head>::type __head,
151 typename __add_c_ref<_Tail>::type... __tail)
152 : _Inherited(__tail...), _Base(__head) { }
154 template<typename... _UElements>
155 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
156 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
158 _Tuple_impl(const _Tuple_impl& __in)
159 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
161 template<typename... _UElements>
163 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
165 _M_head() = __in._M_head();
166 _M_tail() = __in._M_tail();
171 operator=(const _Tuple_impl& __in)
173 _M_head() = __in._M_head();
174 _M_tail() = __in._M_tail();
179 template<typename... _Elements>
180 class tuple : public _Tuple_impl<0, _Elements...>
182 typedef _Tuple_impl<0, _Elements...> _Inherited;
189 tuple(typename __add_c_ref<_Elements>::type... __elements)
190 : _Inherited(__elements...) { }
192 template<typename... _UElements>
193 tuple(const tuple<_UElements...>& __in)
194 : _Inherited(__in) { }
196 tuple(const tuple& __in)
197 : _Inherited(__in) { }
199 template<typename... _UElements>
201 operator=(const tuple<_UElements...>& __in)
203 static_cast<_Inherited&>(*this) = __in;
208 operator=(const tuple& __in)
210 static_cast<_Inherited&>(*this) = __in;
215 template<> class tuple<> { };
217 // 2-element tuple, with construction and assignment from a pair.
218 template<typename _T1, typename _T2>
219 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
221 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
228 tuple(typename __add_c_ref<_T1>::type __a1,
229 typename __add_c_ref<_T2>::type __a2)
230 : _Inherited(__a1, __a2) { }
232 template<typename _U1, typename _U2>
233 tuple(const tuple<_U1, _U2>& __in)
234 : _Inherited(__in) { }
236 tuple(const tuple& __in)
237 : _Inherited(__in) { }
239 template<typename _U1, typename _U2>
240 tuple(const pair<_U1, _U2>& __in)
241 : _Inherited(_Tuple_impl<0,
242 typename __add_c_ref<_U1>::type,
243 typename __add_c_ref<_U2>::type>(__in.first,
247 template<typename _U1, typename _U2>
249 operator=(const tuple<_U1, _U2>& __in)
251 static_cast<_Inherited&>(*this) = __in;
256 operator=(const tuple& __in)
258 static_cast<_Inherited&>(*this) = __in;
262 template<typename _U1, typename _U2>
264 operator=(const pair<_U1, _U2>& __in)
266 this->_M_head() = __in.first;
267 this->_M_tail()._M_head() = __in.second;
273 /// Gives the type of the ith element of a given tuple type.
274 template<int __i, typename _Tp>
275 struct tuple_element;
279 * Recursive case for tuple_element: strip off the first element in
280 * the tuple and retrieve the (i-1)th element of the remaining tuple.
283 template<int __i, typename _Head, typename... _Tail>
284 struct tuple_element<__i, tuple<_Head, _Tail...> >
285 : tuple_element<__i - 1, tuple<_Tail...> > { };
289 * Basis case for tuple_element: The first element is the one we're seeking.
292 template<typename _Head, typename... _Tail>
293 struct tuple_element<0, tuple<_Head, _Tail...> >
298 /// Finds the size of a given tuple type.
299 template<typename _Tp>
302 /// @brief class tuple_size
303 template<typename... _Elements>
304 struct tuple_size<tuple<_Elements...> >
306 static const int value = sizeof...(_Elements);
309 template<typename... _Elements>
310 const int tuple_size<tuple<_Elements...> >::value;
312 template<int __i, typename _Head, typename... _Tail>
313 inline typename __add_ref<_Head>::type
314 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
315 { return __t._M_head(); }
317 template<int __i, typename _Head, typename... _Tail>
318 inline typename __add_c_ref<_Head>::type
319 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
320 { return __t._M_head(); }
322 // Return a reference (const reference) to the ith element of a tuple.
323 // Any const or non-const ref elements are returned with their original type.
324 template<int __i, typename... _Elements>
325 inline typename __add_ref<
326 typename tuple_element<__i, tuple<_Elements...> >::type
328 get(tuple<_Elements...>& __t)
329 { return __get_helper<__i>(__t); }
331 template<int __i, typename... _Elements>
332 inline typename __add_c_ref<
333 typename tuple_element<__i, tuple<_Elements...> >::type
335 get(const tuple<_Elements...>& __t)
336 { return __get_helper<__i>(__t); }
338 // This class helps construct the various comparison operations on tuples
339 template<int __check_equal_size, int __i, int __j,
340 typename _Tp, typename _Up>
341 struct __tuple_compare;
343 template<int __i, int __j, typename _Tp, typename _Up>
344 struct __tuple_compare<0, __i, __j, _Tp, _Up>
346 static bool __eq(const _Tp& __t, const _Up& __u)
348 return (get<__i>(__t) == get<__i>(__u) &&
349 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
352 static bool __less(const _Tp& __t, const _Up& __u)
354 return ((get<__i>(__t) < get<__i>(__u))
355 || !(get<__i>(__u) < get<__i>(__t)) &&
356 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
360 template<int __i, typename _Tp, typename _Up>
361 struct __tuple_compare<0, __i, __i, _Tp, _Up>
363 static bool __eq(const _Tp&, const _Up&)
366 static bool __less(const _Tp&, const _Up&)
370 template<typename... _TElements, typename... _UElements>
372 operator==(const tuple<_TElements...>& __t,
373 const tuple<_UElements...>& __u)
375 typedef tuple<_TElements...> _Tp;
376 typedef tuple<_UElements...> _Up;
377 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value,
378 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
381 template<typename... _TElements, typename... _UElements>
383 operator<(const tuple<_TElements...>& __t,
384 const tuple<_UElements...>& __u)
386 typedef tuple<_TElements...> _Tp;
387 typedef tuple<_UElements...> _Up;
388 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value,
389 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
392 template<typename... _TElements, typename... _UElements>
394 operator!=(const tuple<_TElements...>& __t,
395 const tuple<_UElements...>& __u)
396 { return !(__t == __u); }
398 template<typename... _TElements, typename... _UElements>
400 operator>(const tuple<_TElements...>& __t,
401 const tuple<_UElements...>& __u)
402 { return __u < __t; }
404 template<typename... _TElements, typename... _UElements>
406 operator<=(const tuple<_TElements...>& __t,
407 const tuple<_UElements...>& __u)
408 { return !(__u < __t); }
410 template<typename... _TElements, typename... _UElements>
412 operator>=(const tuple<_TElements...>& __t,
413 const tuple<_UElements...>& __u)
414 { return !(__t < __u); }
417 template<typename... _Elements>
418 inline tuple<typename __decay_and_strip<_Elements>::__type...>
419 make_tuple(_Elements&&... __args)
421 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
423 return __result_type(std::forward<_Elements>(__args)...);
426 template<int...> struct __index_holder { };
428 template<int __i, typename _IdxHolder, typename... _Elements>
429 struct __index_holder_impl;
431 template<int __i, int... _Indexes, typename _IdxHolder, typename... _Elements>
432 struct __index_holder_impl<__i, __index_holder<_Indexes...>,
433 _IdxHolder, _Elements...>
435 typedef typename __index_holder_impl<__i + 1,
436 __index_holder<_Indexes..., __i>,
437 _Elements...>::type type;
440 template<int __i, int... _Indexes>
441 struct __index_holder_impl<__i, __index_holder<_Indexes...> >
442 { typedef __index_holder<_Indexes...> type; };
444 template<typename... _Elements>
445 struct __make_index_holder
446 : __index_holder_impl<0, __index_holder<>, _Elements...> { };
448 template<typename... _TElements, int... _TIdx,
449 typename... _UElements, int... _UIdx>
450 inline tuple<_TElements..., _UElements...>
451 __tuple_cat_helper(const tuple<_TElements...>& __t,
452 const __index_holder<_TIdx...>&,
453 const tuple<_UElements...>& __u,
454 const __index_holder<_UIdx...>&)
455 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
456 get<_UIdx>(__u)...); }
458 template<typename... _TElements, int... _TIdx,
459 typename... _UElements, int... _UIdx>
460 inline tuple<_TElements..., _UElements...>
461 __tuple_cat_helper(tuple<_TElements...>&& __t,
462 const __index_holder<_TIdx...>&,
463 const tuple<_UElements...>& __u,
464 const __index_holder<_UIdx...>&)
465 { return tuple<_TElements..., _UElements...>(std::move(get<_TIdx>(__t))...,
466 get<_UIdx>(__u)...); }
468 template<typename... _TElements, int... _TIdx,
469 typename... _UElements, int... _UIdx>
470 inline tuple<_TElements..., _UElements...>
471 __tuple_cat_helper(const tuple<_TElements...>& __t,
472 const __index_holder<_TIdx...>&,
473 tuple<_UElements...>&& __u,
474 const __index_holder<_UIdx...>&)
475 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
476 std::move(get<_UIdx>(__u))...); }
478 template<typename... _TElements, int... _TIdx,
479 typename... _UElements, int... _UIdx>
480 inline tuple<_TElements..., _UElements...>
481 __tuple_cat_helper(tuple<_TElements...>&& __t,
482 const __index_holder<_TIdx...>&,
483 tuple<_UElements...>&& __u,
484 const __index_holder<_UIdx...>&)
485 { return tuple<_TElements..., _UElements...>(std::move(get<_TIdx>(__t))...,
486 std::move(get<_UIdx>(__u))...); }
488 template<typename... _TElements, typename... _UElements>
489 inline tuple<_TElements..., _UElements...>
490 tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u)
492 return __tuple_cat_helper(__t, typename
493 __make_index_holder<_TElements...>::type(),
495 __make_index_holder<_UElements...>::type());
498 template<typename... _TElements, typename... _UElements>
499 inline tuple<_TElements..., _UElements...>
500 tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u)
502 return __tuple_cat_helper(std::move(__t), typename
503 __make_index_holder<_TElements...>::type(),
505 __make_index_holder<_UElements...>::type());
508 template<typename... _TElements, typename... _UElements>
509 inline tuple<_TElements..., _UElements...>
510 tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u)
512 return __tuple_cat_helper(__t, typename
513 __make_index_holder<_TElements...>::type(),
514 std::move(__u), typename
515 __make_index_holder<_UElements...>::type());
518 template<typename... _TElements, typename... _UElements>
519 inline tuple<_TElements..., _UElements...>
520 tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
522 return __tuple_cat_helper(std::move(__t), typename
523 __make_index_holder<_TElements...>::type(),
524 std::move(__u), typename
525 __make_index_holder<_UElements...>::type());
528 template<typename... _TElements, typename... _UElements>
529 tuple<_TElements..., _UElements...>
530 operator+(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
531 { return tuple_cat(std::forward<decltype(__t)>(__t),
532 std::forward<decltype(__u)>(__u)); }
534 template<typename... _Elements>
535 inline tuple<_Elements&...>
536 tie(_Elements&... __args)
537 { return tuple<_Elements&...>(__args...); }
539 // A class (and instance) which can be used in 'tie' when an element
540 // of a tuple is not required
541 struct _Swallow_assign
545 operator=(const _Tp&)
549 // TODO: Put this in some kind of shared file.
552 _Swallow_assign ignore;
553 }; // anonymous namespace
556 #endif // _GLIBCXX_CXX0X_TUPLE