1 // class template tuple -*- C++ -*-
3 // Copyright (C) 2004-2023 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
37 #include <bits/requires_hosted.h> // TR1
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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; };
66 * Contains the actual implementation of the @c tuple template, stored
67 * as a recursive inheritance hierarchy from the first element (most
68 * derived class) to the last (least derived class). The @c Idx
69 * parameter gives the 0-based index of the element stored at this
70 * point in the hierarchy; we use it to implement a constant-time
73 template<int _Idx, typename... _Elements>
77 * Zero-element tuple implementation. This is the basis case for the
78 * inheritance recursion.
81 struct _Tuple_impl<_Idx> { };
84 * Recursive tuple implementation. Here we store the @c Head element
85 * and derive from a @c Tuple_impl containing the remaining elements
86 * (which contains the @c Tail).
88 template<int _Idx, typename _Head, typename... _Tail>
89 struct _Tuple_impl<_Idx, _Head, _Tail...>
90 : public _Tuple_impl<_Idx + 1, _Tail...>
92 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
96 _Inherited& _M_tail() { return *this; }
97 const _Inherited& _M_tail() const { return *this; }
99 _Tuple_impl() : _Inherited(), _M_head() { }
102 _Tuple_impl(typename __add_c_ref<_Head>::type __head,
103 typename __add_c_ref<_Tail>::type... __tail)
104 : _Inherited(__tail...), _M_head(__head) { }
106 template<typename... _UElements>
107 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
108 : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
110 _Tuple_impl(const _Tuple_impl& __in)
111 : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
113 template<typename... _UElements>
115 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
117 _M_head = __in._M_head;
118 _M_tail() = __in._M_tail();
123 operator=(const _Tuple_impl& __in)
125 _M_head = __in._M_head;
126 _M_tail() = __in._M_tail();
131 template<typename... _Elements>
132 class tuple : public _Tuple_impl<0, _Elements...>
134 typedef _Tuple_impl<0, _Elements...> _Inherited;
137 tuple() : _Inherited() { }
140 tuple(typename __add_c_ref<_Elements>::type... __elements)
141 : _Inherited(__elements...) { }
143 template<typename... _UElements>
144 tuple(const tuple<_UElements...>& __in)
145 : _Inherited(__in) { }
147 tuple(const tuple& __in)
148 : _Inherited(__in) { }
150 template<typename... _UElements>
152 operator=(const tuple<_UElements...>& __in)
154 static_cast<_Inherited&>(*this) = __in;
159 operator=(const tuple& __in)
161 static_cast<_Inherited&>(*this) = __in;
166 template<> class tuple<> { };
168 // 2-element tuple, with construction and assignment from a pair.
169 template<typename _T1, typename _T2>
170 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
172 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
175 tuple() : _Inherited() { }
178 tuple(typename __add_c_ref<_T1>::type __a1,
179 typename __add_c_ref<_T2>::type __a2)
180 : _Inherited(__a1, __a2) { }
182 template<typename _U1, typename _U2>
183 tuple(const tuple<_U1, _U2>& __in)
184 : _Inherited(__in) { }
186 tuple(const tuple& __in)
187 : _Inherited(__in) { }
189 template<typename _U1, typename _U2>
190 tuple(const pair<_U1, _U2>& __in)
191 : _Inherited(_Tuple_impl<0,
192 typename __add_c_ref<_U1>::type,
193 typename __add_c_ref<_U2>::type>(__in.first,
197 template<typename _U1, typename _U2>
199 operator=(const tuple<_U1, _U2>& __in)
201 static_cast<_Inherited&>(*this) = __in;
206 operator=(const tuple& __in)
208 static_cast<_Inherited&>(*this) = __in;
212 template<typename _U1, typename _U2>
214 operator=(const pair<_U1, _U2>& __in)
216 this->_M_head = __in.first;
217 this->_M_tail()._M_head = __in.second;
223 /// Gives the type of the ith element of a given tuple type.
224 template<int __i, typename _Tp>
225 struct tuple_element;
228 * Recursive case for tuple_element: strip off the first element in
229 * the tuple and retrieve the (i-1)th element of the remaining tuple.
231 template<int __i, typename _Head, typename... _Tail>
232 struct tuple_element<__i, tuple<_Head, _Tail...> >
233 : tuple_element<__i - 1, tuple<_Tail...> > { };
236 * Basis case for tuple_element: The first element is the one we're seeking.
238 template<typename _Head, typename... _Tail>
239 struct tuple_element<0, tuple<_Head, _Tail...> >
244 /// Finds the size of a given tuple type.
245 template<typename _Tp>
249 template<typename... _Elements>
250 struct tuple_size<tuple<_Elements...> >
252 static const int value = sizeof...(_Elements);
255 template<typename... _Elements>
256 const int tuple_size<tuple<_Elements...> >::value;
258 template<int __i, typename _Head, typename... _Tail>
259 inline typename __add_ref<_Head>::type
260 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
265 template<int __i, typename _Head, typename... _Tail>
266 inline typename __add_c_ref<_Head>::type
267 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
272 // Return a reference (const reference) to the ith element of a tuple.
273 // Any const or non-const ref elements are returned with their original type.
274 template<int __i, typename... _Elements>
275 inline typename __add_ref<
276 typename tuple_element<__i, tuple<_Elements...> >::type
278 get(tuple<_Elements...>& __t)
280 return __get_helper<__i>(__t);
283 template<int __i, typename... _Elements>
284 inline typename __add_c_ref<
285 typename tuple_element<__i, tuple<_Elements...> >::type
287 get(const tuple<_Elements...>& __t)
289 return __get_helper<__i>(__t);
292 // This class helps construct the various comparison operations on tuples
293 template<int __check_equal_size, int __i, int __j,
294 typename _Tp, typename _Up>
295 struct __tuple_compare;
297 template<int __i, int __j, typename _Tp, typename _Up>
298 struct __tuple_compare<0, __i, __j, _Tp, _Up>
300 static bool __eq(const _Tp& __t, const _Up& __u)
302 return (get<__i>(__t) == get<__i>(__u) &&
303 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
306 static bool __less(const _Tp& __t, const _Up& __u)
308 return ((get<__i>(__t) < get<__i>(__u))
309 || !(get<__i>(__u) < get<__i>(__t)) &&
310 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
314 template<int __i, typename _Tp, typename _Up>
315 struct __tuple_compare<0, __i, __i, _Tp, _Up>
317 static bool __eq(const _Tp&, const _Up&)
320 static bool __less(const _Tp&, const _Up&)
324 template<typename... _TElements, typename... _UElements>
326 operator==(const tuple<_TElements...>& __t,
327 const tuple<_UElements...>& __u)
329 typedef tuple<_TElements...> _Tp;
330 typedef tuple<_UElements...> _Up;
331 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
332 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
335 template<typename... _TElements, typename... _UElements>
337 operator<(const tuple<_TElements...>& __t,
338 const tuple<_UElements...>& __u)
340 typedef tuple<_TElements...> _Tp;
341 typedef tuple<_UElements...> _Up;
342 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
343 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
346 template<typename... _TElements, typename... _UElements>
348 operator!=(const tuple<_TElements...>& __t,
349 const tuple<_UElements...>& __u)
350 { return !(__t == __u); }
352 template<typename... _TElements, typename... _UElements>
354 operator>(const tuple<_TElements...>& __t,
355 const tuple<_UElements...>& __u)
356 { return __u < __t; }
358 template<typename... _TElements, typename... _UElements>
360 operator<=(const tuple<_TElements...>& __t,
361 const tuple<_UElements...>& __u)
362 { return !(__u < __t); }
364 template<typename... _TElements, typename... _UElements>
366 operator>=(const tuple<_TElements...>& __t,
367 const tuple<_UElements...>& __u)
368 { return !(__t < __u); }
370 template<typename _Tp>
371 class reference_wrapper;
373 // Helper which adds a reference to a type when given a reference_wrapper
374 template<typename _Tp>
375 struct __strip_reference_wrapper
380 template<typename _Tp>
381 struct __strip_reference_wrapper<reference_wrapper<_Tp> >
386 template<typename _Tp>
387 struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
392 template<typename... _Elements>
393 inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
394 make_tuple(_Elements... __args)
396 typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
398 return __result_type(__args...);
401 template<typename... _Elements>
402 inline tuple<_Elements&...>
403 tie(_Elements&... __args)
405 return tuple<_Elements&...>(__args...);
408 // A class (and instance) which can be used in 'tie' when an element
409 // of a tuple is not required
410 struct _Swallow_assign
414 operator=(const _Tp&)
418 // TODO: Put this in some kind of shared file.
421 _Swallow_assign ignore;
422 }; // anonymous namespace
425 _GLIBCXX_END_NAMESPACE_VERSION
428 #endif // _GLIBCXX_TR1_TUPLE