3 // Copyright (C) 2007, 2008 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_TUPLE
35 #define _GLIBCXX_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<std::size_t _Idx, typename _Head, bool _IsEmpty>
68 template<std::size_t _Idx, typename _Head>
69 struct _Head_base<_Idx, _Head, true>
75 _Head_base(const _Head& __h)
78 template<typename _UHead>
79 _Head_base(_UHead&& __h)
80 : _Head(std::forward<_UHead>(__h)) { }
82 _Head& _M_head() { return *this; }
83 const _Head& _M_head() const { return *this; }
85 void __swap_impl(_Head&&) { /* no-op */ }
88 template<std::size_t _Idx, typename _Head>
89 struct _Head_base<_Idx, _Head, false>
94 _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 __swap_impl(_Head&& __h)
108 swap(__h, _M_head_impl);
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>
132 void __swap_impl(_Tuple_impl&&) { /* no-op */ }
136 * Recursive tuple implementation. Here we store the @c Head element
137 * and derive from a @c Tuple_impl containing the remaining elements
138 * (which contains the @c Tail).
140 template<std::size_t _Idx, typename _Head, typename... _Tail>
141 struct _Tuple_impl<_Idx, _Head, _Tail...>
142 : public _Tuple_impl<_Idx + 1, _Tail...>,
143 private _Head_base<_Idx, _Head, std::is_empty<_Head>::value>
145 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
146 typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
148 _Head& _M_head() { return _Base::_M_head(); }
149 const _Head& _M_head() const { return _Base::_M_head(); }
151 _Inherited& _M_tail() { return *this; }
152 const _Inherited& _M_tail() const { return *this; }
155 : _Inherited(), _Base() { }
158 _Tuple_impl(const _Head& __head, const _Tail&... __tail)
159 : _Inherited(__tail...), _Base(__head) { }
161 template<typename _UHead, typename... _UTail>
163 _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
164 : _Inherited(std::forward<_UTail>(__tail)...),
165 _Base(std::forward<_UHead>(__head)) { }
167 _Tuple_impl(const _Tuple_impl& __in)
168 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
170 _Tuple_impl(_Tuple_impl&& __in)
171 : _Inherited(std::move<_Inherited&&>(__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<typename _Tuple_impl<_Idx, _UElements...>::
181 _Inherited&&>(__in._M_tail())),
182 _Base(std::forward<typename _Tuple_impl<_Idx, _UElements...>::
183 _Base>(__in._M_head())) { }
186 operator=(const _Tuple_impl& __in)
188 _M_head() = __in._M_head();
189 _M_tail() = __in._M_tail();
194 operator=(_Tuple_impl&& __in)
196 _M_head() = std::move(__in._M_head());
197 _M_tail() = std::move(__in._M_tail());
201 template<typename... _UElements>
203 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
205 _M_head() = __in._M_head();
206 _M_tail() = __in._M_tail();
210 template<typename... _UElements>
212 operator=(_Tuple_impl<_Idx, _UElements...>&& __in)
214 _M_head() = std::move(__in._M_head());
215 _M_tail() = std::move(__in._M_tail());
220 __swap_impl(_Tuple_impl&& __in)
222 _Base::__swap_impl(__in._M_head());
223 _Inherited::__swap_impl(__in._M_tail());
228 template<typename... _Elements>
229 class tuple : public _Tuple_impl<0, _Elements...>
231 typedef _Tuple_impl<0, _Elements...> _Inherited;
238 tuple(const _Elements&... __elements)
239 : _Inherited(__elements...) { }
241 template<typename... _UElements>
243 tuple(_UElements&&... __elements)
244 : _Inherited(std::forward<_UElements>(__elements)...) { }
246 tuple(const tuple& __in)
247 : _Inherited(static_cast<const _Inherited&>(__in)) { }
250 : _Inherited(std::move<_Inherited>(__in)) { }
252 template<typename... _UElements>
253 tuple(const tuple<_UElements...>& __in)
254 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
257 template<typename... _UElements>
258 tuple(tuple<_UElements...>&& __in)
259 : _Inherited(std::move<_Tuple_impl<0, _UElements...> >(__in)) { }
261 // XXX http://gcc.gnu.org/ml/libstdc++/2008-02/msg00047.html
262 template<typename... _UElements>
263 tuple(tuple<_UElements...>& __in)
264 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
268 operator=(const tuple& __in)
270 static_cast<_Inherited&>(*this) = __in;
275 operator=(tuple&& __in)
277 static_cast<_Inherited&>(*this) = std::move(__in);
281 template<typename... _UElements>
283 operator=(const tuple<_UElements...>& __in)
285 static_cast<_Inherited&>(*this) = __in;
289 template<typename... _UElements>
291 operator=(tuple<_UElements...>&& __in)
293 static_cast<_Inherited&>(*this) = std::move(__in);
299 { _Inherited::__swap_impl(__in); }
307 void swap(tuple&&) { /* no-op */ }
310 /// tuple (2-element), with construction and assignment from a pair.
311 template<typename _T1, typename _T2>
312 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
314 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
321 tuple(const _T1& __a1, const _T2& __a2)
322 : _Inherited(__a1, __a2) { }
324 template<typename _U1, typename _U2>
326 tuple(_U1&& __a1, _U2&& __a2)
327 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
329 tuple(const tuple& __in)
330 : _Inherited(static_cast<const _Inherited&>(__in)) { }
333 : _Inherited(std::move<_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(std::move<_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::move(__in.first), std::move(__in.second)) { }
352 operator=(const tuple& __in)
354 static_cast<_Inherited&>(*this) = __in;
359 operator=(tuple&& __in)
361 static_cast<_Inherited&>(*this) = std::move(__in);
365 template<typename _U1, typename _U2>
367 operator=(const tuple<_U1, _U2>& __in)
369 static_cast<_Inherited&>(*this) = __in;
373 template<typename _U1, typename _U2>
375 operator=(tuple<_U1, _U2>&& __in)
377 static_cast<_Inherited&>(*this) = std::move(__in);
381 template<typename _U1, typename _U2>
383 operator=(const pair<_U1, _U2>& __in)
385 this->_M_head() = __in.first;
386 this->_M_tail()._M_head() = __in.second;
390 template<typename _U1, typename _U2>
392 operator=(pair<_U1, _U2>&& __in)
394 this->_M_head() = std::move(__in.first);
395 this->_M_tail()._M_head() = std::move(__in.second);
403 swap(this->_M_head(), __in._M_head());
404 swap(this->_M_tail()._M_head(), __in._M_tail()._M_head());
409 /// Gives the type of the ith element of a given tuple type.
410 template<std::size_t __i, typename _Tp>
411 struct tuple_element;
414 * Recursive case for tuple_element: strip off the first element in
415 * the tuple and retrieve the (i-1)th element of the remaining tuple.
417 template<std::size_t __i, typename _Head, typename... _Tail>
418 struct tuple_element<__i, tuple<_Head, _Tail...> >
419 : tuple_element<__i - 1, tuple<_Tail...> > { };
422 * Basis case for tuple_element: The first element is the one we're seeking.
424 template<typename _Head, typename... _Tail>
425 struct tuple_element<0, tuple<_Head, _Tail...> >
430 /// Finds the size of a given tuple type.
431 template<typename _Tp>
435 template<typename... _Elements>
436 struct tuple_size<tuple<_Elements...> >
438 static const std::size_t value = sizeof...(_Elements);
441 template<typename... _Elements>
442 const std::size_t tuple_size<tuple<_Elements...> >::value;
444 template<std::size_t __i, typename _Head, typename... _Tail>
445 inline typename __add_ref<_Head>::type
446 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
447 { return __t._M_head(); }
449 template<std::size_t __i, typename _Head, typename... _Tail>
450 inline typename __add_c_ref<_Head>::type
451 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
452 { return __t._M_head(); }
454 // Return a reference (const reference) to the ith element of a tuple.
455 // Any const or non-const ref elements are returned with their original type.
456 template<std::size_t __i, typename... _Elements>
457 inline typename __add_ref<
458 typename tuple_element<__i, tuple<_Elements...> >::type
460 get(tuple<_Elements...>& __t)
461 { return __get_helper<__i>(__t); }
463 template<std::size_t __i, typename... _Elements>
464 inline typename __add_c_ref<
465 typename tuple_element<__i, tuple<_Elements...> >::type
467 get(const tuple<_Elements...>& __t)
468 { return __get_helper<__i>(__t); }
470 // This class helps construct the various comparison operations on tuples
471 template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
472 typename _Tp, typename _Up>
473 struct __tuple_compare;
475 template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
476 struct __tuple_compare<0, __i, __j, _Tp, _Up>
478 static bool __eq(const _Tp& __t, const _Up& __u)
480 return (get<__i>(__t) == get<__i>(__u) &&
481 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
484 static bool __less(const _Tp& __t, const _Up& __u)
486 return ((get<__i>(__t) < get<__i>(__u))
487 || !(get<__i>(__u) < get<__i>(__t)) &&
488 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
492 template<std::size_t __i, typename _Tp, typename _Up>
493 struct __tuple_compare<0, __i, __i, _Tp, _Up>
495 static bool __eq(const _Tp&, const _Up&)
498 static bool __less(const _Tp&, const _Up&)
502 template<typename... _TElements, typename... _UElements>
504 operator==(const tuple<_TElements...>& __t,
505 const tuple<_UElements...>& __u)
507 typedef tuple<_TElements...> _Tp;
508 typedef tuple<_UElements...> _Up;
509 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
510 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
513 template<typename... _TElements, typename... _UElements>
515 operator<(const tuple<_TElements...>& __t,
516 const tuple<_UElements...>& __u)
518 typedef tuple<_TElements...> _Tp;
519 typedef tuple<_UElements...> _Up;
520 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
521 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
524 template<typename... _TElements, typename... _UElements>
526 operator!=(const tuple<_TElements...>& __t,
527 const tuple<_UElements...>& __u)
528 { return !(__t == __u); }
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 !(__u < __t); }
542 template<typename... _TElements, typename... _UElements>
544 operator>=(const tuple<_TElements...>& __t,
545 const tuple<_UElements...>& __u)
546 { return !(__t < __u); }
549 template<typename... _Elements>
550 inline tuple<typename __decay_and_strip<_Elements>::__type...>
551 make_tuple(_Elements&&... __args)
553 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
555 return __result_type(std::forward<_Elements>(__args)...);
558 template<std::size_t...> struct __index_holder { };
560 template<std::size_t __i, typename _IdxHolder, typename... _Elements>
561 struct __index_holder_impl;
563 template<std::size_t __i, std::size_t... _Indexes, typename _IdxHolder,
564 typename... _Elements>
565 struct __index_holder_impl<__i, __index_holder<_Indexes...>,
566 _IdxHolder, _Elements...>
568 typedef typename __index_holder_impl<__i + 1,
569 __index_holder<_Indexes..., __i>,
570 _Elements...>::type type;
573 template<std::size_t __i, std::size_t... _Indexes>
574 struct __index_holder_impl<__i, __index_holder<_Indexes...> >
575 { typedef __index_holder<_Indexes...> type; };
577 template<typename... _Elements>
578 struct __make_index_holder
579 : __index_holder_impl<0, __index_holder<>, _Elements...> { };
581 template<typename... _TElements, std::size_t... _TIdx,
582 typename... _UElements, std::size_t... _UIdx>
583 inline tuple<_TElements..., _UElements...>
584 __tuple_cat_helper(const tuple<_TElements...>& __t,
585 const __index_holder<_TIdx...>&,
586 const tuple<_UElements...>& __u,
587 const __index_holder<_UIdx...>&)
588 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
589 get<_UIdx>(__u)...); }
591 template<typename... _TElements, std::size_t... _TIdx,
592 typename... _UElements, std::size_t... _UIdx>
593 inline tuple<_TElements..., _UElements...>
594 __tuple_cat_helper(tuple<_TElements...>&& __t,
595 const __index_holder<_TIdx...>&,
596 const tuple<_UElements...>& __u,
597 const __index_holder<_UIdx...>&)
598 { return tuple<_TElements..., _UElements...>
599 (std::move(get<_TIdx>(__t))..., get<_UIdx>(__u)...); }
601 template<typename... _TElements, std::size_t... _TIdx,
602 typename... _UElements, std::size_t... _UIdx>
603 inline tuple<_TElements..., _UElements...>
604 __tuple_cat_helper(const tuple<_TElements...>& __t,
605 const __index_holder<_TIdx...>&,
606 tuple<_UElements...>&& __u,
607 const __index_holder<_UIdx...>&)
608 { return tuple<_TElements..., _UElements...>
609 (get<_TIdx>(__t)..., std::move(get<_UIdx>(__u))...); }
611 template<typename... _TElements, std::size_t... _TIdx,
612 typename... _UElements, std::size_t... _UIdx>
613 inline tuple<_TElements..., _UElements...>
614 __tuple_cat_helper(tuple<_TElements...>&& __t,
615 const __index_holder<_TIdx...>&,
616 tuple<_UElements...>&& __u,
617 const __index_holder<_UIdx...>&)
618 { return tuple<_TElements..., _UElements...>
619 (std::move(get<_TIdx>(__t))..., std::move(get<_UIdx>(__u))...); }
621 template<typename... _TElements, typename... _UElements>
622 inline tuple<_TElements..., _UElements...>
623 tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u)
625 return __tuple_cat_helper(__t, typename
626 __make_index_holder<_TElements...>::type(),
628 __make_index_holder<_UElements...>::type());
631 template<typename... _TElements, typename... _UElements>
632 inline tuple<_TElements..., _UElements...>
633 tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u)
635 return __tuple_cat_helper(std::move(__t), typename
636 __make_index_holder<_TElements...>::type(),
638 __make_index_holder<_UElements...>::type());
641 template<typename... _TElements, typename... _UElements>
642 inline tuple<_TElements..., _UElements...>
643 tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u)
645 return __tuple_cat_helper(__t, typename
646 __make_index_holder<_TElements...>::type(),
647 std::move(__u), typename
648 __make_index_holder<_UElements...>::type());
651 template<typename... _TElements, typename... _UElements>
652 inline tuple<_TElements..., _UElements...>
653 tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
655 return __tuple_cat_helper(std::move(__t), typename
656 __make_index_holder<_TElements...>::type(),
657 std::move(__u), typename
658 __make_index_holder<_UElements...>::type());
661 template<typename... _Elements>
662 inline tuple<_Elements&...>
663 tie(_Elements&... __args)
664 { return tuple<_Elements&...>(__args...); }
666 template<typename... _Elements>
668 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
671 template<typename... _Elements>
673 swap(tuple<_Elements...>&& __x, tuple<_Elements...>& __y)
676 template<typename... _Elements>
678 swap(tuple<_Elements...>& __x, tuple<_Elements...>&& __y)
681 // A class (and instance) which can be used in 'tie' when an element
682 // of a tuple is not required
683 struct _Swallow_assign
687 operator=(const _Tp&)
691 // TODO: Put this in some kind of shared file.
694 _Swallow_assign ignore;
695 }; // anonymous namespace
698 #endif // __GXX_EXPERIMENTAL_CXX0X__
700 #endif // _GLIBCXX_TUPLE