1 // class template tuple -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 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 along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
31 * This is a TR1 C++ Library header.
34 // Chris Jefferson <chris@bubblescope.net>
35 // Variadic Templates support by Douglas Gregor <doug.gregor@gmail.com>
37 #ifndef _GLIBCXX_TR1_TUPLE
38 #define _GLIBCXX_TR1_TUPLE 1
40 #pragma GCC system_header
48 // Adds a const reference to a non-reference type.
49 template<typename _Tp>
51 { typedef const _Tp& type; };
53 template<typename _Tp>
54 struct __add_c_ref<_Tp&>
55 { typedef _Tp& type; };
57 // Adds a reference to a non-reference type.
58 template<typename _Tp>
60 { typedef _Tp& type; };
62 template<typename _Tp>
63 struct __add_ref<_Tp&>
64 { typedef _Tp& type; };
67 * Contains the actual implementation of the @c tuple template, stored
68 * as a recursive inheritance hierarchy from the first element (most
69 * derived class) to the last (least derived class). The @c Idx
70 * parameter gives the 0-based index of the element stored at this
71 * point in the hierarchy; we use it to implement a constant-time
74 template<int _Idx, typename... _Elements>
78 * Zero-element tuple implementation. This is the basis case for the
79 * inheritance recursion.
82 struct _Tuple_impl<_Idx> { };
85 * Recursive tuple implementation. Here we store the @c Head element
86 * and derive from a @c Tuple_impl containing the remaining elements
87 * (which contains the @c Tail).
89 template<int _Idx, typename _Head, typename... _Tail>
90 struct _Tuple_impl<_Idx, _Head, _Tail...>
91 : public _Tuple_impl<_Idx + 1, _Tail...>
93 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
97 _Inherited& _M_tail() { return *this; }
98 const _Inherited& _M_tail() const { return *this; }
100 _Tuple_impl() : _Inherited(), _M_head() { }
103 _Tuple_impl(typename __add_c_ref<_Head>::type __head,
104 typename __add_c_ref<_Tail>::type... __tail)
105 : _Inherited(__tail...), _M_head(__head) { }
107 template<typename... _UElements>
108 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
109 : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
111 _Tuple_impl(const _Tuple_impl& __in)
112 : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
114 template<typename... _UElements>
116 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
118 _M_head = __in._M_head;
119 _M_tail() = __in._M_tail();
124 operator=(const _Tuple_impl& __in)
126 _M_head = __in._M_head;
127 _M_tail() = __in._M_tail();
132 template<typename... _Elements>
133 class tuple : public _Tuple_impl<0, _Elements...>
135 typedef _Tuple_impl<0, _Elements...> _Inherited;
138 tuple() : _Inherited() { }
141 tuple(typename __add_c_ref<_Elements>::type... __elements)
142 : _Inherited(__elements...) { }
144 template<typename... _UElements>
145 tuple(const tuple<_UElements...>& __in)
146 : _Inherited(__in) { }
148 tuple(const tuple& __in)
149 : _Inherited(__in) { }
151 template<typename... _UElements>
153 operator=(const tuple<_UElements...>& __in)
155 static_cast<_Inherited&>(*this) = __in;
160 operator=(const tuple& __in)
162 static_cast<_Inherited&>(*this) = __in;
167 template<> class tuple<> { };
169 // 2-element tuple, with construction and assignment from a pair.
170 template<typename _T1, typename _T2>
171 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
173 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
176 tuple() : _Inherited() { }
179 tuple(typename __add_c_ref<_T1>::type __a1,
180 typename __add_c_ref<_T2>::type __a2)
181 : _Inherited(__a1, __a2) { }
183 template<typename _U1, typename _U2>
184 tuple(const tuple<_U1, _U2>& __in)
185 : _Inherited(__in) { }
187 tuple(const tuple& __in)
188 : _Inherited(__in) { }
190 template<typename _U1, typename _U2>
191 tuple(const pair<_U1, _U2>& __in)
192 : _Inherited(_Tuple_impl<0,
193 typename __add_c_ref<_U1>::type,
194 typename __add_c_ref<_U2>::type>(__in.first,
198 template<typename _U1, typename _U2>
200 operator=(const tuple<_U1, _U2>& __in)
202 static_cast<_Inherited&>(*this) = __in;
207 operator=(const tuple& __in)
209 static_cast<_Inherited&>(*this) = __in;
213 template<typename _U1, typename _U2>
215 operator=(const pair<_U1, _U2>& __in)
217 this->_M_head = __in.first;
218 this->_M_tail()._M_head = __in.second;
224 /// Gives the type of the ith element of a given tuple type.
225 template<int __i, typename _Tp>
226 struct tuple_element;
229 * Recursive case for tuple_element: strip off the first element in
230 * the tuple and retrieve the (i-1)th element of the remaining tuple.
232 template<int __i, typename _Head, typename... _Tail>
233 struct tuple_element<__i, tuple<_Head, _Tail...> >
234 : tuple_element<__i - 1, tuple<_Tail...> > { };
237 * Basis case for tuple_element: The first element is the one we're seeking.
239 template<typename _Head, typename... _Tail>
240 struct tuple_element<0, tuple<_Head, _Tail...> >
245 /// Finds the size of a given tuple type.
246 template<typename _Tp>
250 template<typename... _Elements>
251 struct tuple_size<tuple<_Elements...> >
253 static const int value = sizeof...(_Elements);
256 template<typename... _Elements>
257 const int tuple_size<tuple<_Elements...> >::value;
259 template<int __i, typename _Head, typename... _Tail>
260 inline typename __add_ref<_Head>::type
261 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
266 template<int __i, typename _Head, typename... _Tail>
267 inline typename __add_c_ref<_Head>::type
268 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
273 // Return a reference (const reference) to the ith element of a tuple.
274 // Any const or non-const ref elements are returned with their original type.
275 template<int __i, typename... _Elements>
276 inline typename __add_ref<
277 typename tuple_element<__i, tuple<_Elements...> >::type
279 get(tuple<_Elements...>& __t)
281 return __get_helper<__i>(__t);
284 template<int __i, typename... _Elements>
285 inline typename __add_c_ref<
286 typename tuple_element<__i, tuple<_Elements...> >::type
288 get(const tuple<_Elements...>& __t)
290 return __get_helper<__i>(__t);
293 // This class helps construct the various comparison operations on tuples
294 template<int __check_equal_size, int __i, int __j,
295 typename _Tp, typename _Up>
296 struct __tuple_compare;
298 template<int __i, int __j, typename _Tp, typename _Up>
299 struct __tuple_compare<0, __i, __j, _Tp, _Up>
301 static bool __eq(const _Tp& __t, const _Up& __u)
303 return (get<__i>(__t) == get<__i>(__u) &&
304 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
307 static bool __less(const _Tp& __t, const _Up& __u)
309 return ((get<__i>(__t) < get<__i>(__u))
310 || !(get<__i>(__u) < get<__i>(__t)) &&
311 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
315 template<int __i, typename _Tp, typename _Up>
316 struct __tuple_compare<0, __i, __i, _Tp, _Up>
318 static bool __eq(const _Tp&, const _Up&)
321 static bool __less(const _Tp&, const _Up&)
325 template<typename... _TElements, typename... _UElements>
327 operator==(const tuple<_TElements...>& __t,
328 const tuple<_UElements...>& __u)
330 typedef tuple<_TElements...> _Tp;
331 typedef tuple<_UElements...> _Up;
332 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
333 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
336 template<typename... _TElements, typename... _UElements>
338 operator<(const tuple<_TElements...>& __t,
339 const tuple<_UElements...>& __u)
341 typedef tuple<_TElements...> _Tp;
342 typedef tuple<_UElements...> _Up;
343 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
344 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
347 template<typename... _TElements, typename... _UElements>
349 operator!=(const tuple<_TElements...>& __t,
350 const tuple<_UElements...>& __u)
351 { return !(__t == __u); }
353 template<typename... _TElements, typename... _UElements>
355 operator>(const tuple<_TElements...>& __t,
356 const tuple<_UElements...>& __u)
357 { return __u < __t; }
359 template<typename... _TElements, typename... _UElements>
361 operator<=(const tuple<_TElements...>& __t,
362 const tuple<_UElements...>& __u)
363 { return !(__u < __t); }
365 template<typename... _TElements, typename... _UElements>
367 operator>=(const tuple<_TElements...>& __t,
368 const tuple<_UElements...>& __u)
369 { return !(__t < __u); }
371 template<typename _Tp>
372 class reference_wrapper;
374 // Helper which adds a reference to a type when given a reference_wrapper
375 template<typename _Tp>
376 struct __strip_reference_wrapper
381 template<typename _Tp>
382 struct __strip_reference_wrapper<reference_wrapper<_Tp> >
387 template<typename _Tp>
388 struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
393 template<typename... _Elements>
394 inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
395 make_tuple(_Elements... __args)
397 typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
399 return __result_type(__args...);
402 template<typename... _Elements>
403 inline tuple<_Elements&...>
404 tie(_Elements&... __args)
406 return tuple<_Elements&...>(__args...);
409 // A class (and instance) which can be used in 'tie' when an element
410 // of a tuple is not required
411 struct _Swallow_assign
415 operator=(const _Tp&)
419 // TODO: Put this in some kind of shared file.
422 _Swallow_assign ignore;
423 }; // anonymous namespace
427 #endif // _GLIBCXX_TR1_TUPLE