3 // Copyright (C) 2007, 2008, 2009, 2010 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 <bits/c++0x_warning.h>
40 _GLIBCXX_BEGIN_NAMESPACE(std)
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>
67 constexpr _Head_base()
70 constexpr _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; }
81 _M_swap_impl(_Head& __h)
88 template<std::size_t _Idx, typename _Head>
89 struct _Head_base<_Idx, _Head, false>
91 constexpr _Head_base()
94 constexpr _Head_base(const _Head& __h)
95 : _M_head_impl(__h) { }
97 template<typename _UHead>
98 _Head_base(_UHead&& __h)
99 : _M_head_impl(std::forward<_UHead>(__h)) { }
101 _Head& _M_head() { return _M_head_impl; }
102 const _Head& _M_head() const { return _M_head_impl; }
105 _M_swap_impl(_Head& __h)
108 swap(__h, _M_head());
115 * Contains the actual implementation of the @c tuple template, stored
116 * as a recursive inheritance hierarchy from the first element (most
117 * derived class) to the last (least derived class). The @c Idx
118 * parameter gives the 0-based index of the element stored at this
119 * point in the hierarchy; we use it to implement a constant-time
122 template<std::size_t _Idx, typename... _Elements>
126 * Zero-element tuple implementation. This is the basis case for the
127 * inheritance recursion.
129 template<std::size_t _Idx>
130 struct _Tuple_impl<_Idx>
133 void _M_swap_impl(_Tuple_impl&) { /* no-op */ }
137 * Recursive tuple implementation. Here we store the @c Head element
138 * and derive from a @c Tuple_impl containing the remaining elements
139 * (which contains the @c Tail).
141 template<std::size_t _Idx, typename _Head, typename... _Tail>
142 struct _Tuple_impl<_Idx, _Head, _Tail...>
143 : public _Tuple_impl<_Idx + 1, _Tail...>,
144 private _Head_base<_Idx, _Head, std::is_empty<_Head>::value>
146 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
147 typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
149 _Head& _M_head() { return _Base::_M_head(); }
150 const _Head& _M_head() const { return _Base::_M_head(); }
152 _Inherited& _M_tail() { return *this; }
153 const _Inherited& _M_tail() const { return *this; }
155 constexpr _Tuple_impl()
156 : _Inherited(), _Base() { }
159 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
160 : _Inherited(__tail...), _Base(__head) { }
162 template<typename _UHead, typename... _UTail>
164 _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
165 : _Inherited(std::forward<_UTail>(__tail)...),
166 _Base(std::forward<_UHead>(__head)) { }
168 constexpr _Tuple_impl(const _Tuple_impl&) = default;
170 _Tuple_impl(_Tuple_impl&& __in)
171 : _Inherited(std::move(__in._M_tail())),
172 _Base(std::forward<_Head>(__in._M_head())) { }
174 template<typename... _UElements>
175 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
176 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
178 template<typename... _UElements>
179 _Tuple_impl(_Tuple_impl<_Idx, _UElements...>&& __in)
180 : _Inherited(std::move(__in._M_tail())),
181 _Base(std::move(__in._M_head())) { }
184 operator=(const _Tuple_impl& __in)
186 _M_head() = __in._M_head();
187 _M_tail() = __in._M_tail();
192 operator=(_Tuple_impl&& __in)
194 _M_head() = std::move(__in._M_head());
195 _M_tail() = std::move(__in._M_tail());
199 template<typename... _UElements>
201 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
203 _M_head() = __in._M_head();
204 _M_tail() = __in._M_tail();
208 template<typename... _UElements>
210 operator=(_Tuple_impl<_Idx, _UElements...>&& __in)
212 _M_head() = std::move(__in._M_head());
213 _M_tail() = std::move(__in._M_tail());
219 _M_swap_impl(_Tuple_impl& __in)
221 _Base::_M_swap_impl(__in._M_head());
222 _Inherited::_M_swap_impl(__in._M_tail());
227 template<typename... _Elements>
228 class tuple : public _Tuple_impl<0, _Elements...>
230 typedef _Tuple_impl<0, _Elements...> _Inherited;
237 constexpr tuple(const _Elements&... __elements)
238 : _Inherited(__elements...) { }
240 template<typename... _UElements, typename = typename
241 std::enable_if<sizeof...(_UElements)
242 == sizeof...(_Elements)>::type>
244 tuple(_UElements&&... __elements)
245 : _Inherited(std::forward<_UElements>(__elements)...) { }
247 constexpr tuple(const tuple&) = default;
250 : _Inherited(static_cast<_Inherited&&>(__in)) { }
252 template<typename... _UElements, typename = typename
253 std::enable_if<sizeof...(_UElements)
254 == sizeof...(_Elements)>::type>
255 tuple(const tuple<_UElements...>& __in)
256 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
259 template<typename... _UElements, typename = typename
260 std::enable_if<sizeof...(_UElements)
261 == sizeof...(_Elements)>::type>
262 tuple(tuple<_UElements...>&& __in)
263 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
266 operator=(const tuple& __in)
268 static_cast<_Inherited&>(*this) = __in;
273 operator=(tuple&& __in)
275 static_cast<_Inherited&>(*this) = std::move(__in);
279 template<typename... _UElements, typename = typename
280 std::enable_if<sizeof...(_UElements)
281 == sizeof...(_Elements)>::type>
283 operator=(const tuple<_UElements...>& __in)
285 static_cast<_Inherited&>(*this) = __in;
289 template<typename... _UElements, typename = typename
290 std::enable_if<sizeof...(_UElements)
291 == sizeof...(_Elements)>::type>
293 operator=(tuple<_UElements...>&& __in)
295 static_cast<_Inherited&>(*this) = std::move(__in);
301 { _Inherited::_M_swap_impl(__in); }
308 void swap(tuple&) { /* no-op */ }
311 /// tuple (2-element), with construction and assignment from a pair.
312 template<typename _T1, typename _T2>
313 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
315 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
322 constexpr tuple(const _T1& __a1, const _T2& __a2)
323 : _Inherited(__a1, __a2) { }
325 template<typename _U1, typename _U2>
327 tuple(_U1&& __a1, _U2&& __a2)
328 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
330 constexpr tuple(const tuple&) = default;
333 : _Inherited(static_cast<_Inherited&&>(__in)) { }
335 template<typename _U1, typename _U2>
336 tuple(const tuple<_U1, _U2>& __in)
337 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
339 template<typename _U1, typename _U2>
340 tuple(tuple<_U1, _U2>&& __in)
341 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
343 template<typename _U1, typename _U2>
344 tuple(const pair<_U1, _U2>& __in)
345 : _Inherited(__in.first, __in.second) { }
347 template<typename _U1, typename _U2>
348 tuple(pair<_U1, _U2>&& __in)
349 : _Inherited(std::forward<_U1>(__in.first),
350 std::forward<_U2>(__in.second)) { }
353 operator=(const tuple& __in)
355 static_cast<_Inherited&>(*this) = __in;
360 operator=(tuple&& __in)
362 static_cast<_Inherited&>(*this) = std::move(__in);
366 template<typename _U1, typename _U2>
368 operator=(const tuple<_U1, _U2>& __in)
370 static_cast<_Inherited&>(*this) = __in;
374 template<typename _U1, typename _U2>
376 operator=(tuple<_U1, _U2>&& __in)
378 static_cast<_Inherited&>(*this) = std::move(__in);
382 template<typename _U1, typename _U2>
384 operator=(const pair<_U1, _U2>& __in)
386 this->_M_head() = __in.first;
387 this->_M_tail()._M_head() = __in.second;
391 template<typename _U1, typename _U2>
393 operator=(pair<_U1, _U2>&& __in)
395 this->_M_head() = std::forward<_U1>(__in.first);
396 this->_M_tail()._M_head() = std::forward<_U2>(__in.second);
404 swap(this->_M_head(), __in._M_head());
405 swap(this->_M_tail()._M_head(), __in._M_tail()._M_head());
409 /// tuple (1-element).
410 template<typename _T1>
411 class tuple<_T1> : public _Tuple_impl<0, _T1>
413 typedef _Tuple_impl<0, _T1> _Inherited;
420 constexpr tuple(const _T1& __a1)
421 : _Inherited(__a1) { }
423 template<typename _U1, typename = typename
424 std::enable_if<std::is_convertible<_U1, _T1>::value>::type>
427 : _Inherited(std::forward<_U1>(__a1)) { }
429 constexpr tuple(const tuple&) = default;
432 : _Inherited(static_cast<_Inherited&&>(__in)) { }
434 template<typename _U1>
435 tuple(const tuple<_U1>& __in)
436 : _Inherited(static_cast<const _Tuple_impl<0, _U1>&>(__in)) { }
438 template<typename _U1>
439 tuple(tuple<_U1>&& __in)
440 : _Inherited(static_cast<_Tuple_impl<0, _U1>&&>(__in)) { }
443 operator=(const tuple& __in)
445 static_cast<_Inherited&>(*this) = __in;
450 operator=(tuple&& __in)
452 static_cast<_Inherited&>(*this) = std::move(__in);
456 template<typename _U1>
458 operator=(const tuple<_U1>& __in)
460 static_cast<_Inherited&>(*this) = __in;
464 template<typename _U1>
466 operator=(tuple<_U1>&& __in)
468 static_cast<_Inherited&>(*this) = std::move(__in);
474 { _Inherited::_M_swap_impl(__in); }
478 /// Gives the type of the ith element of a given tuple type.
479 template<std::size_t __i, typename _Tp>
480 struct tuple_element;
483 * Recursive case for tuple_element: strip off the first element in
484 * the tuple and retrieve the (i-1)th element of the remaining tuple.
486 template<std::size_t __i, typename _Head, typename... _Tail>
487 struct tuple_element<__i, tuple<_Head, _Tail...> >
488 : tuple_element<__i - 1, tuple<_Tail...> > { };
491 * Basis case for tuple_element: The first element is the one we're seeking.
493 template<typename _Head, typename... _Tail>
494 struct tuple_element<0, tuple<_Head, _Tail...> >
499 /// Finds the size of a given tuple type.
500 template<typename _Tp>
504 template<typename... _Elements>
505 struct tuple_size<tuple<_Elements...> >
507 static const std::size_t value = sizeof...(_Elements);
510 template<typename... _Elements>
511 const std::size_t tuple_size<tuple<_Elements...> >::value;
513 template<std::size_t __i, typename _Head, typename... _Tail>
514 inline typename __add_ref<_Head>::type
515 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
516 { return __t._M_head(); }
518 template<std::size_t __i, typename _Head, typename... _Tail>
519 inline typename __add_c_ref<_Head>::type
520 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
521 { return __t._M_head(); }
523 // Return a reference (const reference) to the ith element of a tuple.
524 // Any const or non-const ref elements are returned with their original type.
525 template<std::size_t __i, typename... _Elements>
526 inline typename __add_ref<
527 typename tuple_element<__i, tuple<_Elements...> >::type
529 get(tuple<_Elements...>& __t)
530 { return __get_helper<__i>(__t); }
532 template<std::size_t __i, typename... _Elements>
533 inline typename __add_c_ref<
534 typename tuple_element<__i, tuple<_Elements...> >::type
536 get(const tuple<_Elements...>& __t)
537 { return __get_helper<__i>(__t); }
539 // This class helps construct the various comparison operations on tuples
540 template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
541 typename _Tp, typename _Up>
542 struct __tuple_compare;
544 template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
545 struct __tuple_compare<0, __i, __j, _Tp, _Up>
547 static bool __eq(const _Tp& __t, const _Up& __u)
549 return (get<__i>(__t) == get<__i>(__u) &&
550 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
553 static bool __less(const _Tp& __t, const _Up& __u)
555 return ((get<__i>(__t) < get<__i>(__u))
556 || !(get<__i>(__u) < get<__i>(__t)) &&
557 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
561 template<std::size_t __i, typename _Tp, typename _Up>
562 struct __tuple_compare<0, __i, __i, _Tp, _Up>
564 static bool __eq(const _Tp&, const _Up&)
567 static bool __less(const _Tp&, const _Up&)
571 template<typename... _TElements, typename... _UElements>
573 operator==(const tuple<_TElements...>& __t,
574 const tuple<_UElements...>& __u)
576 typedef tuple<_TElements...> _Tp;
577 typedef tuple<_UElements...> _Up;
578 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
579 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
582 template<typename... _TElements, typename... _UElements>
584 operator<(const tuple<_TElements...>& __t,
585 const tuple<_UElements...>& __u)
587 typedef tuple<_TElements...> _Tp;
588 typedef tuple<_UElements...> _Up;
589 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
590 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
593 template<typename... _TElements, typename... _UElements>
595 operator!=(const tuple<_TElements...>& __t,
596 const tuple<_UElements...>& __u)
597 { return !(__t == __u); }
599 template<typename... _TElements, typename... _UElements>
601 operator>(const tuple<_TElements...>& __t,
602 const tuple<_UElements...>& __u)
603 { return __u < __t; }
605 template<typename... _TElements, typename... _UElements>
607 operator<=(const tuple<_TElements...>& __t,
608 const tuple<_UElements...>& __u)
609 { return !(__u < __t); }
611 template<typename... _TElements, typename... _UElements>
613 operator>=(const tuple<_TElements...>& __t,
614 const tuple<_UElements...>& __u)
615 { return !(__t < __u); }
618 template<typename... _Elements>
619 inline tuple<typename __decay_and_strip<_Elements>::__type...>
620 make_tuple(_Elements&&... __args)
622 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
624 return __result_type(std::forward<_Elements>(__args)...);
627 template<typename... _Elements>
628 inline tuple<_Elements&&...>
629 forward_as_tuple(_Elements&&... __args)
630 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
632 template<std::size_t...> struct __index_holder { };
634 template<std::size_t __i, typename _IdxHolder, typename... _Elements>
635 struct __index_holder_impl;
637 template<std::size_t __i, std::size_t... _Indexes, typename _IdxHolder,
638 typename... _Elements>
639 struct __index_holder_impl<__i, __index_holder<_Indexes...>,
640 _IdxHolder, _Elements...>
642 typedef typename __index_holder_impl<__i + 1,
643 __index_holder<_Indexes..., __i>,
644 _Elements...>::type type;
647 template<std::size_t __i, std::size_t... _Indexes>
648 struct __index_holder_impl<__i, __index_holder<_Indexes...> >
649 { typedef __index_holder<_Indexes...> type; };
651 template<typename... _Elements>
652 struct __make_index_holder
653 : __index_holder_impl<0, __index_holder<>, _Elements...> { };
655 template<typename... _TElements, std::size_t... _TIdx,
656 typename... _UElements, std::size_t... _UIdx>
657 inline tuple<_TElements..., _UElements...>
658 __tuple_cat_helper(const tuple<_TElements...>& __t,
659 const __index_holder<_TIdx...>&,
660 const tuple<_UElements...>& __u,
661 const __index_holder<_UIdx...>&)
662 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
663 get<_UIdx>(__u)...); }
665 template<typename... _TElements, std::size_t... _TIdx,
666 typename... _UElements, std::size_t... _UIdx>
667 inline tuple<_TElements..., _UElements...>
668 __tuple_cat_helper(tuple<_TElements...>&& __t,
669 const __index_holder<_TIdx...>&,
670 const tuple<_UElements...>& __u,
671 const __index_holder<_UIdx...>&)
672 { return tuple<_TElements..., _UElements...>
673 (std::move(get<_TIdx>(__t))..., get<_UIdx>(__u)...); }
675 template<typename... _TElements, std::size_t... _TIdx,
676 typename... _UElements, std::size_t... _UIdx>
677 inline tuple<_TElements..., _UElements...>
678 __tuple_cat_helper(const tuple<_TElements...>& __t,
679 const __index_holder<_TIdx...>&,
680 tuple<_UElements...>&& __u,
681 const __index_holder<_UIdx...>&)
682 { return tuple<_TElements..., _UElements...>
683 (get<_TIdx>(__t)..., std::move(get<_UIdx>(__u))...); }
685 template<typename... _TElements, std::size_t... _TIdx,
686 typename... _UElements, std::size_t... _UIdx>
687 inline tuple<_TElements..., _UElements...>
688 __tuple_cat_helper(tuple<_TElements...>&& __t,
689 const __index_holder<_TIdx...>&,
690 tuple<_UElements...>&& __u,
691 const __index_holder<_UIdx...>&)
692 { return tuple<_TElements..., _UElements...>
693 (std::move(get<_TIdx>(__t))..., std::move(get<_UIdx>(__u))...); }
695 template<typename... _TElements, typename... _UElements>
696 inline tuple<_TElements..., _UElements...>
697 tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u)
699 return __tuple_cat_helper(__t, typename
700 __make_index_holder<_TElements...>::type(),
702 __make_index_holder<_UElements...>::type());
705 template<typename... _TElements, typename... _UElements>
706 inline tuple<_TElements..., _UElements...>
707 tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u)
709 return __tuple_cat_helper(std::move(__t), typename
710 __make_index_holder<_TElements...>::type(),
712 __make_index_holder<_UElements...>::type());
715 template<typename... _TElements, typename... _UElements>
716 inline tuple<_TElements..., _UElements...>
717 tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u)
719 return __tuple_cat_helper(__t, typename
720 __make_index_holder<_TElements...>::type(),
721 std::move(__u), typename
722 __make_index_holder<_UElements...>::type());
725 template<typename... _TElements, typename... _UElements>
726 inline tuple<_TElements..., _UElements...>
727 tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
729 return __tuple_cat_helper(std::move(__t), typename
730 __make_index_holder<_TElements...>::type(),
731 std::move(__u), typename
732 __make_index_holder<_UElements...>::type());
735 template<typename... _Elements>
736 inline tuple<_Elements&...>
737 tie(_Elements&... __args)
738 { return tuple<_Elements&...>(__args...); }
740 template<typename... _Elements>
742 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
745 // A class (and instance) which can be used in 'tie' when an element
746 // of a tuple is not required
747 struct _Swallow_assign
750 const _Swallow_assign&
751 operator=(const _Tp&) const
755 const _Swallow_assign ignore{};
758 * Stores a tuple of indices. Used by bind() to extract the elements
761 template<int... _Indexes>
764 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
767 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
768 template<std::size_t _Num>
769 struct _Build_index_tuple
771 typedef typename _Build_index_tuple<_Num-1>::__type::__next __type;
775 struct _Build_index_tuple<0>
777 typedef _Index_tuple<> __type;
781 template<class _T1, class _T2>
782 template<typename _Tp, typename... _Args>
785 __cons(tuple<_Args...>&& __tuple)
787 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
789 return __do_cons<_Tp>(std::move(__tuple), _Indexes());
792 template<class _T1, class _T2>
793 template<typename _Tp, typename... _Args, int... _Indexes>
796 __do_cons(tuple<_Args...>&& __tuple,
797 const _Index_tuple<_Indexes...>&)
798 { return _Tp(std::forward<_Args>(get<_Indexes>(__tuple))...); }
800 _GLIBCXX_END_NAMESPACE
802 #endif // __GXX_EXPERIMENTAL_CXX0X__
804 #endif // _GLIBCXX_TUPLE