OSDN Git Service

1b4a82382769787a09cd084b32028bea71c5bde2
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / tuple
1 // <tuple> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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.
10
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.
15
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.
19
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/>.
24
25 /** @file include/tuple
26  *  This is a Standard C++ Library header.
27  */
28
29 #ifndef _GLIBCXX_TUPLE
30 #define _GLIBCXX_TUPLE 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <utility>
39 #include <bits/uses_allocator.h>
40
41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44
45   // Adds a const reference to a non-reference type.
46   template<typename _Tp>
47     struct __add_c_ref
48     { typedef const _Tp& type; };
49
50   template<typename _Tp>
51     struct __add_c_ref<_Tp&>
52     { typedef _Tp& type; };
53
54   // Adds a reference to a non-reference type.
55   template<typename _Tp>
56     struct __add_ref
57     { typedef _Tp& type; };
58
59   template<typename _Tp>
60     struct __add_ref<_Tp&>
61     { typedef _Tp& type; };
62
63   // Adds an rvalue reference to a non-reference type.
64   template<typename _Tp>
65     struct __add_r_ref
66     { typedef _Tp&& type; };
67
68   template<typename _Tp>
69     struct __add_r_ref<_Tp&>
70     { typedef _Tp& type; };
71
72   // To work around c++/49225 aka c++/48322.
73   template<typename...>
74     struct __conv_types { };
75
76   template<typename _Tuple1, typename _Tuple2>
77     struct __one_by_one_convertible
78     : public false_type { };
79
80   template<typename _Tp, typename _Up>
81     struct __one_by_one_convertible<__conv_types<_Tp>, __conv_types<_Up>>
82     : public is_convertible<_Tp, _Up>::type { };
83
84   template<typename _T1, typename... _TR, typename _U1, typename... _UR>
85     struct __one_by_one_convertible<__conv_types<_T1, _TR...>,
86                                     __conv_types<_U1, _UR...>>
87     : public __and_<is_convertible<_T1, _U1>,
88                     __one_by_one_convertible<__conv_types<_TR...>,
89                                              __conv_types<_UR...>>>::type
90     { };
91
92   template<typename _Tuple1, typename _Tuple2>
93     struct __all_convertible;
94
95   template<typename... _TTypes, typename... _UTypes>
96     struct __all_convertible<__conv_types<_TTypes...>,
97                              __conv_types<_UTypes...>>
98     : public __one_by_one_convertible<__conv_types<_TTypes...>,
99                                       __conv_types<_UTypes...>>::type { };
100
101   template<std::size_t _Idx, typename _Head, bool _IsEmpty>
102     struct _Head_base;
103
104   template<std::size_t _Idx, typename _Head>
105     struct _Head_base<_Idx, _Head, true>
106     : public _Head
107     {
108       constexpr _Head_base()
109       : _Head() { }
110
111       constexpr _Head_base(const _Head& __h)
112       : _Head(__h) { }
113
114       template<typename _UHead, typename = typename
115                enable_if<!is_convertible<_UHead,
116                                          __uses_alloc_base>::value>::type>
117         constexpr _Head_base(_UHead&& __h)
118         : _Head(std::forward<_UHead>(__h)) { }
119
120       _Head_base(__uses_alloc0)
121       : _Head() { }
122
123       template<typename _Alloc>
124         _Head_base(__uses_alloc1<_Alloc> __a)
125         : _Head(allocator_arg, *__a._M_a) { }
126
127       template<typename _Alloc>
128         _Head_base(__uses_alloc2<_Alloc> __a)
129         : _Head(*__a._M_a) { }
130
131       template<typename _UHead>
132         _Head_base(__uses_alloc0, _UHead&& __uhead)
133         : _Head(std::forward<_UHead>(__uhead)) { }
134
135       template<typename _Alloc, typename _UHead>
136         _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
137         : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
138
139       template<typename _Alloc, typename _UHead>
140         _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
141         : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
142
143       _Head&       
144       _M_head() noexcept { return *this; }
145
146       constexpr const _Head& 
147       _M_head() const noexcept { return *this; }
148     };
149
150   template<std::size_t _Idx, typename _Head>
151     struct _Head_base<_Idx, _Head, false>
152     {
153       constexpr _Head_base()
154       : _M_head_impl() { }
155
156       constexpr _Head_base(const _Head& __h)
157       : _M_head_impl(__h) { }
158
159       template<typename _UHead, typename = typename
160                enable_if<!is_convertible<_UHead,
161                                          __uses_alloc_base>::value>::type>
162         constexpr _Head_base(_UHead&& __h)
163         : _M_head_impl(std::forward<_UHead>(__h)) { }
164
165       _Head_base(__uses_alloc0)
166       : _M_head_impl() { }
167
168       template<typename _Alloc>
169         _Head_base(__uses_alloc1<_Alloc> __a)
170         : _M_head_impl(allocator_arg, *__a._M_a) { }
171
172       template<typename _Alloc>
173         _Head_base(__uses_alloc2<_Alloc> __a)
174         : _M_head_impl(*__a._M_a) { }
175
176       template<typename _UHead>
177         _Head_base(__uses_alloc0, _UHead&& __uhead)
178         : _M_head_impl(std::forward<_UHead>(__uhead)) { }
179
180       template<typename _Alloc, typename _UHead>
181         _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
182         : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
183         { }
184
185       template<typename _Alloc, typename _UHead>
186         _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
187         : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
188
189       _Head&       
190       _M_head() noexcept { return _M_head_impl; }
191
192       constexpr const _Head& 
193       _M_head() const noexcept { return _M_head_impl; }        
194
195       _Head _M_head_impl; 
196     };
197
198   /**
199    * Contains the actual implementation of the @c tuple template, stored
200    * as a recursive inheritance hierarchy from the first element (most
201    * derived class) to the last (least derived class). The @c Idx
202    * parameter gives the 0-based index of the element stored at this
203    * point in the hierarchy; we use it to implement a constant-time
204    * get() operation.
205    */
206   template<std::size_t _Idx, typename... _Elements>
207     struct _Tuple_impl; 
208
209   /**
210    * Zero-element tuple implementation. This is the basis case for the 
211    * inheritance recursion.
212    */
213   template<std::size_t _Idx>
214     struct _Tuple_impl<_Idx>
215     {
216       template<std::size_t, typename...> friend class _Tuple_impl;
217
218       _Tuple_impl() = default;
219
220       template<typename _Alloc>
221         _Tuple_impl(allocator_arg_t, const _Alloc&) { }
222
223       template<typename _Alloc>
224         _Tuple_impl(allocator_arg_t, const _Alloc&, const _Tuple_impl&) { }
225
226       template<typename _Alloc>
227         _Tuple_impl(allocator_arg_t, const _Alloc&, _Tuple_impl&&) { }
228
229     protected:
230       void _M_swap(_Tuple_impl&) noexcept { /* no-op */ }
231     };
232
233   /**
234    * Recursive tuple implementation. Here we store the @c Head element
235    * and derive from a @c Tuple_impl containing the remaining elements
236    * (which contains the @c Tail).
237    */
238   template<std::size_t _Idx, typename _Head, typename... _Tail>
239     struct _Tuple_impl<_Idx, _Head, _Tail...>
240     : public _Tuple_impl<_Idx + 1, _Tail...>,
241       private _Head_base<_Idx, _Head, std::is_empty<_Head>::value>
242     {
243       template<std::size_t, typename...> friend class _Tuple_impl;
244
245       typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
246       typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
247
248       _Head&            
249       _M_head() noexcept { return _Base::_M_head(); }
250
251       constexpr const _Head&      
252       _M_head() const noexcept { return _Base::_M_head(); }
253
254       _Inherited&       
255       _M_tail() noexcept { return *this; }
256
257       constexpr const _Inherited& 
258       _M_tail() const noexcept { return *this; }
259
260       constexpr _Tuple_impl()
261       : _Inherited(), _Base() { }
262
263       explicit 
264       constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
265       : _Inherited(__tail...), _Base(__head) { }
266
267       template<typename _UHead, typename... _UTail, typename = typename
268                enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type> 
269         explicit
270         constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
271         : _Inherited(std::forward<_UTail>(__tail)...),
272           _Base(std::forward<_UHead>(__head)) { }
273
274       constexpr _Tuple_impl(const _Tuple_impl&) = default;
275
276       _Tuple_impl(_Tuple_impl&& __in)
277       noexcept(__and_<is_nothrow_move_constructible<_Head>,
278                       is_nothrow_move_constructible<_Inherited>>::value)
279       : _Inherited(std::move(__in._M_tail())), 
280         _Base(std::forward<_Head>(__in._M_head())) { }
281
282       template<typename... _UElements>
283         constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
284         : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
285
286       template<typename _UHead, typename... _UTails>
287         _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
288         : _Inherited(std::move(__in._M_tail())),
289           _Base(std::forward<_UHead>(__in._M_head())) { }
290
291       template<typename _Alloc>
292         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
293         : _Inherited(__tag, __a),
294           _Base(__use_alloc<_Head>(__a)) { }
295
296       template<typename _Alloc>
297         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
298                     const _Head& __head, const _Tail&... __tail)
299         : _Inherited(__tag, __a, __tail...),
300           _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
301
302       template<typename _Alloc, typename _UHead, typename... _UTail,
303                typename = typename enable_if<sizeof...(_Tail)
304                                              == sizeof...(_UTail)>::type>
305         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
306                     _UHead&& __head, _UTail&&... __tail)
307         : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
308           _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
309                 std::forward<_UHead>(__head)) { }
310
311       template<typename _Alloc>
312         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
313                     const _Tuple_impl& __in)
314         : _Inherited(__tag, __a, __in._M_tail()), 
315           _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __in._M_head()) { }
316
317       template<typename _Alloc>
318         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
319                     _Tuple_impl&& __in)
320         : _Inherited(__tag, __a, std::move(__in._M_tail())), 
321           _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
322                 std::forward<_Head>(__in._M_head())) { }
323
324       template<typename _Alloc, typename... _UElements>
325         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
326                     const _Tuple_impl<_Idx, _UElements...>& __in)
327         : _Inherited(__tag, __a, __in._M_tail()),
328           _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __in._M_head()) { }
329
330       template<typename _Alloc, typename _UHead, typename... _UTails>
331         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
332                     _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
333         : _Inherited(__tag, __a, std::move(__in._M_tail())),
334           _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
335                 std::forward<_UHead>(__in._M_head())) { }
336
337       _Tuple_impl&
338       operator=(const _Tuple_impl& __in)
339       {
340         _M_head() = __in._M_head();
341         _M_tail() = __in._M_tail();
342         return *this;
343       }
344
345       _Tuple_impl&
346       operator=(_Tuple_impl&& __in)
347       noexcept(__and_<is_nothrow_move_assignable<_Head>,
348                       is_nothrow_move_assignable<_Inherited>>::value)
349       {
350         _M_head() = std::forward<_Head>(__in._M_head());
351         _M_tail() = std::move(__in._M_tail());
352         return *this;
353       }
354
355       template<typename... _UElements>
356         _Tuple_impl&
357         operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
358         {
359           _M_head() = __in._M_head();
360           _M_tail() = __in._M_tail();
361           return *this;
362         }
363
364       template<typename _UHead, typename... _UTails>
365         _Tuple_impl&
366         operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
367         {
368           _M_head() = std::forward<_UHead>(__in._M_head());
369           _M_tail() = std::move(__in._M_tail());
370           return *this;
371         }
372
373     protected:
374       void
375       _M_swap(_Tuple_impl& __in)
376       noexcept(noexcept(swap(std::declval<_Head&>(),
377                              std::declval<_Head&>()))
378                && noexcept(__in._M_tail()._M_swap(__in._M_tail())))
379       {
380         using std::swap;
381         swap(this->_M_head(), __in._M_head());
382         _Inherited::_M_swap(__in._M_tail());
383       }
384     };
385
386   /// Primary class template, tuple
387   template<typename... _Elements> 
388     class tuple : public _Tuple_impl<0, _Elements...>
389     {
390       typedef _Tuple_impl<0, _Elements...> _Inherited;
391
392     public:
393       constexpr tuple()
394       : _Inherited() { }
395
396       explicit
397       constexpr tuple(const _Elements&... __elements)
398       : _Inherited(__elements...) { }
399
400       template<typename... _UElements, typename = typename
401         enable_if<__and_<integral_constant<bool, sizeof...(_UElements)
402                                            == sizeof...(_Elements)>,
403                          __all_convertible<__conv_types<_UElements...>,
404                                            __conv_types<_Elements...>>
405                          >::value>::type>
406         explicit
407         constexpr tuple(_UElements&&... __elements)
408         : _Inherited(std::forward<_UElements>(__elements)...) { }
409
410       constexpr tuple(const tuple&) = default;
411
412       constexpr tuple(tuple&&) = default; 
413
414       template<typename... _UElements, typename = typename
415         enable_if<__and_<integral_constant<bool, sizeof...(_UElements)
416                                            == sizeof...(_Elements)>,
417                          __all_convertible<__conv_types<const _UElements&...>,
418                                            __conv_types<_Elements...>>
419                          >::value>::type>
420         constexpr tuple(const tuple<_UElements...>& __in)
421         : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
422         { }
423
424       template<typename... _UElements, typename = typename
425         enable_if<__and_<integral_constant<bool, sizeof...(_UElements)
426                                            == sizeof...(_Elements)>,
427                          __all_convertible<__conv_types<_UElements...>,
428                                            __conv_types<_Elements...>>
429                          >::value>::type>
430         constexpr tuple(tuple<_UElements...>&& __in)
431         : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
432
433       // Allocator-extended constructors.
434
435       template<typename _Alloc>
436         tuple(allocator_arg_t __tag, const _Alloc& __a)
437         : _Inherited(__tag, __a) { }
438
439       template<typename _Alloc>
440         tuple(allocator_arg_t __tag, const _Alloc& __a,
441               const _Elements&... __elements)
442         : _Inherited(__tag, __a, __elements...) { }
443
444       template<typename _Alloc, typename... _UElements, typename = typename
445                enable_if<sizeof...(_UElements)
446                          == sizeof...(_Elements)>::type>
447         tuple(allocator_arg_t __tag, const _Alloc& __a,
448               _UElements&&... __elements)
449         : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
450         { }
451
452       template<typename _Alloc>
453         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
454         : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
455
456       template<typename _Alloc>
457         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
458         : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
459
460       template<typename _Alloc, typename... _UElements, typename = typename
461                enable_if<sizeof...(_UElements)
462                          == sizeof...(_Elements)>::type>
463         tuple(allocator_arg_t __tag, const _Alloc& __a,
464               const tuple<_UElements...>& __in)
465         : _Inherited(__tag, __a,
466                      static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
467         { }
468
469       template<typename _Alloc, typename... _UElements, typename = typename
470                enable_if<sizeof...(_UElements)
471                          == sizeof...(_Elements)>::type>
472         tuple(allocator_arg_t __tag, const _Alloc& __a,
473               tuple<_UElements...>&& __in)
474         : _Inherited(__tag, __a,
475                      static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
476         { }
477
478       tuple&
479       operator=(const tuple& __in)
480       {
481         static_cast<_Inherited&>(*this) = __in;
482         return *this;
483       }
484
485       tuple&
486       operator=(tuple&& __in)
487       noexcept(is_nothrow_move_assignable<_Inherited>::value)
488       {
489         static_cast<_Inherited&>(*this) = std::move(__in);
490         return *this;
491       }
492
493       template<typename... _UElements, typename = typename
494                enable_if<sizeof...(_UElements)
495                          == sizeof...(_Elements)>::type>
496         tuple&
497         operator=(const tuple<_UElements...>& __in)
498         {
499           static_cast<_Inherited&>(*this) = __in;
500           return *this;
501         }
502
503       template<typename... _UElements, typename = typename
504                enable_if<sizeof...(_UElements)
505                          == sizeof...(_Elements)>::type>
506         tuple&
507         operator=(tuple<_UElements...>&& __in)
508         {
509           static_cast<_Inherited&>(*this) = std::move(__in);
510           return *this;
511         }
512
513       void
514       swap(tuple& __in)
515       noexcept(noexcept(__in._M_swap(__in)))
516       { _Inherited::_M_swap(__in); }
517     };
518
519   // Explicit specialization, zero-element tuple.
520   template<>  
521     class tuple<>
522     {
523     public:
524       void swap(tuple&) noexcept { /* no-op */ }
525     };
526
527   /// Partial specialization, 2-element tuple.
528   /// Includes construction and assignment from a pair.
529   template<typename _T1, typename _T2>
530     class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
531     {
532       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
533
534     public:
535       constexpr tuple()
536       : _Inherited() { }
537
538       explicit
539       constexpr tuple(const _T1& __a1, const _T2& __a2)
540       : _Inherited(__a1, __a2) { }
541
542       template<typename _U1, typename _U2, typename = typename
543                enable_if<__and_<is_convertible<_U1, _T1>,
544                                 is_convertible<_U2, _T2>>::value>::type>
545         explicit
546         constexpr tuple(_U1&& __a1, _U2&& __a2)
547         : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
548
549       constexpr tuple(const tuple&) = default;
550
551       constexpr tuple(tuple&&) = default;
552
553       template<typename _U1, typename _U2, typename = typename
554         enable_if<__and_<is_convertible<const _U1&, _T1>,
555                          is_convertible<const _U2&, _T2>>::value>::type>
556         constexpr tuple(const tuple<_U1, _U2>& __in)
557         : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
558
559       template<typename _U1, typename _U2, typename = typename
560                enable_if<__and_<is_convertible<_U1, _T1>,
561                                 is_convertible<_U2, _T2>>::value>::type>
562         constexpr tuple(tuple<_U1, _U2>&& __in)
563         : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
564
565       template<typename _U1, typename _U2, typename = typename
566         enable_if<__and_<is_convertible<const _U1&, _T1>,
567                          is_convertible<const _U2&, _T2>>::value>::type>
568         constexpr tuple(const pair<_U1, _U2>& __in)
569         : _Inherited(__in.first, __in.second) { }
570
571       template<typename _U1, typename _U2, typename = typename
572                enable_if<__and_<is_convertible<_U1, _T1>,
573                                 is_convertible<_U2, _T2>>::value>::type>
574          constexpr tuple(pair<_U1, _U2>&& __in)
575         : _Inherited(std::forward<_U1>(__in.first),
576                      std::forward<_U2>(__in.second)) { }
577
578       // Allocator-extended constructors.
579
580       template<typename _Alloc>
581         tuple(allocator_arg_t __tag, const _Alloc& __a)
582         : _Inherited(__tag, __a) { }
583
584       template<typename _Alloc>
585         tuple(allocator_arg_t __tag, const _Alloc& __a,
586               const _T1& __a1, const _T2& __a2)
587         : _Inherited(__tag, __a, __a1, __a2) { }
588
589       template<typename _Alloc, typename _U1, typename _U2>
590         tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
591         : _Inherited(__tag, __a, std::forward<_U1>(__a1),
592                      std::forward<_U2>(__a2)) { }
593
594       template<typename _Alloc>
595         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
596         : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
597
598       template<typename _Alloc>
599         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
600         : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
601
602       template<typename _Alloc, typename _U1, typename _U2>
603         tuple(allocator_arg_t __tag, const _Alloc& __a,
604               const tuple<_U1, _U2>& __in)
605         : _Inherited(__tag, __a,
606                      static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
607         { }
608
609       template<typename _Alloc, typename _U1, typename _U2>
610         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
611         : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
612         { }
613
614       template<typename _Alloc, typename _U1, typename _U2>
615         tuple(allocator_arg_t __tag, const _Alloc& __a,
616               const pair<_U1, _U2>& __in)
617         : _Inherited(__tag, __a, __in.first, __in.second) { }
618
619       template<typename _Alloc, typename _U1, typename _U2>
620         tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
621         : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
622                      std::forward<_U2>(__in.second)) { }
623
624       tuple&
625       operator=(const tuple& __in)
626       {
627         static_cast<_Inherited&>(*this) = __in;
628         return *this;
629       }
630
631       tuple&
632       operator=(tuple&& __in)
633       noexcept(is_nothrow_move_assignable<_Inherited>::value)
634       {
635         static_cast<_Inherited&>(*this) = std::move(__in);
636         return *this;
637       }
638
639       template<typename _U1, typename _U2>
640         tuple&
641         operator=(const tuple<_U1, _U2>& __in)
642         {
643           static_cast<_Inherited&>(*this) = __in;
644           return *this;
645         }
646
647       template<typename _U1, typename _U2>
648         tuple&
649         operator=(tuple<_U1, _U2>&& __in)
650         {
651           static_cast<_Inherited&>(*this) = std::move(__in);
652           return *this;
653         }
654
655       template<typename _U1, typename _U2>
656         tuple&
657         operator=(const pair<_U1, _U2>& __in)
658         {
659           this->_M_head() = __in.first;
660           this->_M_tail()._M_head() = __in.second;
661           return *this;
662         }
663
664       template<typename _U1, typename _U2>
665         tuple&
666         operator=(pair<_U1, _U2>&& __in)
667         {
668           this->_M_head() = std::forward<_U1>(__in.first);
669           this->_M_tail()._M_head() = std::forward<_U2>(__in.second);
670           return *this;
671         }
672
673       void
674       swap(tuple& __in)
675       noexcept(noexcept(__in._M_swap(__in)))
676       { _Inherited::_M_swap(__in); }
677     };
678
679
680   /// Gives the type of the ith element of a given tuple type.
681   template<std::size_t __i, typename _Tp>
682     struct tuple_element;
683
684   /**
685    * Recursive case for tuple_element: strip off the first element in
686    * the tuple and retrieve the (i-1)th element of the remaining tuple.
687    */
688   template<std::size_t __i, typename _Head, typename... _Tail>
689     struct tuple_element<__i, tuple<_Head, _Tail...> >
690     : tuple_element<__i - 1, tuple<_Tail...> > { };
691
692   /**
693    * Basis case for tuple_element: The first element is the one we're seeking.
694    */
695   template<typename _Head, typename... _Tail>
696     struct tuple_element<0, tuple<_Head, _Tail...> >
697     {
698       typedef _Head type;
699     };
700
701   template<std::size_t __i, typename _Tp>
702     struct tuple_element<__i, const _Tp>
703     {
704       typedef typename
705       add_const<typename tuple_element<__i, _Tp>::type>::type type;
706     };
707
708   template<std::size_t __i, typename _Tp>
709     struct tuple_element<__i, volatile _Tp>
710     {
711       typedef typename
712       add_volatile<typename tuple_element<__i, _Tp>::type>::type type;
713     };
714
715   template<std::size_t __i, typename _Tp>
716     struct tuple_element<__i, const volatile _Tp>
717     {
718       typedef typename
719       add_cv<typename tuple_element<__i, _Tp>::type>::type type;
720     };
721
722   /// Finds the size of a given tuple type.
723   template<typename _Tp>
724     struct tuple_size;
725
726   template<typename _Tp>
727     struct tuple_size<const _Tp>
728     : public integral_constant<
729              typename remove_cv<decltype(tuple_size<_Tp>::value)>::type,
730              tuple_size<_Tp>::value> { };
731
732   template<typename _Tp>
733     struct tuple_size<volatile _Tp>
734     : public integral_constant<
735              typename remove_cv<decltype(tuple_size<_Tp>::value)>::type,
736              tuple_size<_Tp>::value> { };
737
738   template<typename _Tp>
739     struct tuple_size<const volatile _Tp>
740     : public integral_constant<
741              typename remove_cv<decltype(tuple_size<_Tp>::value)>::type,
742              tuple_size<_Tp>::value> { };
743
744   /// class tuple_size
745   template<typename... _Elements>
746     struct tuple_size<tuple<_Elements...>>
747     : public integral_constant<std::size_t, sizeof...(_Elements)> { };
748
749   template<std::size_t __i, typename _Head, typename... _Tail>
750     inline typename __add_ref<_Head>::type
751     __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
752     { return __t._M_head(); }
753
754   template<std::size_t __i, typename _Head, typename... _Tail>
755     inline constexpr typename __add_c_ref<_Head>::type
756     __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
757     { return __t._M_head(); }
758
759   // Return a reference (const reference, rvalue reference) to the ith element
760   // of a tuple.  Any const or non-const ref elements are returned with their
761   // original type.
762   template<std::size_t __i, typename... _Elements>
763     inline typename __add_ref<
764                       typename tuple_element<__i, tuple<_Elements...>>::type
765                     >::type
766     get(tuple<_Elements...>& __t) noexcept
767     { return __get_helper<__i>(__t); }
768
769   template<std::size_t __i, typename... _Elements>
770     inline constexpr typename __add_c_ref<
771                       typename tuple_element<__i, tuple<_Elements...>>::type
772                     >::type
773     get(const tuple<_Elements...>& __t) noexcept
774     { return __get_helper<__i>(__t); }
775
776   template<std::size_t __i, typename... _Elements>
777     inline typename __add_r_ref<
778                       typename tuple_element<__i, tuple<_Elements...>>::type
779                     >::type
780     get(tuple<_Elements...>&& __t) noexcept
781     { return std::forward<typename tuple_element<__i,
782         tuple<_Elements...>>::type&&>(get<__i>(__t)); }
783
784   // This class helps construct the various comparison operations on tuples
785   template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
786            typename _Tp, typename _Up>
787     struct __tuple_compare;
788
789   template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
790     struct __tuple_compare<0, __i, __j, _Tp, _Up>
791     {
792       static bool 
793       __eq(const _Tp& __t, const _Up& __u)
794       {
795         return (get<__i>(__t) == get<__i>(__u) &&
796                 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
797       }
798      
799       static bool 
800       __less(const _Tp& __t, const _Up& __u)
801       {
802         return ((get<__i>(__t) < get<__i>(__u))
803                 || !(get<__i>(__u) < get<__i>(__t)) &&
804                 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
805       }
806     };
807
808   template<std::size_t __i, typename _Tp, typename _Up>
809     struct __tuple_compare<0, __i, __i, _Tp, _Up>
810     {
811       static bool 
812       __eq(const _Tp&, const _Up&) { return true; }
813      
814       static bool 
815       __less(const _Tp&, const _Up&) { return false; }
816     };
817
818   template<typename... _TElements, typename... _UElements>
819     bool
820     operator==(const tuple<_TElements...>& __t,
821                const tuple<_UElements...>& __u)
822     {
823       typedef tuple<_TElements...> _Tp;
824       typedef tuple<_UElements...> _Up;
825       return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
826               0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
827     }
828
829   template<typename... _TElements, typename... _UElements>
830     bool
831     operator<(const tuple<_TElements...>& __t,
832               const tuple<_UElements...>& __u)
833     {
834       typedef tuple<_TElements...> _Tp;
835       typedef tuple<_UElements...> _Up;
836       return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
837               0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
838     }
839
840   template<typename... _TElements, typename... _UElements>
841     inline bool
842     operator!=(const tuple<_TElements...>& __t,
843                const tuple<_UElements...>& __u)
844     { return !(__t == __u); }
845
846   template<typename... _TElements, typename... _UElements>
847     inline bool
848     operator>(const tuple<_TElements...>& __t,
849               const tuple<_UElements...>& __u)
850     { return __u < __t; }
851
852   template<typename... _TElements, typename... _UElements>
853     inline bool
854     operator<=(const tuple<_TElements...>& __t,
855                const tuple<_UElements...>& __u)
856     { return !(__u < __t); }
857
858   template<typename... _TElements, typename... _UElements>
859     inline bool
860     operator>=(const tuple<_TElements...>& __t,
861                const tuple<_UElements...>& __u)
862     { return !(__t < __u); }
863
864   // NB: DR 705.
865   template<typename... _Elements>
866     inline tuple<typename __decay_and_strip<_Elements>::__type...>
867     make_tuple(_Elements&&... __args)
868     {
869       typedef tuple<typename __decay_and_strip<_Elements>::__type...>
870         __result_type;
871       return __result_type(std::forward<_Elements>(__args)...);
872     }
873
874   template<typename... _Elements>
875     inline tuple<_Elements&&...>
876     forward_as_tuple(_Elements&&... __args) noexcept
877     { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
878
879
880   template<typename, std::size_t> struct array;
881
882   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
883     _Tp& get(array<_Tp, _Nm>&) noexcept;
884
885   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
886     _Tp&& get(array<_Tp, _Nm>&&) noexcept;
887
888   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
889     const _Tp& get(const array<_Tp, _Nm>&) noexcept;
890
891   template<typename>
892     struct __is_tuple_like_impl : false_type
893     { };
894
895   template<typename... _Tps>
896     struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
897     { };
898
899   template<typename _T1, typename _T2>
900     struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
901     { };
902
903   template<typename _Tp, std::size_t _Nm>
904     struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
905     { };
906
907   // Internal type trait that allows us to sfinae-protect tuple_cat.
908   template<typename _Tp>
909     struct __is_tuple_like
910     : public __is_tuple_like_impl<typename std::remove_cv
911             <typename std::remove_reference<_Tp>::type>::type>::type
912     { };
913
914   // Stores a tuple of indices.  Also used by bind() to extract the elements
915   // in a tuple. 
916   template<std::size_t... _Indexes>
917     struct _Index_tuple
918     {
919       typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
920     };
921
922   // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
923   template<std::size_t _Num>
924     struct _Build_index_tuple
925     {
926       typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type;
927     };
928
929   template<>
930     struct _Build_index_tuple<0>
931     {
932       typedef _Index_tuple<> __type;
933     };
934
935   template<std::size_t, typename, typename, std::size_t>
936     struct __make_tuple_impl;
937
938   template<std::size_t _Idx, typename _Tuple, typename... _Tp,
939            std::size_t _Nm>
940     struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
941     {
942       typedef typename __make_tuple_impl<_Idx + 1, tuple<_Tp...,
943         typename std::tuple_element<_Idx, _Tuple>::type>, _Tuple, _Nm>::__type
944       __type;
945     };
946
947   template<std::size_t _Nm, typename _Tuple, typename... _Tp>
948     struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
949     {
950       typedef tuple<_Tp...> __type;
951     };
952
953   template<typename _Tuple>
954     struct __do_make_tuple
955     : public __make_tuple_impl<0, tuple<>, _Tuple,
956                                std::tuple_size<_Tuple>::value>
957     { };
958
959   // Returns the std::tuple equivalent of a tuple-like type.
960   template<typename _Tuple>
961     struct __make_tuple
962     : public __do_make_tuple<typename std::remove_cv
963             <typename std::remove_reference<_Tuple>::type>::type>
964     { };
965
966   // Combines several std::tuple's into a single one.
967   template<typename...>
968     struct __combine_tuples;
969
970   template<>
971     struct __combine_tuples<>
972     {
973       typedef tuple<> __type;
974     };
975
976   template<typename... _Ts>
977     struct __combine_tuples<tuple<_Ts...>>
978     {
979       typedef tuple<_Ts...> __type;
980     };
981
982   template<typename... _T1s, typename... _T2s, typename... _Rem>
983     struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
984     {
985       typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
986                                         _Rem...>::__type __type;
987     };
988
989   // Computes the result type of tuple_cat given a set of tuple-like types.
990   template<typename... _Tpls>
991     struct __tuple_cat_result
992     {
993       typedef typename __combine_tuples
994         <typename __make_tuple<_Tpls>::__type...>::__type __type;
995     };
996
997   // Helper to determine the index set for the first tuple-like
998   // type of a given set.
999   template<typename...>
1000     struct __make_1st_indices;
1001
1002   template<>
1003     struct __make_1st_indices<>
1004     {
1005       typedef std::_Index_tuple<> __type;
1006     };
1007
1008   template<typename _Tp, typename... _Tpls>
1009     struct __make_1st_indices<_Tp, _Tpls...>
1010     {
1011       typedef typename std::_Build_index_tuple<std::tuple_size<
1012         typename std::remove_reference<_Tp>::type>::value>::__type __type;
1013     };
1014
1015   // Performs the actual concatenation by step-wise expanding tuple-like
1016   // objects into the elements,  which are finally forwarded into the
1017   // result tuple.
1018   template<typename _Ret, typename _Indices, typename... _Tpls>
1019     struct __tuple_concater;
1020
1021   template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1022     struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1023     {
1024       template<typename... _Us>
1025         static _Ret
1026         _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1027         {
1028           typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1029           typedef __tuple_concater<_Ret, __idx, _Tpls...>      __next;
1030           return __next::_S_do(std::forward<_Tpls>(__tps)...,
1031                                std::forward<_Us>(__us)...,
1032                                std::get<_Is>(std::forward<_Tp>(__tp))...);
1033         }
1034     };
1035
1036   template<typename _Ret>
1037     struct __tuple_concater<_Ret, std::_Index_tuple<>>
1038     {
1039       template<typename... _Us>
1040         static _Ret
1041         _S_do(_Us&&... __us)
1042         {
1043           return _Ret(std::forward<_Us>(__us)...);
1044         }
1045     };
1046
1047   template<typename... _Tpls>
1048     inline typename
1049     std::enable_if<__and_<__is_tuple_like<_Tpls>...>::value,
1050                    typename __tuple_cat_result<_Tpls...>::__type>::type
1051     tuple_cat(_Tpls&&... __tpls)
1052     {
1053       typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1054       typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1055       typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1056       return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1057     }
1058
1059   template<typename... _Elements>
1060     inline tuple<_Elements&...>
1061     tie(_Elements&... __args) noexcept
1062     { return tuple<_Elements&...>(__args...); }
1063
1064   template<typename... _Elements>
1065     inline void 
1066     swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1067     noexcept(noexcept(__x.swap(__y)))
1068     { __x.swap(__y); }
1069
1070   // A class (and instance) which can be used in 'tie' when an element
1071   // of a tuple is not required
1072   struct _Swallow_assign
1073   {
1074     template<class _Tp>
1075       const _Swallow_assign&
1076       operator=(const _Tp&) const
1077       { return *this; }
1078   };
1079
1080   const _Swallow_assign ignore{};
1081
1082   /// Partial specialization for tuples
1083   template<typename... _Types, typename _Alloc>
1084     struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1085
1086   // See stl_pair.h...
1087   template<class _T1, class _T2>
1088     template<typename _Tp, typename... _Args>
1089       inline _Tp
1090       pair<_T1, _T2>::__cons(tuple<_Args...>&& __tuple)
1091       {
1092         typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1093           _Indexes;
1094         return __do_cons<_Tp>(std::move(__tuple), _Indexes());
1095       }
1096
1097   template<class _T1, class _T2>
1098     template<typename _Tp, typename... _Args, std::size_t... _Indexes>
1099       inline _Tp
1100       pair<_T1, _T2>::__do_cons(tuple<_Args...>&& __tuple,
1101                                 const _Index_tuple<_Indexes...>&)
1102       { return _Tp(std::forward<_Args>(get<_Indexes>(__tuple))...); }
1103
1104 _GLIBCXX_END_NAMESPACE_VERSION
1105 } // namespace
1106
1107 #endif // __GXX_EXPERIMENTAL_CXX0X__
1108
1109 #endif // _GLIBCXX_TUPLE