Fix handling of large arguments passed by value.
[official-gcc.git] / libstdc++-v3 / include / tr1 / tuple
blob95a545598e19c2ffc7a4b4651f748d098319abe5
1 // class template tuple -*- C++ -*-
3 // Copyright (C) 2004-2023 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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 tr1/tuple
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
39 #include <utility>
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 namespace tr1
47   // Adds a const reference to a non-reference type.
48   template<typename _Tp>
49     struct __add_c_ref
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>
58     struct __add_ref
59     { typedef _Tp& type; };
61   template<typename _Tp>
62     struct __add_ref<_Tp&>
63     { typedef _Tp& type; };
65   /**
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
71    * get() operation.
72    */
73   template<int _Idx, typename... _Elements>
74     struct _Tuple_impl; 
76   /**
77    * Zero-element tuple implementation. This is the basis case for the 
78    * inheritance recursion.
79    */
80   template<int _Idx>
81     struct _Tuple_impl<_Idx> { };
83   /**
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).
87    */
88   template<int _Idx, typename _Head, typename... _Tail>
89     struct _Tuple_impl<_Idx, _Head, _Tail...>
90     : public _Tuple_impl<_Idx + 1, _Tail...>
91     {
92       typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
93       
94       _Head _M_head;
95       
96       _Inherited&       _M_tail()       { return *this; }
97       const _Inherited& _M_tail() const { return *this; }
98       
99       _Tuple_impl() : _Inherited(), _M_head() { }
100       
101       explicit 
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) { }
112      
113       template<typename... _UElements>
114         _Tuple_impl&
115         operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
116         {
117           _M_head = __in._M_head;
118           _M_tail() = __in._M_tail();
119           return *this;
120         }
122       _Tuple_impl&
123       operator=(const _Tuple_impl& __in)
124       {
125         _M_head = __in._M_head;
126         _M_tail() = __in._M_tail();
127         return *this;
128       }
129     };
131   template<typename... _Elements> 
132     class tuple : public _Tuple_impl<0, _Elements...>
133     {
134       typedef _Tuple_impl<0, _Elements...> _Inherited;
136     public:
137       tuple() : _Inherited() { }
139       explicit
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>
151         tuple&
152         operator=(const tuple<_UElements...>& __in)
153         {
154           static_cast<_Inherited&>(*this) = __in;
155           return *this;
156         }
158       tuple&
159       operator=(const tuple& __in)
160       {
161         static_cast<_Inherited&>(*this) = __in;
162         return *this;
163       }
164     };
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>
171     {
172       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
174     public:
175       tuple() : _Inherited() { }
177       explicit
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, 
194                                                       __in.second))
195         { }
196   
197       template<typename _U1, typename _U2>
198         tuple&
199         operator=(const tuple<_U1, _U2>& __in)
200         {
201           static_cast<_Inherited&>(*this) = __in;
202           return *this;
203         }
205       tuple&
206       operator=(const tuple& __in)
207       {
208         static_cast<_Inherited&>(*this) = __in;
209         return *this;
210       }
212       template<typename _U1, typename _U2>
213         tuple&
214         operator=(const pair<_U1, _U2>& __in)
215         {
216           this->_M_head = __in.first;
217           this->_M_tail()._M_head = __in.second;
218           return *this;
219         }
220     };
222   
223   /// Gives the type of the ith element of a given tuple type.
224   template<int __i, typename _Tp>
225     struct tuple_element;
227   /**
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.
230    */
231   template<int __i, typename _Head, typename... _Tail>
232     struct tuple_element<__i, tuple<_Head, _Tail...> >
233     : tuple_element<__i - 1, tuple<_Tail...> > { };
235   /**
236    * Basis case for tuple_element: The first element is the one we're seeking.
237    */
238   template<typename _Head, typename... _Tail>
239     struct tuple_element<0, tuple<_Head, _Tail...> >
240     {
241       typedef _Head type;
242     };
244   /// Finds the size of a given tuple type.
245   template<typename _Tp>
246     struct tuple_size;
248   /// class tuple_size
249   template<typename... _Elements>
250     struct tuple_size<tuple<_Elements...> >
251     {
252       static const int value = sizeof...(_Elements);
253     };
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)
261     {
262       return __t._M_head;
263     }
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)
268     {
269       return __t._M_head;
270     }
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
277                     >::type
278     get(tuple<_Elements...>& __t)
279     { 
280       return __get_helper<__i>(__t); 
281     }
283   template<int __i, typename... _Elements>
284     inline typename __add_c_ref<
285                       typename tuple_element<__i, tuple<_Elements...> >::type
286                     >::type
287     get(const tuple<_Elements...>& __t)
288     {
289       return __get_helper<__i>(__t);
290     }
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>
299     {
300       static bool __eq(const _Tp& __t, const _Up& __u)
301       {
302         return (get<__i>(__t) == get<__i>(__u) &&
303                 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
304       }
305      
306       static bool __less(const _Tp& __t, const _Up& __u)
307       {
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));
311       }
312     };
314   template<int __i, typename _Tp, typename _Up>
315     struct __tuple_compare<0, __i, __i, _Tp, _Up>
316     {
317       static bool __eq(const _Tp&, const _Up&)
318       { return true; }
319      
320       static bool __less(const _Tp&, const _Up&)
321       { return false; }
322     };
324   template<typename... _TElements, typename... _UElements>
325     bool
326     operator==(const tuple<_TElements...>& __t,
327                const tuple<_UElements...>& __u)
328     {
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));
333     }
335   template<typename... _TElements, typename... _UElements>
336     bool
337     operator<(const tuple<_TElements...>& __t,
338               const tuple<_UElements...>& __u)
339     {
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));
344     }
346   template<typename... _TElements, typename... _UElements>
347     inline bool
348     operator!=(const tuple<_TElements...>& __t,
349                const tuple<_UElements...>& __u)
350     { return !(__t == __u); }
352   template<typename... _TElements, typename... _UElements>
353     inline bool
354     operator>(const tuple<_TElements...>& __t,
355               const tuple<_UElements...>& __u)
356     { return __u < __t; }
358   template<typename... _TElements, typename... _UElements>
359     inline bool
360     operator<=(const tuple<_TElements...>& __t,
361                const tuple<_UElements...>& __u)
362     { return !(__u < __t); }
364   template<typename... _TElements, typename... _UElements>
365     inline bool
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
376     {
377       typedef _Tp __type;
378     };
380   template<typename _Tp>
381     struct __strip_reference_wrapper<reference_wrapper<_Tp> >
382     {
383       typedef _Tp& __type;
384     };
386   template<typename _Tp>
387     struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
388     {
389       typedef _Tp& __type;
390     };
392   template<typename... _Elements>
393     inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
394     make_tuple(_Elements... __args)
395     {
396       typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
397         __result_type;
398       return __result_type(__args...);
399     }
401   template<typename... _Elements>
402     inline tuple<_Elements&...>
403     tie(_Elements&... __args)
404     {
405       return tuple<_Elements&...>(__args...);
406     }
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
411   {
412     template<class _Tp>
413       _Swallow_assign&
414       operator=(const _Tp&)
415       { return *this; }
416   };
418   // TODO: Put this in some kind of shared file.
419   namespace
420   {
421     _Swallow_assign ignore;
422   }; // anonymous namespace
425 _GLIBCXX_END_NAMESPACE_VERSION
428 #endif // _GLIBCXX_TR1_TUPLE