2014-10-01 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libstdc++-v3 / include / std / tuple
blob6be7f234b92c7baffd30e24fd976b636c820794a
1 // <tuple> -*- C++ -*-
3 // Copyright (C) 2007-2014 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 include/tuple
26  *  This is a Standard C++ Library header.
27  */
29 #ifndef _GLIBCXX_TUPLE
30 #define _GLIBCXX_TUPLE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
36 #else
38 #include <utility>
39 #include <array>
40 #include <bits/uses_allocator.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46   /**
47    *  @addtogroup utilities
48    *  @{
49    */
51   template<std::size_t _Idx, typename _Head, bool _IsEmptyNotFinal>
52     struct _Head_base;
54   template<std::size_t _Idx, typename _Head>
55     struct _Head_base<_Idx, _Head, true>
56     : public _Head
57     {
58       constexpr _Head_base()
59       : _Head() { }
61       constexpr _Head_base(const _Head& __h)
62       : _Head(__h) { }
64       constexpr _Head_base(const _Head_base&) = default;
65       constexpr _Head_base(_Head_base&&) = default;
67       template<typename _UHead>
68         constexpr _Head_base(_UHead&& __h)
69         : _Head(std::forward<_UHead>(__h)) { }
71       _Head_base(allocator_arg_t, __uses_alloc0)
72       : _Head() { }
74       template<typename _Alloc>
75         _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
76         : _Head(allocator_arg, *__a._M_a) { }
78       template<typename _Alloc>
79         _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
80         : _Head(*__a._M_a) { }
82       template<typename _UHead>
83         _Head_base(__uses_alloc0, _UHead&& __uhead)
84         : _Head(std::forward<_UHead>(__uhead)) { }
86       template<typename _Alloc, typename _UHead>
87         _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
88         : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
90       template<typename _Alloc, typename _UHead>
91         _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
92         : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
94       static constexpr _Head&
95       _M_head(_Head_base& __b) noexcept { return __b; }
97       static constexpr const _Head&
98       _M_head(const _Head_base& __b) noexcept { return __b; }
99     };
101   template<std::size_t _Idx, typename _Head>
102     struct _Head_base<_Idx, _Head, false>
103     {
104       constexpr _Head_base()
105       : _M_head_impl() { }
107       constexpr _Head_base(const _Head& __h)
108       : _M_head_impl(__h) { }
110       constexpr _Head_base(const _Head_base&) = default;
111       constexpr _Head_base(_Head_base&&) = default;
113       template<typename _UHead>
114         constexpr _Head_base(_UHead&& __h)
115         : _M_head_impl(std::forward<_UHead>(__h)) { }
117       _Head_base(allocator_arg_t, __uses_alloc0)
118       : _M_head_impl() { }
120       template<typename _Alloc>
121         _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
122         : _M_head_impl(allocator_arg, *__a._M_a) { }
124       template<typename _Alloc>
125         _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
126         : _M_head_impl(*__a._M_a) { }
128       template<typename _UHead>
129         _Head_base(__uses_alloc0, _UHead&& __uhead)
130         : _M_head_impl(std::forward<_UHead>(__uhead)) { }
132       template<typename _Alloc, typename _UHead>
133         _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
134         : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
135         { }
137       template<typename _Alloc, typename _UHead>
138         _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
139         : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
141       static constexpr _Head&
142       _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
144       static constexpr const _Head&
145       _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
147       _Head _M_head_impl;
148     };
150   /**
151    * Contains the actual implementation of the @c tuple template, stored
152    * as a recursive inheritance hierarchy from the first element (most
153    * derived class) to the last (least derived class). The @c Idx
154    * parameter gives the 0-based index of the element stored at this
155    * point in the hierarchy; we use it to implement a constant-time
156    * get() operation.
157    */
158   template<std::size_t _Idx, typename... _Elements>
159     struct _Tuple_impl; 
161   /**
162    * Zero-element tuple implementation. This is the basis case for the 
163    * inheritance recursion.
164    */
165   template<std::size_t _Idx>
166     struct _Tuple_impl<_Idx>
167     {
168       template<std::size_t, typename...> friend class _Tuple_impl;
170       _Tuple_impl() = default;
172       template<typename _Alloc>
173         _Tuple_impl(allocator_arg_t, const _Alloc&) { }
175       template<typename _Alloc>
176         _Tuple_impl(allocator_arg_t, const _Alloc&, const _Tuple_impl&) { }
178       template<typename _Alloc>
179         _Tuple_impl(allocator_arg_t, const _Alloc&, _Tuple_impl&&) { }
181     protected:
182       void _M_swap(_Tuple_impl&) noexcept { /* no-op */ }
183     };
185   template<typename _Tp>
186     struct __is_empty_non_tuple : is_empty<_Tp> { };
188   // Using EBO for elements that are tuples causes ambiguous base errors.
189   template<typename _El0, typename... _El>
190     struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
192   // Use the Empty Base-class Optimization for empty, non-final types.
193   template<typename _Tp>
194     using __empty_not_final
195     = typename conditional<__is_final(_Tp), false_type,
196                            __is_empty_non_tuple<_Tp>>::type;
198   /**
199    * Recursive tuple implementation. Here we store the @c Head element
200    * and derive from a @c Tuple_impl containing the remaining elements
201    * (which contains the @c Tail).
202    */
203   template<std::size_t _Idx, typename _Head, typename... _Tail>
204     struct _Tuple_impl<_Idx, _Head, _Tail...>
205     : public _Tuple_impl<_Idx + 1, _Tail...>,
206       private _Head_base<_Idx, _Head, __empty_not_final<_Head>::value>
207     {
208       template<std::size_t, typename...> friend class _Tuple_impl;
210       typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
211       typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
213       static constexpr _Head&  
214       _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
216       static constexpr const _Head&
217       _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
219       static constexpr _Inherited&
220       _M_tail(_Tuple_impl& __t) noexcept { return __t; }
222       static constexpr const _Inherited&
223       _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
225       constexpr _Tuple_impl()
226       : _Inherited(), _Base() { }
228       explicit 
229       constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
230       : _Inherited(__tail...), _Base(__head) { }
232       template<typename _UHead, typename... _UTail, typename = typename
233                enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type> 
234         explicit
235         constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
236         : _Inherited(std::forward<_UTail>(__tail)...),
237           _Base(std::forward<_UHead>(__head)) { }
239       constexpr _Tuple_impl(const _Tuple_impl&) = default;
241       constexpr
242       _Tuple_impl(_Tuple_impl&& __in)
243       noexcept(__and_<is_nothrow_move_constructible<_Head>,
244                       is_nothrow_move_constructible<_Inherited>>::value)
245       : _Inherited(std::move(_M_tail(__in))), 
246         _Base(std::forward<_Head>(_M_head(__in))) { }
248       template<typename... _UElements>
249         constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
250         : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
251           _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
253       template<typename _UHead, typename... _UTails>
254         constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
255         : _Inherited(std::move
256                      (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
257           _Base(std::forward<_UHead>
258                 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
260       template<typename _Alloc>
261         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
262         : _Inherited(__tag, __a),
263           _Base(__tag, __use_alloc<_Head>(__a)) { }
265       template<typename _Alloc>
266         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
267                     const _Head& __head, const _Tail&... __tail)
268         : _Inherited(__tag, __a, __tail...),
269           _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
271       template<typename _Alloc, typename _UHead, typename... _UTail,
272                typename = typename enable_if<sizeof...(_Tail)
273                                              == sizeof...(_UTail)>::type>
274         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
275                     _UHead&& __head, _UTail&&... __tail)
276         : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
277           _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
278                 std::forward<_UHead>(__head)) { }
280       template<typename _Alloc>
281         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
282                     const _Tuple_impl& __in)
283         : _Inherited(__tag, __a, _M_tail(__in)), 
284           _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
286       template<typename _Alloc>
287         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
288                     _Tuple_impl&& __in)
289         : _Inherited(__tag, __a, std::move(_M_tail(__in))), 
290           _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
291                 std::forward<_Head>(_M_head(__in))) { }
293       template<typename _Alloc, typename... _UElements>
294         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
295                     const _Tuple_impl<_Idx, _UElements...>& __in)
296         : _Inherited(__tag, __a,
297                      _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
298           _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
299                 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
301       template<typename _Alloc, typename _UHead, typename... _UTails>
302         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
303                     _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
304         : _Inherited(__tag, __a, std::move
305                      (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
306           _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
307                 std::forward<_UHead>
308                 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
310       _Tuple_impl&
311       operator=(const _Tuple_impl& __in)
312       {
313         _M_head(*this) = _M_head(__in);
314         _M_tail(*this) = _M_tail(__in);
315         return *this;
316       }
318       _Tuple_impl&
319       operator=(_Tuple_impl&& __in)
320       noexcept(__and_<is_nothrow_move_assignable<_Head>,
321                       is_nothrow_move_assignable<_Inherited>>::value)
322       {
323         _M_head(*this) = std::forward<_Head>(_M_head(__in));
324         _M_tail(*this) = std::move(_M_tail(__in));
325         return *this;
326       }
328       template<typename... _UElements>
329         _Tuple_impl&
330         operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
331         {
332           _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
333           _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
334           return *this;
335         }
337       template<typename _UHead, typename... _UTails>
338         _Tuple_impl&
339         operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
340         {
341           _M_head(*this) = std::forward<_UHead>
342             (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
343           _M_tail(*this) = std::move
344             (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
345           return *this;
346         }
348     protected:
349       void
350       _M_swap(_Tuple_impl& __in)
351       noexcept(noexcept(swap(std::declval<_Head&>(),
352                              std::declval<_Head&>()))
353                && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
354       {
355         using std::swap;
356         swap(_M_head(*this), _M_head(__in));
357         _Inherited::_M_swap(_M_tail(__in));
358       }
359     };
361   /// Primary class template, tuple
362   template<typename... _Elements> 
363     class tuple : public _Tuple_impl<0, _Elements...>
364     {
365       typedef _Tuple_impl<0, _Elements...> _Inherited;
367     public:
368       constexpr tuple()
369       : _Inherited() { }
371       explicit
372       constexpr tuple(const _Elements&... __elements)
373       : _Inherited(__elements...) { }
375       template<typename... _UElements, typename = typename
376         enable_if<__and_<is_convertible<_UElements,
377                                         _Elements>...>::value>::type>
378         explicit
379         constexpr tuple(_UElements&&... __elements)
380         : _Inherited(std::forward<_UElements>(__elements)...) { }
382       constexpr tuple(const tuple&) = default;
384       constexpr tuple(tuple&&) = default; 
386       template<typename... _UElements, typename = typename
387         enable_if<__and_<is_convertible<const _UElements&,
388                                         _Elements>...>::value>::type>
389         constexpr tuple(const tuple<_UElements...>& __in)
390         : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
391         { }
393       template<typename... _UElements, typename = typename
394         enable_if<__and_<is_convertible<_UElements,
395                                         _Elements>...>::value>::type>
396         constexpr tuple(tuple<_UElements...>&& __in)
397         : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
399       // Allocator-extended constructors.
401       template<typename _Alloc>
402         tuple(allocator_arg_t __tag, const _Alloc& __a)
403         : _Inherited(__tag, __a) { }
405       template<typename _Alloc>
406         tuple(allocator_arg_t __tag, const _Alloc& __a,
407               const _Elements&... __elements)
408         : _Inherited(__tag, __a, __elements...) { }
410       template<typename _Alloc, typename... _UElements, typename = typename
411                enable_if<sizeof...(_UElements)
412                          == sizeof...(_Elements)>::type>
413         tuple(allocator_arg_t __tag, const _Alloc& __a,
414               _UElements&&... __elements)
415         : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
416         { }
418       template<typename _Alloc>
419         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
420         : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
422       template<typename _Alloc>
423         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
424         : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
426       template<typename _Alloc, typename... _UElements, typename = typename
427                enable_if<sizeof...(_UElements)
428                          == sizeof...(_Elements)>::type>
429         tuple(allocator_arg_t __tag, const _Alloc& __a,
430               const tuple<_UElements...>& __in)
431         : _Inherited(__tag, __a,
432                      static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
433         { }
435       template<typename _Alloc, typename... _UElements, typename = typename
436                enable_if<sizeof...(_UElements)
437                          == sizeof...(_Elements)>::type>
438         tuple(allocator_arg_t __tag, const _Alloc& __a,
439               tuple<_UElements...>&& __in)
440         : _Inherited(__tag, __a,
441                      static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
442         { }
444       tuple&
445       operator=(const tuple& __in)
446       {
447         static_cast<_Inherited&>(*this) = __in;
448         return *this;
449       }
451       tuple&
452       operator=(tuple&& __in)
453       noexcept(is_nothrow_move_assignable<_Inherited>::value)
454       {
455         static_cast<_Inherited&>(*this) = std::move(__in);
456         return *this;
457       }
459       template<typename... _UElements, typename = typename
460                enable_if<sizeof...(_UElements)
461                          == sizeof...(_Elements)>::type>
462         tuple&
463         operator=(const tuple<_UElements...>& __in)
464         {
465           static_cast<_Inherited&>(*this) = __in;
466           return *this;
467         }
469       template<typename... _UElements, typename = typename
470                enable_if<sizeof...(_UElements)
471                          == sizeof...(_Elements)>::type>
472         tuple&
473         operator=(tuple<_UElements...>&& __in)
474         {
475           static_cast<_Inherited&>(*this) = std::move(__in);
476           return *this;
477         }
479       void
480       swap(tuple& __in)
481       noexcept(noexcept(__in._M_swap(__in)))
482       { _Inherited::_M_swap(__in); }
483     };
485   // Explicit specialization, zero-element tuple.
486   template<>  
487     class tuple<>
488     {
489     public:
490       void swap(tuple&) noexcept { /* no-op */ }
491     };
493   /// Partial specialization, 2-element tuple.
494   /// Includes construction and assignment from a pair.
495   template<typename _T1, typename _T2>
496     class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
497     {
498       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
500     public:
501       constexpr tuple()
502       : _Inherited() { }
504       explicit
505       constexpr tuple(const _T1& __a1, const _T2& __a2)
506       : _Inherited(__a1, __a2) { }
508       template<typename _U1, typename _U2, typename = typename
509                enable_if<__and_<is_convertible<_U1, _T1>,
510                                 is_convertible<_U2, _T2>>::value>::type>
511         explicit
512         constexpr tuple(_U1&& __a1, _U2&& __a2)
513         : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
515       constexpr tuple(const tuple&) = default;
517       constexpr tuple(tuple&&) = default;
519       template<typename _U1, typename _U2, typename = typename
520         enable_if<__and_<is_convertible<const _U1&, _T1>,
521                          is_convertible<const _U2&, _T2>>::value>::type>
522         constexpr tuple(const tuple<_U1, _U2>& __in)
523         : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
525       template<typename _U1, typename _U2, typename = typename
526                enable_if<__and_<is_convertible<_U1, _T1>,
527                                 is_convertible<_U2, _T2>>::value>::type>
528         constexpr tuple(tuple<_U1, _U2>&& __in)
529         : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
531       template<typename _U1, typename _U2, typename = typename
532         enable_if<__and_<is_convertible<const _U1&, _T1>,
533                          is_convertible<const _U2&, _T2>>::value>::type>
534         constexpr tuple(const pair<_U1, _U2>& __in)
535         : _Inherited(__in.first, __in.second) { }
537       template<typename _U1, typename _U2, typename = typename
538                enable_if<__and_<is_convertible<_U1, _T1>,
539                                 is_convertible<_U2, _T2>>::value>::type>
540         constexpr tuple(pair<_U1, _U2>&& __in)
541         : _Inherited(std::forward<_U1>(__in.first),
542                      std::forward<_U2>(__in.second)) { }
544       // Allocator-extended constructors.
546       template<typename _Alloc>
547         tuple(allocator_arg_t __tag, const _Alloc& __a)
548         : _Inherited(__tag, __a) { }
550       template<typename _Alloc>
551         tuple(allocator_arg_t __tag, const _Alloc& __a,
552               const _T1& __a1, const _T2& __a2)
553         : _Inherited(__tag, __a, __a1, __a2) { }
555       template<typename _Alloc, typename _U1, typename _U2>
556         tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
557         : _Inherited(__tag, __a, std::forward<_U1>(__a1),
558                      std::forward<_U2>(__a2)) { }
560       template<typename _Alloc>
561         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
562         : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
564       template<typename _Alloc>
565         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
566         : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
568       template<typename _Alloc, typename _U1, typename _U2>
569         tuple(allocator_arg_t __tag, const _Alloc& __a,
570               const tuple<_U1, _U2>& __in)
571         : _Inherited(__tag, __a,
572                      static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
573         { }
575       template<typename _Alloc, typename _U1, typename _U2>
576         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
577         : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
578         { }
580       template<typename _Alloc, typename _U1, typename _U2>
581         tuple(allocator_arg_t __tag, const _Alloc& __a,
582               const pair<_U1, _U2>& __in)
583         : _Inherited(__tag, __a, __in.first, __in.second) { }
585       template<typename _Alloc, typename _U1, typename _U2>
586         tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
587         : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
588                      std::forward<_U2>(__in.second)) { }
590       tuple&
591       operator=(const tuple& __in)
592       {
593         static_cast<_Inherited&>(*this) = __in;
594         return *this;
595       }
597       tuple&
598       operator=(tuple&& __in)
599       noexcept(is_nothrow_move_assignable<_Inherited>::value)
600       {
601         static_cast<_Inherited&>(*this) = std::move(__in);
602         return *this;
603       }
605       template<typename _U1, typename _U2>
606         tuple&
607         operator=(const tuple<_U1, _U2>& __in)
608         {
609           static_cast<_Inherited&>(*this) = __in;
610           return *this;
611         }
613       template<typename _U1, typename _U2>
614         tuple&
615         operator=(tuple<_U1, _U2>&& __in)
616         {
617           static_cast<_Inherited&>(*this) = std::move(__in);
618           return *this;
619         }
621       template<typename _U1, typename _U2>
622         tuple&
623         operator=(const pair<_U1, _U2>& __in)
624         {
625           this->_M_head(*this) = __in.first;
626           this->_M_tail(*this)._M_head(*this) = __in.second;
627           return *this;
628         }
630       template<typename _U1, typename _U2>
631         tuple&
632         operator=(pair<_U1, _U2>&& __in)
633         {
634           this->_M_head(*this) = std::forward<_U1>(__in.first);
635           this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
636           return *this;
637         }
639       void
640       swap(tuple& __in)
641       noexcept(noexcept(__in._M_swap(__in)))
642       { _Inherited::_M_swap(__in); }
643     };
646   /// Gives the type of the ith element of a given tuple type.
647   template<std::size_t __i, typename _Tp>
648     struct tuple_element;
650   /**
651    * Recursive case for tuple_element: strip off the first element in
652    * the tuple and retrieve the (i-1)th element of the remaining tuple.
653    */
654   template<std::size_t __i, typename _Head, typename... _Tail>
655     struct tuple_element<__i, tuple<_Head, _Tail...> >
656     : tuple_element<__i - 1, tuple<_Tail...> > { };
658   /**
659    * Basis case for tuple_element: The first element is the one we're seeking.
660    */
661   template<typename _Head, typename... _Tail>
662     struct tuple_element<0, tuple<_Head, _Tail...> >
663     {
664       typedef _Head type;
665     };
667   // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
668   template<std::size_t __i, typename _Tp>
669     using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
671   template<std::size_t __i, typename _Tp>
672     struct tuple_element<__i, const _Tp>
673     {
674       typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
675     };
677   template<std::size_t __i, typename _Tp>
678     struct tuple_element<__i, volatile _Tp>
679     {
680       typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
681     };
683   template<std::size_t __i, typename _Tp>
684     struct tuple_element<__i, const volatile _Tp>
685     {
686       typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
687     };
689 #if __cplusplus > 201103L
690   template<std::size_t __i, typename _Tp>
691     using tuple_element_t = typename tuple_element<__i, _Tp>::type;
692 #endif
694   /// Finds the size of a given tuple type.
695   template<typename _Tp>
696     struct tuple_size;
698   // _GLIBCXX_RESOLVE_LIB_DEFECTS
699   // 2313. tuple_size should always derive from integral_constant<size_t, N>
700   template<typename _Tp>
701     struct tuple_size<const _Tp>
702     : integral_constant<size_t, tuple_size<_Tp>::value> { };
704   template<typename _Tp>
705     struct tuple_size<volatile _Tp>
706     : integral_constant<size_t, tuple_size<_Tp>::value> { };
708   template<typename _Tp>
709     struct tuple_size<const volatile _Tp>
710     : integral_constant<size_t, tuple_size<_Tp>::value> { };
712   /// class tuple_size
713   template<typename... _Elements>
714     struct tuple_size<tuple<_Elements...>>
715     : public integral_constant<std::size_t, sizeof...(_Elements)> { };
717   template<std::size_t __i, typename _Head, typename... _Tail>
718     constexpr _Head&
719     __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
720     { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
722   template<std::size_t __i, typename _Head, typename... _Tail>
723     constexpr const _Head&
724     __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
725     { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
727   /// Return a reference to the ith element of a tuple.
728   template<std::size_t __i, typename... _Elements>
729     constexpr __tuple_element_t<__i, tuple<_Elements...>>&
730     get(tuple<_Elements...>& __t) noexcept
731     { return std::__get_helper<__i>(__t); }
733   /// Return a const reference to the ith element of a const tuple.
734   template<std::size_t __i, typename... _Elements>
735     constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
736     get(const tuple<_Elements...>& __t) noexcept
737     { return std::__get_helper<__i>(__t); }
739   /// Return an rvalue reference to the ith element of a tuple rvalue.
740   template<std::size_t __i, typename... _Elements>
741     constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
742     get(tuple<_Elements...>&& __t) noexcept
743     {
744       typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
745       return std::forward<__element_type&&>(std::get<__i>(__t));
746     }
748 #if __cplusplus > 201103L
750 #define __cpp_lib_tuples_by_type 201304
752   template<typename _Head, size_t __i, typename... _Tail>
753     constexpr _Head&
754     __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
755     { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
757   template<typename _Head, size_t __i, typename... _Tail>
758     constexpr const _Head&
759     __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
760     { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
762   /// Return a reference to the unique element of type _Tp of a tuple.
763   template <typename _Tp, typename... _Types>
764     constexpr _Tp&
765     get(tuple<_Types...>& __t) noexcept
766     { return std::__get_helper2<_Tp>(__t); }
768   /// Return a reference to the unique element of type _Tp of a tuple rvalue.
769   template <typename _Tp, typename... _Types>
770     constexpr _Tp&&
771     get(tuple<_Types...>&& __t) noexcept
772     { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
774   /// Return a const reference to the unique element of type _Tp of a tuple.
775   template <typename _Tp, typename... _Types>
776     constexpr const _Tp&
777     get(const tuple<_Types...>& __t) noexcept
778     { return std::__get_helper2<_Tp>(__t); }
779 #endif
781   // This class performs the comparison operations on tuples
782   template<typename _Tp, typename _Up, size_t __i, size_t __size>
783     struct __tuple_compare
784     {
785       static constexpr bool
786       __eq(const _Tp& __t, const _Up& __u)
787       {
788         return bool(std::get<__i>(__t) == std::get<__i>(__u))
789           && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
790       }
791    
792       static constexpr bool
793       __less(const _Tp& __t, const _Up& __u)
794       {
795         return bool(std::get<__i>(__t) < std::get<__i>(__u))
796           || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
797               && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
798       }
799     };
801   template<typename _Tp, typename _Up, size_t __size>
802     struct __tuple_compare<_Tp, _Up, __size, __size>
803     {
804       static constexpr bool
805       __eq(const _Tp&, const _Up&) { return true; }
806    
807       static constexpr bool
808       __less(const _Tp&, const _Up&) { return false; }
809     };
811   template<typename... _TElements, typename... _UElements>
812     constexpr bool
813     operator==(const tuple<_TElements...>& __t,
814                const tuple<_UElements...>& __u)
815     {
816       static_assert(sizeof...(_TElements) == sizeof...(_UElements),
817           "tuple objects can only be compared if they have equal sizes.");
818       using __compare = __tuple_compare<tuple<_TElements...>,
819                                         tuple<_UElements...>,
820                                         0, sizeof...(_TElements)>;
821       return __compare::__eq(__t, __u);
822     }
824   template<typename... _TElements, typename... _UElements>
825     constexpr bool
826     operator<(const tuple<_TElements...>& __t,
827               const tuple<_UElements...>& __u)
828     {
829       static_assert(sizeof...(_TElements) == sizeof...(_UElements),
830           "tuple objects can only be compared if they have equal sizes.");
831       using __compare = __tuple_compare<tuple<_TElements...>,
832                                         tuple<_UElements...>,
833                                         0, sizeof...(_TElements)>;
834       return __compare::__less(__t, __u);
835     }
837   template<typename... _TElements, typename... _UElements>
838     constexpr bool
839     operator!=(const tuple<_TElements...>& __t,
840                const tuple<_UElements...>& __u)
841     { return !(__t == __u); }
843   template<typename... _TElements, typename... _UElements>
844     constexpr bool
845     operator>(const tuple<_TElements...>& __t,
846               const tuple<_UElements...>& __u)
847     { return __u < __t; }
849   template<typename... _TElements, typename... _UElements>
850     constexpr bool
851     operator<=(const tuple<_TElements...>& __t,
852                const tuple<_UElements...>& __u)
853     { return !(__u < __t); }
855   template<typename... _TElements, typename... _UElements>
856     constexpr bool
857     operator>=(const tuple<_TElements...>& __t,
858                const tuple<_UElements...>& __u)
859     { return !(__t < __u); }
861   // NB: DR 705.
862   template<typename... _Elements>
863     constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
864     make_tuple(_Elements&&... __args)
865     {
866       typedef tuple<typename __decay_and_strip<_Elements>::__type...>
867         __result_type;
868       return __result_type(std::forward<_Elements>(__args)...);
869     }
871   template<typename... _Elements>
872     tuple<_Elements&&...>
873     forward_as_tuple(_Elements&&... __args) noexcept
874     { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
876   template<typename>
877     struct __is_tuple_like_impl : false_type
878     { };
880   template<typename... _Tps>
881     struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
882     { };
884   template<typename _T1, typename _T2>
885     struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
886     { };
888   template<typename _Tp, std::size_t _Nm>
889     struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
890     { };
892   // Internal type trait that allows us to sfinae-protect tuple_cat.
893   template<typename _Tp>
894     struct __is_tuple_like
895     : public __is_tuple_like_impl<typename std::remove_cv
896             <typename std::remove_reference<_Tp>::type>::type>::type
897     { };
899   template<size_t, typename, typename, size_t>
900     struct __make_tuple_impl;
902   template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
903     struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
904     : __make_tuple_impl<_Idx + 1,
905                         tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
906                         _Tuple, _Nm>
907     { };
909   template<std::size_t _Nm, typename _Tuple, typename... _Tp>
910     struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
911     {
912       typedef tuple<_Tp...> __type;
913     };
915   template<typename _Tuple>
916     struct __do_make_tuple
917     : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
918     { };
920   // Returns the std::tuple equivalent of a tuple-like type.
921   template<typename _Tuple>
922     struct __make_tuple
923     : public __do_make_tuple<typename std::remove_cv
924             <typename std::remove_reference<_Tuple>::type>::type>
925     { };
927   // Combines several std::tuple's into a single one.
928   template<typename...>
929     struct __combine_tuples;
931   template<>
932     struct __combine_tuples<>
933     {
934       typedef tuple<> __type;
935     };
937   template<typename... _Ts>
938     struct __combine_tuples<tuple<_Ts...>>
939     {
940       typedef tuple<_Ts...> __type;
941     };
943   template<typename... _T1s, typename... _T2s, typename... _Rem>
944     struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
945     {
946       typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
947                                         _Rem...>::__type __type;
948     };
950   // Computes the result type of tuple_cat given a set of tuple-like types.
951   template<typename... _Tpls>
952     struct __tuple_cat_result
953     {
954       typedef typename __combine_tuples
955         <typename __make_tuple<_Tpls>::__type...>::__type __type;
956     };
958   // Helper to determine the index set for the first tuple-like
959   // type of a given set.
960   template<typename...>
961     struct __make_1st_indices;
963   template<>
964     struct __make_1st_indices<>
965     {
966       typedef std::_Index_tuple<> __type;
967     };
969   template<typename _Tp, typename... _Tpls>
970     struct __make_1st_indices<_Tp, _Tpls...>
971     {
972       typedef typename std::_Build_index_tuple<std::tuple_size<
973         typename std::remove_reference<_Tp>::type>::value>::__type __type;
974     };
976   // Performs the actual concatenation by step-wise expanding tuple-like
977   // objects into the elements,  which are finally forwarded into the
978   // result tuple.
979   template<typename _Ret, typename _Indices, typename... _Tpls>
980     struct __tuple_concater;
982   template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
983     struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
984     {
985       template<typename... _Us>
986         static constexpr _Ret
987         _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
988         {
989           typedef typename __make_1st_indices<_Tpls...>::__type __idx;
990           typedef __tuple_concater<_Ret, __idx, _Tpls...>      __next;
991           return __next::_S_do(std::forward<_Tpls>(__tps)...,
992                                std::forward<_Us>(__us)...,
993                                std::get<_Is>(std::forward<_Tp>(__tp))...);
994         }
995     };
997   template<typename _Ret>
998     struct __tuple_concater<_Ret, std::_Index_tuple<>>
999     {
1000       template<typename... _Us>
1001         static constexpr _Ret
1002         _S_do(_Us&&... __us)
1003         {
1004           return _Ret(std::forward<_Us>(__us)...);
1005         }
1006     };
1008   /// tuple_cat
1009   template<typename... _Tpls, typename = typename
1010            enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1011     constexpr auto
1012     tuple_cat(_Tpls&&... __tpls)
1013     -> typename __tuple_cat_result<_Tpls...>::__type
1014     {
1015       typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1016       typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1017       typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1018       return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1019     }
1021   /// tie
1022   template<typename... _Elements>
1023     inline tuple<_Elements&...>
1024     tie(_Elements&... __args) noexcept
1025     { return tuple<_Elements&...>(__args...); }
1027   /// swap
1028   template<typename... _Elements>
1029     inline void 
1030     swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1031     noexcept(noexcept(__x.swap(__y)))
1032     { __x.swap(__y); }
1034   // A class (and instance) which can be used in 'tie' when an element
1035   // of a tuple is not required
1036   struct _Swallow_assign
1037   {
1038     template<class _Tp>
1039       const _Swallow_assign&
1040       operator=(const _Tp&) const
1041       { return *this; }
1042   };
1044   const _Swallow_assign ignore{};
1046   /// Partial specialization for tuples
1047   template<typename... _Types, typename _Alloc>
1048     struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1050   // See stl_pair.h...
1051   template<class _T1, class _T2>
1052     template<typename... _Args1, typename... _Args2>
1053       inline
1054       pair<_T1, _T2>::
1055       pair(piecewise_construct_t,
1056            tuple<_Args1...> __first, tuple<_Args2...> __second)
1057       : pair(__first, __second,
1058              typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1059              typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1060       { }
1062   template<class _T1, class _T2>
1063     template<typename... _Args1, std::size_t... _Indexes1,
1064              typename... _Args2, std::size_t... _Indexes2>
1065       inline
1066       pair<_T1, _T2>::
1067       pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1068            _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1069       : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1070         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1071       { }
1073   /// @}
1075 _GLIBCXX_END_NAMESPACE_VERSION
1076 } // namespace std
1078 #endif // C++11
1080 #endif // _GLIBCXX_TUPLE