3 // Copyright (C) 2007, 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/tuple
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_TUPLE
30 #define _GLIBCXX_TUPLE 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <c++0x_warning.h>
42 // Adds a const reference to a non-reference type.
43 template<typename _Tp>
45 { typedef const _Tp& type; };
47 template<typename _Tp>
48 struct __add_c_ref<_Tp&>
49 { typedef _Tp& type; };
51 // Adds a reference to a non-reference type.
52 template<typename _Tp>
54 { typedef _Tp& type; };
56 template<typename _Tp>
57 struct __add_ref<_Tp&>
58 { typedef _Tp& type; };
60 template<std::size_t _Idx, typename _Head, bool _IsEmpty>
63 template<std::size_t _Idx, typename _Head>
64 struct _Head_base<_Idx, _Head, true>
70 _Head_base(const _Head& __h)
73 template<typename _UHead>
74 _Head_base(_UHead&& __h)
75 : _Head(std::forward<_UHead>(__h)) { }
77 _Head& _M_head() { return *this; }
78 const _Head& _M_head() const { return *this; }
80 void _M_swap_impl(_Head&&) { /* no-op */ }
83 template<std::size_t _Idx, typename _Head>
84 struct _Head_base<_Idx, _Head, false>
89 _Head_base(const _Head& __h)
90 : _M_head_impl(__h) { }
92 template<typename _UHead>
93 _Head_base(_UHead&& __h)
94 : _M_head_impl(std::forward<_UHead>(__h)) { }
96 _Head& _M_head() { return _M_head_impl; }
97 const _Head& _M_head() const { return _M_head_impl; }
100 _M_swap_impl(_Head&& __h)
103 swap(__h, _M_head_impl);
110 * Contains the actual implementation of the @c tuple template, stored
111 * as a recursive inheritance hierarchy from the first element (most
112 * derived class) to the last (least derived class). The @c Idx
113 * parameter gives the 0-based index of the element stored at this
114 * point in the hierarchy; we use it to implement a constant-time
117 template<std::size_t _Idx, typename... _Elements>
121 * Zero-element tuple implementation. This is the basis case for the
122 * inheritance recursion.
124 template<std::size_t _Idx>
125 struct _Tuple_impl<_Idx>
128 void _M_swap_impl(_Tuple_impl&&) { /* no-op */ }
132 * Recursive tuple implementation. Here we store the @c Head element
133 * and derive from a @c Tuple_impl containing the remaining elements
134 * (which contains the @c Tail).
136 template<std::size_t _Idx, typename _Head, typename... _Tail>
137 struct _Tuple_impl<_Idx, _Head, _Tail...>
138 : public _Tuple_impl<_Idx + 1, _Tail...>,
139 private _Head_base<_Idx, _Head, std::is_empty<_Head>::value>
141 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
142 typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
144 _Head& _M_head() { return _Base::_M_head(); }
145 const _Head& _M_head() const { return _Base::_M_head(); }
147 _Inherited& _M_tail() { return *this; }
148 const _Inherited& _M_tail() const { return *this; }
151 : _Inherited(), _Base() { }
154 _Tuple_impl(const _Head& __head, const _Tail&... __tail)
155 : _Inherited(__tail...), _Base(__head) { }
157 template<typename _UHead, typename... _UTail>
159 _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
160 : _Inherited(std::forward<_UTail>(__tail)...),
161 _Base(std::forward<_UHead>(__head)) { }
163 _Tuple_impl(const _Tuple_impl& __in)
164 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
166 _Tuple_impl(_Tuple_impl&& __in)
167 : _Inherited(std::move<_Inherited&&>(__in._M_tail())),
168 _Base(std::forward<_Head>(__in._M_head())) { }
170 template<typename... _UElements>
171 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
172 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
174 template<typename... _UElements>
175 _Tuple_impl(_Tuple_impl<_Idx, _UElements...>&& __in)
176 : _Inherited(std::move<typename _Tuple_impl<_Idx, _UElements...>::
177 _Inherited&&>(__in._M_tail())),
178 _Base(std::forward<typename _Tuple_impl<_Idx, _UElements...>::
179 _Base>(__in._M_head())) { }
182 operator=(const _Tuple_impl& __in)
184 _M_head() = __in._M_head();
185 _M_tail() = __in._M_tail();
190 operator=(_Tuple_impl&& __in)
192 _M_head() = std::move(__in._M_head());
193 _M_tail() = std::move(__in._M_tail());
197 template<typename... _UElements>
199 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
201 _M_head() = __in._M_head();
202 _M_tail() = __in._M_tail();
206 template<typename... _UElements>
208 operator=(_Tuple_impl<_Idx, _UElements...>&& __in)
210 _M_head() = std::move(__in._M_head());
211 _M_tail() = std::move(__in._M_tail());
217 _M_swap_impl(_Tuple_impl&& __in)
219 _Base::_M_swap_impl(__in._M_head());
220 _Inherited::_M_swap_impl(__in._M_tail());
225 template<typename... _Elements>
226 class tuple : public _Tuple_impl<0, _Elements...>
228 typedef _Tuple_impl<0, _Elements...> _Inherited;
235 tuple(const _Elements&... __elements)
236 : _Inherited(__elements...) { }
238 template<typename... _UElements>
240 tuple(_UElements&&... __elements)
241 : _Inherited(std::forward<_UElements>(__elements)...) { }
243 tuple(const tuple& __in)
244 : _Inherited(static_cast<const _Inherited&>(__in)) { }
247 : _Inherited(std::move<_Inherited>(__in)) { }
249 template<typename... _UElements>
250 tuple(const tuple<_UElements...>& __in)
251 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
254 template<typename... _UElements>
255 tuple(tuple<_UElements...>&& __in)
256 : _Inherited(std::move<_Tuple_impl<0, _UElements...> >(__in)) { }
258 // XXX http://gcc.gnu.org/ml/libstdc++/2008-02/msg00047.html
259 template<typename... _UElements>
260 tuple(tuple<_UElements...>& __in)
261 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
265 operator=(const tuple& __in)
267 static_cast<_Inherited&>(*this) = __in;
272 operator=(tuple&& __in)
274 static_cast<_Inherited&>(*this) = std::move(__in);
278 template<typename... _UElements>
280 operator=(const tuple<_UElements...>& __in)
282 static_cast<_Inherited&>(*this) = __in;
286 template<typename... _UElements>
288 operator=(tuple<_UElements...>&& __in)
290 static_cast<_Inherited&>(*this) = std::move(__in);
296 { _Inherited::_M_swap_impl(__in); }
304 void swap(tuple&&) { /* no-op */ }
307 /// tuple (2-element), with construction and assignment from a pair.
308 template<typename _T1, typename _T2>
309 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
311 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
318 tuple(const _T1& __a1, const _T2& __a2)
319 : _Inherited(__a1, __a2) { }
321 template<typename _U1, typename _U2>
323 tuple(_U1&& __a1, _U2&& __a2)
324 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
326 tuple(const tuple& __in)
327 : _Inherited(static_cast<const _Inherited&>(__in)) { }
330 : _Inherited(std::move<_Inherited>(__in)) { }
332 template<typename _U1, typename _U2>
333 tuple(const tuple<_U1, _U2>& __in)
334 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
336 template<typename _U1, typename _U2>
337 tuple(tuple<_U1, _U2>&& __in)
338 : _Inherited(std::move<_Tuple_impl<0, _U1, _U2> >(__in)) { }
340 template<typename _U1, typename _U2>
341 tuple(const pair<_U1, _U2>& __in)
342 : _Inherited(__in.first, __in.second) { }
344 template<typename _U1, typename _U2>
345 tuple(pair<_U1, _U2>&& __in)
346 : _Inherited(std::move(__in.first), std::move(__in.second)) { }
349 operator=(const tuple& __in)
351 static_cast<_Inherited&>(*this) = __in;
356 operator=(tuple&& __in)
358 static_cast<_Inherited&>(*this) = std::move(__in);
362 template<typename _U1, typename _U2>
364 operator=(const tuple<_U1, _U2>& __in)
366 static_cast<_Inherited&>(*this) = __in;
370 template<typename _U1, typename _U2>
372 operator=(tuple<_U1, _U2>&& __in)
374 static_cast<_Inherited&>(*this) = std::move(__in);
378 template<typename _U1, typename _U2>
380 operator=(const pair<_U1, _U2>& __in)
382 this->_M_head() = __in.first;
383 this->_M_tail()._M_head() = __in.second;
387 template<typename _U1, typename _U2>
389 operator=(pair<_U1, _U2>&& __in)
391 this->_M_head() = std::move(__in.first);
392 this->_M_tail()._M_head() = std::move(__in.second);
400 swap(this->_M_head(), __in._M_head());
401 swap(this->_M_tail()._M_head(), __in._M_tail()._M_head());
406 /// Gives the type of the ith element of a given tuple type.
407 template<std::size_t __i, typename _Tp>
408 struct tuple_element;
411 * Recursive case for tuple_element: strip off the first element in
412 * the tuple and retrieve the (i-1)th element of the remaining tuple.
414 template<std::size_t __i, typename _Head, typename... _Tail>
415 struct tuple_element<__i, tuple<_Head, _Tail...> >
416 : tuple_element<__i - 1, tuple<_Tail...> > { };
419 * Basis case for tuple_element: The first element is the one we're seeking.
421 template<typename _Head, typename... _Tail>
422 struct tuple_element<0, tuple<_Head, _Tail...> >
427 /// Finds the size of a given tuple type.
428 template<typename _Tp>
432 template<typename... _Elements>
433 struct tuple_size<tuple<_Elements...> >
435 static const std::size_t value = sizeof...(_Elements);
438 template<typename... _Elements>
439 const std::size_t tuple_size<tuple<_Elements...> >::value;
441 template<std::size_t __i, typename _Head, typename... _Tail>
442 inline typename __add_ref<_Head>::type
443 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
444 { return __t._M_head(); }
446 template<std::size_t __i, typename _Head, typename... _Tail>
447 inline typename __add_c_ref<_Head>::type
448 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
449 { return __t._M_head(); }
451 // Return a reference (const reference) to the ith element of a tuple.
452 // Any const or non-const ref elements are returned with their original type.
453 template<std::size_t __i, typename... _Elements>
454 inline typename __add_ref<
455 typename tuple_element<__i, tuple<_Elements...> >::type
457 get(tuple<_Elements...>& __t)
458 { return __get_helper<__i>(__t); }
460 template<std::size_t __i, typename... _Elements>
461 inline typename __add_c_ref<
462 typename tuple_element<__i, tuple<_Elements...> >::type
464 get(const tuple<_Elements...>& __t)
465 { return __get_helper<__i>(__t); }
467 // This class helps construct the various comparison operations on tuples
468 template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
469 typename _Tp, typename _Up>
470 struct __tuple_compare;
472 template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
473 struct __tuple_compare<0, __i, __j, _Tp, _Up>
475 static bool __eq(const _Tp& __t, const _Up& __u)
477 return (get<__i>(__t) == get<__i>(__u) &&
478 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
481 static bool __less(const _Tp& __t, const _Up& __u)
483 return ((get<__i>(__t) < get<__i>(__u))
484 || !(get<__i>(__u) < get<__i>(__t)) &&
485 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
489 template<std::size_t __i, typename _Tp, typename _Up>
490 struct __tuple_compare<0, __i, __i, _Tp, _Up>
492 static bool __eq(const _Tp&, const _Up&)
495 static bool __less(const _Tp&, const _Up&)
499 template<typename... _TElements, typename... _UElements>
501 operator==(const tuple<_TElements...>& __t,
502 const tuple<_UElements...>& __u)
504 typedef tuple<_TElements...> _Tp;
505 typedef tuple<_UElements...> _Up;
506 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
507 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
510 template<typename... _TElements, typename... _UElements>
512 operator<(const tuple<_TElements...>& __t,
513 const tuple<_UElements...>& __u)
515 typedef tuple<_TElements...> _Tp;
516 typedef tuple<_UElements...> _Up;
517 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
518 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
521 template<typename... _TElements, typename... _UElements>
523 operator!=(const tuple<_TElements...>& __t,
524 const tuple<_UElements...>& __u)
525 { return !(__t == __u); }
527 template<typename... _TElements, typename... _UElements>
529 operator>(const tuple<_TElements...>& __t,
530 const tuple<_UElements...>& __u)
531 { return __u < __t; }
533 template<typename... _TElements, typename... _UElements>
535 operator<=(const tuple<_TElements...>& __t,
536 const tuple<_UElements...>& __u)
537 { return !(__u < __t); }
539 template<typename... _TElements, typename... _UElements>
541 operator>=(const tuple<_TElements...>& __t,
542 const tuple<_UElements...>& __u)
543 { return !(__t < __u); }
546 template<typename... _Elements>
547 inline tuple<typename __decay_and_strip<_Elements>::__type...>
548 make_tuple(_Elements&&... __args)
550 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
552 return __result_type(std::forward<_Elements>(__args)...);
555 template<std::size_t...> struct __index_holder { };
557 template<std::size_t __i, typename _IdxHolder, typename... _Elements>
558 struct __index_holder_impl;
560 template<std::size_t __i, std::size_t... _Indexes, typename _IdxHolder,
561 typename... _Elements>
562 struct __index_holder_impl<__i, __index_holder<_Indexes...>,
563 _IdxHolder, _Elements...>
565 typedef typename __index_holder_impl<__i + 1,
566 __index_holder<_Indexes..., __i>,
567 _Elements...>::type type;
570 template<std::size_t __i, std::size_t... _Indexes>
571 struct __index_holder_impl<__i, __index_holder<_Indexes...> >
572 { typedef __index_holder<_Indexes...> type; };
574 template<typename... _Elements>
575 struct __make_index_holder
576 : __index_holder_impl<0, __index_holder<>, _Elements...> { };
578 template<typename... _TElements, std::size_t... _TIdx,
579 typename... _UElements, std::size_t... _UIdx>
580 inline tuple<_TElements..., _UElements...>
581 __tuple_cat_helper(const tuple<_TElements...>& __t,
582 const __index_holder<_TIdx...>&,
583 const tuple<_UElements...>& __u,
584 const __index_holder<_UIdx...>&)
585 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
586 get<_UIdx>(__u)...); }
588 template<typename... _TElements, std::size_t... _TIdx,
589 typename... _UElements, std::size_t... _UIdx>
590 inline tuple<_TElements..., _UElements...>
591 __tuple_cat_helper(tuple<_TElements...>&& __t,
592 const __index_holder<_TIdx...>&,
593 const tuple<_UElements...>& __u,
594 const __index_holder<_UIdx...>&)
595 { return tuple<_TElements..., _UElements...>
596 (std::move(get<_TIdx>(__t))..., get<_UIdx>(__u)...); }
598 template<typename... _TElements, std::size_t... _TIdx,
599 typename... _UElements, std::size_t... _UIdx>
600 inline tuple<_TElements..., _UElements...>
601 __tuple_cat_helper(const tuple<_TElements...>& __t,
602 const __index_holder<_TIdx...>&,
603 tuple<_UElements...>&& __u,
604 const __index_holder<_UIdx...>&)
605 { return tuple<_TElements..., _UElements...>
606 (get<_TIdx>(__t)..., std::move(get<_UIdx>(__u))...); }
608 template<typename... _TElements, std::size_t... _TIdx,
609 typename... _UElements, std::size_t... _UIdx>
610 inline tuple<_TElements..., _UElements...>
611 __tuple_cat_helper(tuple<_TElements...>&& __t,
612 const __index_holder<_TIdx...>&,
613 tuple<_UElements...>&& __u,
614 const __index_holder<_UIdx...>&)
615 { return tuple<_TElements..., _UElements...>
616 (std::move(get<_TIdx>(__t))..., std::move(get<_UIdx>(__u))...); }
618 template<typename... _TElements, typename... _UElements>
619 inline tuple<_TElements..., _UElements...>
620 tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u)
622 return __tuple_cat_helper(__t, typename
623 __make_index_holder<_TElements...>::type(),
625 __make_index_holder<_UElements...>::type());
628 template<typename... _TElements, typename... _UElements>
629 inline tuple<_TElements..., _UElements...>
630 tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u)
632 return __tuple_cat_helper(std::move(__t), typename
633 __make_index_holder<_TElements...>::type(),
635 __make_index_holder<_UElements...>::type());
638 template<typename... _TElements, typename... _UElements>
639 inline tuple<_TElements..., _UElements...>
640 tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u)
642 return __tuple_cat_helper(__t, typename
643 __make_index_holder<_TElements...>::type(),
644 std::move(__u), typename
645 __make_index_holder<_UElements...>::type());
648 template<typename... _TElements, typename... _UElements>
649 inline tuple<_TElements..., _UElements...>
650 tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
652 return __tuple_cat_helper(std::move(__t), typename
653 __make_index_holder<_TElements...>::type(),
654 std::move(__u), typename
655 __make_index_holder<_UElements...>::type());
658 template<typename... _Elements>
659 inline tuple<_Elements&...>
660 tie(_Elements&... __args)
661 { return tuple<_Elements&...>(__args...); }
663 template<typename... _Elements>
665 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
668 template<typename... _Elements>
670 swap(tuple<_Elements...>&& __x, tuple<_Elements...>& __y)
673 template<typename... _Elements>
675 swap(tuple<_Elements...>& __x, tuple<_Elements...>&& __y)
678 // A class (and instance) which can be used in 'tie' when an element
679 // of a tuple is not required
680 struct _Swallow_assign
684 operator=(const _Tp&)
688 // TODO: Put this in some kind of shared file.
691 _Swallow_assign ignore;
692 }; // anonymous namespace
695 #endif // __GXX_EXPERIMENTAL_CXX0X__
697 #endif // _GLIBCXX_TUPLE