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>
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(__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(__in._M_tail())),
177 _Base(std::move(__in._M_head())) { }
180 operator=(const _Tuple_impl& __in)
182 _M_head() = __in._M_head();
183 _M_tail() = __in._M_tail();
188 operator=(_Tuple_impl&& __in)
190 _M_head() = std::move(__in._M_head());
191 _M_tail() = std::move(__in._M_tail());
195 template<typename... _UElements>
197 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
199 _M_head() = __in._M_head();
200 _M_tail() = __in._M_tail();
204 template<typename... _UElements>
206 operator=(_Tuple_impl<_Idx, _UElements...>&& __in)
208 _M_head() = std::move(__in._M_head());
209 _M_tail() = std::move(__in._M_tail());
215 _M_swap_impl(_Tuple_impl& __in)
217 _Base::_M_swap_impl(__in._M_head());
218 _Inherited::_M_swap_impl(__in._M_tail());
223 template<typename... _Elements>
224 class tuple : public _Tuple_impl<0, _Elements...>
226 typedef _Tuple_impl<0, _Elements...> _Inherited;
233 tuple(const _Elements&... __elements)
234 : _Inherited(__elements...) { }
236 template<typename... _UElements>
238 tuple(_UElements&&... __elements)
239 : _Inherited(std::forward<_UElements>(__elements)...) { }
241 tuple(const tuple&) = default;
244 : _Inherited(static_cast<_Inherited&&>(__in)) { }
246 template<typename... _UElements>
247 tuple(const tuple<_UElements...>& __in)
248 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
251 template<typename... _UElements>
252 tuple(tuple<_UElements...>&& __in)
253 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
255 // XXX http://gcc.gnu.org/ml/libstdc++/2008-02/msg00047.html
256 template<typename... _UElements>
257 tuple(tuple<_UElements...>& __in)
258 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
262 operator=(const tuple& __in)
264 static_cast<_Inherited&>(*this) = __in;
269 operator=(tuple&& __in)
271 static_cast<_Inherited&>(*this) = std::move(__in);
275 template<typename... _UElements>
277 operator=(const tuple<_UElements...>& __in)
279 static_cast<_Inherited&>(*this) = __in;
283 template<typename... _UElements>
285 operator=(tuple<_UElements...>&& __in)
287 static_cast<_Inherited&>(*this) = std::move(__in);
293 { _Inherited::_M_swap_impl(__in); }
301 void swap(tuple&) { /* no-op */ }
304 /// tuple (2-element), with construction and assignment from a pair.
305 template<typename _T1, typename _T2>
306 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
308 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
315 tuple(const _T1& __a1, const _T2& __a2)
316 : _Inherited(__a1, __a2) { }
318 template<typename _U1, typename _U2>
320 tuple(_U1&& __a1, _U2&& __a2)
321 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
323 tuple(const tuple&) = default;
326 : _Inherited(static_cast<_Inherited&&>(__in)) { }
328 template<typename _U1, typename _U2>
329 tuple(const tuple<_U1, _U2>& __in)
330 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
332 template<typename _U1, typename _U2>
333 tuple(tuple<_U1, _U2>&& __in)
334 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
336 template<typename _U1, typename _U2>
337 tuple(const pair<_U1, _U2>& __in)
338 : _Inherited(__in.first, __in.second) { }
340 template<typename _U1, typename _U2>
341 tuple(pair<_U1, _U2>&& __in)
342 : _Inherited(std::forward<_U1>(__in.first),
343 std::forward<_U2>(__in.second)) { }
346 operator=(const tuple& __in)
348 static_cast<_Inherited&>(*this) = __in;
353 operator=(tuple&& __in)
355 static_cast<_Inherited&>(*this) = std::move(__in);
359 template<typename _U1, typename _U2>
361 operator=(const tuple<_U1, _U2>& __in)
363 static_cast<_Inherited&>(*this) = __in;
367 template<typename _U1, typename _U2>
369 operator=(tuple<_U1, _U2>&& __in)
371 static_cast<_Inherited&>(*this) = std::move(__in);
375 template<typename _U1, typename _U2>
377 operator=(const pair<_U1, _U2>& __in)
379 this->_M_head() = __in.first;
380 this->_M_tail()._M_head() = __in.second;
384 template<typename _U1, typename _U2>
386 operator=(pair<_U1, _U2>&& __in)
388 this->_M_head() = std::move(__in.first);
389 this->_M_tail()._M_head() = std::move(__in.second);
397 swap(this->_M_head(), __in._M_head());
398 swap(this->_M_tail()._M_head(), __in._M_tail()._M_head());
403 /// Gives the type of the ith element of a given tuple type.
404 template<std::size_t __i, typename _Tp>
405 struct tuple_element;
408 * Recursive case for tuple_element: strip off the first element in
409 * the tuple and retrieve the (i-1)th element of the remaining tuple.
411 template<std::size_t __i, typename _Head, typename... _Tail>
412 struct tuple_element<__i, tuple<_Head, _Tail...> >
413 : tuple_element<__i - 1, tuple<_Tail...> > { };
416 * Basis case for tuple_element: The first element is the one we're seeking.
418 template<typename _Head, typename... _Tail>
419 struct tuple_element<0, tuple<_Head, _Tail...> >
424 /// Finds the size of a given tuple type.
425 template<typename _Tp>
429 template<typename... _Elements>
430 struct tuple_size<tuple<_Elements...> >
432 static const std::size_t value = sizeof...(_Elements);
435 template<typename... _Elements>
436 const std::size_t tuple_size<tuple<_Elements...> >::value;
438 template<std::size_t __i, typename _Head, typename... _Tail>
439 inline typename __add_ref<_Head>::type
440 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
441 { return __t._M_head(); }
443 template<std::size_t __i, typename _Head, typename... _Tail>
444 inline typename __add_c_ref<_Head>::type
445 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
446 { return __t._M_head(); }
448 // Return a reference (const reference) to the ith element of a tuple.
449 // Any const or non-const ref elements are returned with their original type.
450 template<std::size_t __i, typename... _Elements>
451 inline typename __add_ref<
452 typename tuple_element<__i, tuple<_Elements...> >::type
454 get(tuple<_Elements...>& __t)
455 { return __get_helper<__i>(__t); }
457 template<std::size_t __i, typename... _Elements>
458 inline typename __add_c_ref<
459 typename tuple_element<__i, tuple<_Elements...> >::type
461 get(const tuple<_Elements...>& __t)
462 { return __get_helper<__i>(__t); }
464 // This class helps construct the various comparison operations on tuples
465 template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
466 typename _Tp, typename _Up>
467 struct __tuple_compare;
469 template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
470 struct __tuple_compare<0, __i, __j, _Tp, _Up>
472 static bool __eq(const _Tp& __t, const _Up& __u)
474 return (get<__i>(__t) == get<__i>(__u) &&
475 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
478 static bool __less(const _Tp& __t, const _Up& __u)
480 return ((get<__i>(__t) < get<__i>(__u))
481 || !(get<__i>(__u) < get<__i>(__t)) &&
482 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
486 template<std::size_t __i, typename _Tp, typename _Up>
487 struct __tuple_compare<0, __i, __i, _Tp, _Up>
489 static bool __eq(const _Tp&, const _Up&)
492 static bool __less(const _Tp&, const _Up&)
496 template<typename... _TElements, typename... _UElements>
498 operator==(const tuple<_TElements...>& __t,
499 const tuple<_UElements...>& __u)
501 typedef tuple<_TElements...> _Tp;
502 typedef tuple<_UElements...> _Up;
503 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
504 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
507 template<typename... _TElements, typename... _UElements>
509 operator<(const tuple<_TElements...>& __t,
510 const tuple<_UElements...>& __u)
512 typedef tuple<_TElements...> _Tp;
513 typedef tuple<_UElements...> _Up;
514 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
515 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
518 template<typename... _TElements, typename... _UElements>
520 operator!=(const tuple<_TElements...>& __t,
521 const tuple<_UElements...>& __u)
522 { return !(__t == __u); }
524 template<typename... _TElements, typename... _UElements>
526 operator>(const tuple<_TElements...>& __t,
527 const tuple<_UElements...>& __u)
528 { return __u < __t; }
530 template<typename... _TElements, typename... _UElements>
532 operator<=(const tuple<_TElements...>& __t,
533 const tuple<_UElements...>& __u)
534 { return !(__u < __t); }
536 template<typename... _TElements, typename... _UElements>
538 operator>=(const tuple<_TElements...>& __t,
539 const tuple<_UElements...>& __u)
540 { return !(__t < __u); }
543 template<typename... _Elements>
544 inline tuple<typename __decay_and_strip<_Elements>::__type...>
545 make_tuple(_Elements&&... __args)
547 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
549 return __result_type(std::forward<_Elements>(__args)...);
552 template<typename _Tp, bool = is_array<_Tp>::value>
553 struct __pa_add_rvalue_reference_helper
554 { typedef typename std::add_rvalue_reference<_Tp>::type __type; };
556 template<typename _Tp>
557 struct __pa_add_rvalue_reference_helper<_Tp, true>
558 { typedef _Tp& __type; };
560 template<typename _Tp>
561 struct __pa_add_rvalue_reference
562 : public __pa_add_rvalue_reference_helper<_Tp>
565 template<typename... _Elements>
566 inline tuple<typename __pa_add_rvalue_reference<_Elements>::__type...>
567 pack_arguments(_Elements&&... __args)
569 typedef tuple<typename __pa_add_rvalue_reference<_Elements>::__type...>
571 return __result_type(std::forward<_Elements>(__args)...);
574 template<std::size_t...> struct __index_holder { };
576 template<std::size_t __i, typename _IdxHolder, typename... _Elements>
577 struct __index_holder_impl;
579 template<std::size_t __i, std::size_t... _Indexes, typename _IdxHolder,
580 typename... _Elements>
581 struct __index_holder_impl<__i, __index_holder<_Indexes...>,
582 _IdxHolder, _Elements...>
584 typedef typename __index_holder_impl<__i + 1,
585 __index_holder<_Indexes..., __i>,
586 _Elements...>::type type;
589 template<std::size_t __i, std::size_t... _Indexes>
590 struct __index_holder_impl<__i, __index_holder<_Indexes...> >
591 { typedef __index_holder<_Indexes...> type; };
593 template<typename... _Elements>
594 struct __make_index_holder
595 : __index_holder_impl<0, __index_holder<>, _Elements...> { };
597 template<typename... _TElements, std::size_t... _TIdx,
598 typename... _UElements, std::size_t... _UIdx>
599 inline tuple<_TElements..., _UElements...>
600 __tuple_cat_helper(const tuple<_TElements...>& __t,
601 const __index_holder<_TIdx...>&,
602 const tuple<_UElements...>& __u,
603 const __index_holder<_UIdx...>&)
604 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
605 get<_UIdx>(__u)...); }
607 template<typename... _TElements, std::size_t... _TIdx,
608 typename... _UElements, std::size_t... _UIdx>
609 inline tuple<_TElements..., _UElements...>
610 __tuple_cat_helper(tuple<_TElements...>&& __t,
611 const __index_holder<_TIdx...>&,
612 const tuple<_UElements...>& __u,
613 const __index_holder<_UIdx...>&)
614 { return tuple<_TElements..., _UElements...>
615 (std::move(get<_TIdx>(__t))..., get<_UIdx>(__u)...); }
617 template<typename... _TElements, std::size_t... _TIdx,
618 typename... _UElements, std::size_t... _UIdx>
619 inline tuple<_TElements..., _UElements...>
620 __tuple_cat_helper(const tuple<_TElements...>& __t,
621 const __index_holder<_TIdx...>&,
622 tuple<_UElements...>&& __u,
623 const __index_holder<_UIdx...>&)
624 { return tuple<_TElements..., _UElements...>
625 (get<_TIdx>(__t)..., std::move(get<_UIdx>(__u))...); }
627 template<typename... _TElements, std::size_t... _TIdx,
628 typename... _UElements, std::size_t... _UIdx>
629 inline tuple<_TElements..., _UElements...>
630 __tuple_cat_helper(tuple<_TElements...>&& __t,
631 const __index_holder<_TIdx...>&,
632 tuple<_UElements...>&& __u,
633 const __index_holder<_UIdx...>&)
634 { return tuple<_TElements..., _UElements...>
635 (std::move(get<_TIdx>(__t))..., std::move(get<_UIdx>(__u))...); }
637 template<typename... _TElements, typename... _UElements>
638 inline tuple<_TElements..., _UElements...>
639 tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u)
641 return __tuple_cat_helper(__t, typename
642 __make_index_holder<_TElements...>::type(),
644 __make_index_holder<_UElements...>::type());
647 template<typename... _TElements, typename... _UElements>
648 inline tuple<_TElements..., _UElements...>
649 tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u)
651 return __tuple_cat_helper(std::move(__t), typename
652 __make_index_holder<_TElements...>::type(),
654 __make_index_holder<_UElements...>::type());
657 template<typename... _TElements, typename... _UElements>
658 inline tuple<_TElements..., _UElements...>
659 tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u)
661 return __tuple_cat_helper(__t, typename
662 __make_index_holder<_TElements...>::type(),
663 std::move(__u), typename
664 __make_index_holder<_UElements...>::type());
667 template<typename... _TElements, typename... _UElements>
668 inline tuple<_TElements..., _UElements...>
669 tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
671 return __tuple_cat_helper(std::move(__t), typename
672 __make_index_holder<_TElements...>::type(),
673 std::move(__u), typename
674 __make_index_holder<_UElements...>::type());
677 template<typename... _Elements>
678 inline tuple<_Elements&...>
679 tie(_Elements&... __args)
680 { return tuple<_Elements&...>(__args...); }
682 template<typename... _Elements>
684 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
687 // A class (and instance) which can be used in 'tie' when an element
688 // of a tuple is not required
689 struct _Swallow_assign
692 const _Swallow_assign&
693 operator=(const _Tp&) const
697 const _Swallow_assign ignore{};
700 * Stores a tuple of indices. Used by bind() to extract the elements
703 template<int... _Indexes>
706 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
709 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
710 template<std::size_t _Num>
711 struct _Build_index_tuple
713 typedef typename _Build_index_tuple<_Num-1>::__type::__next __type;
717 struct _Build_index_tuple<0>
719 typedef _Index_tuple<> __type;
723 template<class _T1, class _T2>
724 template<typename _Tp, typename... _Args>
727 __cons(tuple<_Args...>&& __tuple)
729 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
731 return __do_cons<_Tp>(std::move(__tuple), _Indexes());
734 template<class _T1, class _T2>
735 template<typename _Tp, typename... _Args, int... _Indexes>
738 __do_cons(tuple<_Args...>&& __tuple,
739 const _Index_tuple<_Indexes...>&)
740 { return _Tp(std::forward<_Args>(get<_Indexes>(__tuple))...); }
743 #endif // __GXX_EXPERIMENTAL_CXX0X__
745 #endif // _GLIBCXX_TUPLE