1 // class template tuple -*- C++ -*-
3 // Copyright (C) 2004-2018 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/>.
26 * This is a TR1 C++ Library header.
29 // Chris Jefferson <chris@bubblescope.net>
30 // Variadic Templates support by Douglas Gregor <doug.gregor@gmail.com>
32 #ifndef _GLIBCXX_TR1_TUPLE
33 #define _GLIBCXX_TR1_TUPLE 1
35 #pragma GCC system_header
39 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 // Adds a const reference to a non-reference type.
46 template<typename _Tp>
48 { typedef const _Tp& type; };
50 template<typename _Tp>
51 struct __add_c_ref<_Tp&>
52 { typedef _Tp& type; };
54 // Adds a reference to a non-reference type.
55 template<typename _Tp>
57 { typedef _Tp& type; };
59 template<typename _Tp>
60 struct __add_ref<_Tp&>
61 { typedef _Tp& type; };
64 * Contains the actual implementation of the @c tuple template, stored
65 * as a recursive inheritance hierarchy from the first element (most
66 * derived class) to the last (least derived class). The @c Idx
67 * parameter gives the 0-based index of the element stored at this
68 * point in the hierarchy; we use it to implement a constant-time
71 template<int _Idx, typename... _Elements>
75 * Zero-element tuple implementation. This is the basis case for the
76 * inheritance recursion.
79 struct _Tuple_impl<_Idx> { };
82 * Recursive tuple implementation. Here we store the @c Head element
83 * and derive from a @c Tuple_impl containing the remaining elements
84 * (which contains the @c Tail).
86 template<int _Idx, typename _Head, typename... _Tail>
87 struct _Tuple_impl<_Idx, _Head, _Tail...>
88 : public _Tuple_impl<_Idx + 1, _Tail...>
90 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
94 _Inherited& _M_tail() { return *this; }
95 const _Inherited& _M_tail() const { return *this; }
97 _Tuple_impl() : _Inherited(), _M_head() { }
100 _Tuple_impl(typename __add_c_ref<_Head>::type __head,
101 typename __add_c_ref<_Tail>::type... __tail)
102 : _Inherited(__tail...), _M_head(__head) { }
104 template<typename... _UElements>
105 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
106 : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
108 _Tuple_impl(const _Tuple_impl& __in)
109 : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
111 template<typename... _UElements>
113 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
115 _M_head = __in._M_head;
116 _M_tail() = __in._M_tail();
121 operator=(const _Tuple_impl& __in)
123 _M_head = __in._M_head;
124 _M_tail() = __in._M_tail();
129 template<typename... _Elements>
130 class tuple : public _Tuple_impl<0, _Elements...>
132 typedef _Tuple_impl<0, _Elements...> _Inherited;
135 tuple() : _Inherited() { }
138 tuple(typename __add_c_ref<_Elements>::type... __elements)
139 : _Inherited(__elements...) { }
141 template<typename... _UElements>
142 tuple(const tuple<_UElements...>& __in)
143 : _Inherited(__in) { }
145 tuple(const tuple& __in)
146 : _Inherited(__in) { }
148 template<typename... _UElements>
150 operator=(const tuple<_UElements...>& __in)
152 static_cast<_Inherited&>(*this) = __in;
157 operator=(const tuple& __in)
159 static_cast<_Inherited&>(*this) = __in;
164 template<> class tuple<> { };
166 // 2-element tuple, with construction and assignment from a pair.
167 template<typename _T1, typename _T2>
168 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
170 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
173 tuple() : _Inherited() { }
176 tuple(typename __add_c_ref<_T1>::type __a1,
177 typename __add_c_ref<_T2>::type __a2)
178 : _Inherited(__a1, __a2) { }
180 template<typename _U1, typename _U2>
181 tuple(const tuple<_U1, _U2>& __in)
182 : _Inherited(__in) { }
184 tuple(const tuple& __in)
185 : _Inherited(__in) { }
187 template<typename _U1, typename _U2>
188 tuple(const pair<_U1, _U2>& __in)
189 : _Inherited(_Tuple_impl<0,
190 typename __add_c_ref<_U1>::type,
191 typename __add_c_ref<_U2>::type>(__in.first,
195 template<typename _U1, typename _U2>
197 operator=(const tuple<_U1, _U2>& __in)
199 static_cast<_Inherited&>(*this) = __in;
204 operator=(const tuple& __in)
206 static_cast<_Inherited&>(*this) = __in;
210 template<typename _U1, typename _U2>
212 operator=(const pair<_U1, _U2>& __in)
214 this->_M_head = __in.first;
215 this->_M_tail()._M_head = __in.second;
221 /// Gives the type of the ith element of a given tuple type.
222 template<int __i, typename _Tp>
223 struct tuple_element;
226 * Recursive case for tuple_element: strip off the first element in
227 * the tuple and retrieve the (i-1)th element of the remaining tuple.
229 template<int __i, typename _Head, typename... _Tail>
230 struct tuple_element<__i, tuple<_Head, _Tail...> >
231 : tuple_element<__i - 1, tuple<_Tail...> > { };
234 * Basis case for tuple_element: The first element is the one we're seeking.
236 template<typename _Head, typename... _Tail>
237 struct tuple_element<0, tuple<_Head, _Tail...> >
242 /// Finds the size of a given tuple type.
243 template<typename _Tp>
247 template<typename... _Elements>
248 struct tuple_size<tuple<_Elements...> >
250 static const int value = sizeof...(_Elements);
253 template<typename... _Elements>
254 const int tuple_size<tuple<_Elements...> >::value;
256 template<int __i, typename _Head, typename... _Tail>
257 inline typename __add_ref<_Head>::type
258 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
263 template<int __i, typename _Head, typename... _Tail>
264 inline typename __add_c_ref<_Head>::type
265 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
270 // Return a reference (const reference) to the ith element of a tuple.
271 // Any const or non-const ref elements are returned with their original type.
272 template<int __i, typename... _Elements>
273 inline typename __add_ref<
274 typename tuple_element<__i, tuple<_Elements...> >::type
276 get(tuple<_Elements...>& __t)
278 return __get_helper<__i>(__t);
281 template<int __i, typename... _Elements>
282 inline typename __add_c_ref<
283 typename tuple_element<__i, tuple<_Elements...> >::type
285 get(const tuple<_Elements...>& __t)
287 return __get_helper<__i>(__t);
290 // This class helps construct the various comparison operations on tuples
291 template<int __check_equal_size, int __i, int __j,
292 typename _Tp, typename _Up>
293 struct __tuple_compare;
295 template<int __i, int __j, typename _Tp, typename _Up>
296 struct __tuple_compare<0, __i, __j, _Tp, _Up>
298 static bool __eq(const _Tp& __t, const _Up& __u)
300 return (get<__i>(__t) == get<__i>(__u) &&
301 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
304 static bool __less(const _Tp& __t, const _Up& __u)
306 return ((get<__i>(__t) < get<__i>(__u))
307 || !(get<__i>(__u) < get<__i>(__t)) &&
308 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
312 template<int __i, typename _Tp, typename _Up>
313 struct __tuple_compare<0, __i, __i, _Tp, _Up>
315 static bool __eq(const _Tp&, const _Up&)
318 static bool __less(const _Tp&, const _Up&)
322 template<typename... _TElements, typename... _UElements>
324 operator==(const tuple<_TElements...>& __t,
325 const tuple<_UElements...>& __u)
327 typedef tuple<_TElements...> _Tp;
328 typedef tuple<_UElements...> _Up;
329 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
330 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
333 template<typename... _TElements, typename... _UElements>
335 operator<(const tuple<_TElements...>& __t,
336 const tuple<_UElements...>& __u)
338 typedef tuple<_TElements...> _Tp;
339 typedef tuple<_UElements...> _Up;
340 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
341 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
344 template<typename... _TElements, typename... _UElements>
346 operator!=(const tuple<_TElements...>& __t,
347 const tuple<_UElements...>& __u)
348 { return !(__t == __u); }
350 template<typename... _TElements, typename... _UElements>
352 operator>(const tuple<_TElements...>& __t,
353 const tuple<_UElements...>& __u)
354 { return __u < __t; }
356 template<typename... _TElements, typename... _UElements>
358 operator<=(const tuple<_TElements...>& __t,
359 const tuple<_UElements...>& __u)
360 { return !(__u < __t); }
362 template<typename... _TElements, typename... _UElements>
364 operator>=(const tuple<_TElements...>& __t,
365 const tuple<_UElements...>& __u)
366 { return !(__t < __u); }
368 template<typename _Tp>
369 class reference_wrapper;
371 // Helper which adds a reference to a type when given a reference_wrapper
372 template<typename _Tp>
373 struct __strip_reference_wrapper
378 template<typename _Tp>
379 struct __strip_reference_wrapper<reference_wrapper<_Tp> >
384 template<typename _Tp>
385 struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
390 template<typename... _Elements>
391 inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
392 make_tuple(_Elements... __args)
394 typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
396 return __result_type(__args...);
399 template<typename... _Elements>
400 inline tuple<_Elements&...>
401 tie(_Elements&... __args)
403 return tuple<_Elements&...>(__args...);
406 // A class (and instance) which can be used in 'tie' when an element
407 // of a tuple is not required
408 struct _Swallow_assign
412 operator=(const _Tp&)
416 // TODO: Put this in some kind of shared file.
419 _Swallow_assign ignore;
420 }; // anonymous namespace
423 _GLIBCXX_END_NAMESPACE_VERSION
426 #endif // _GLIBCXX_TR1_TUPLE