OSDN Git Service

474634fc24e8b6b33ae650a85f1950d148b21ac4
[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       static constexpr _Head&
144       _M_head(_Head_base& __b) noexcept { return __b; }
145
146       static constexpr const _Head&
147       _M_head(const _Head_base& __b) noexcept { return __b; }
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       static constexpr _Head&
190       _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
191
192       static constexpr const _Head&
193       _M_head(const _Head_base& __b) noexcept { return __b._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       static constexpr _Head&  
249       _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
250
251       static constexpr const _Head&
252       _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
253
254       static constexpr _Inherited&
255       _M_tail(_Tuple_impl& __t) noexcept { return __t; }
256
257       static constexpr const _Inherited&
258       _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
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       constexpr
277       _Tuple_impl(_Tuple_impl&& __in)
278       noexcept(__and_<is_nothrow_move_constructible<_Head>,
279                       is_nothrow_move_constructible<_Inherited>>::value)
280       : _Inherited(std::move(_M_tail(__in))), 
281         _Base(std::forward<_Head>(_M_head(__in))) { }
282
283       template<typename... _UElements>
284         constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
285         : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
286           _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
287
288       template<typename _UHead, typename... _UTails>
289         constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
290         : _Inherited(std::move
291                      (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
292           _Base(std::forward<_UHead>
293                 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
294
295       template<typename _Alloc>
296         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
297         : _Inherited(__tag, __a),
298           _Base(__use_alloc<_Head>(__a)) { }
299
300       template<typename _Alloc>
301         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
302                     const _Head& __head, const _Tail&... __tail)
303         : _Inherited(__tag, __a, __tail...),
304           _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
305
306       template<typename _Alloc, typename _UHead, typename... _UTail,
307                typename = typename enable_if<sizeof...(_Tail)
308                                              == sizeof...(_UTail)>::type>
309         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
310                     _UHead&& __head, _UTail&&... __tail)
311         : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
312           _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
313                 std::forward<_UHead>(__head)) { }
314
315       template<typename _Alloc>
316         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
317                     const _Tuple_impl& __in)
318         : _Inherited(__tag, __a, _M_tail(__in)), 
319           _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
320
321       template<typename _Alloc>
322         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
323                     _Tuple_impl&& __in)
324         : _Inherited(__tag, __a, std::move(_M_tail(__in))), 
325           _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
326                 std::forward<_Head>(_M_head(__in))) { }
327
328       template<typename _Alloc, typename... _UElements>
329         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
330                     const _Tuple_impl<_Idx, _UElements...>& __in)
331         : _Inherited(__tag, __a,
332                      _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
333           _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
334                 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
335
336       template<typename _Alloc, typename _UHead, typename... _UTails>
337         _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
338                     _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
339         : _Inherited(__tag, __a, std::move
340                      (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
341           _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
342                 std::forward<_UHead>
343                 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
344
345       _Tuple_impl&
346       operator=(const _Tuple_impl& __in)
347       {
348         _M_head(*this) = _M_head(__in);
349         _M_tail(*this) = _M_tail(__in);
350         return *this;
351       }
352
353       _Tuple_impl&
354       operator=(_Tuple_impl&& __in)
355       noexcept(__and_<is_nothrow_move_assignable<_Head>,
356                       is_nothrow_move_assignable<_Inherited>>::value)
357       {
358         _M_head(*this) = std::forward<_Head>(_M_head(__in));
359         _M_tail(*this) = std::move(_M_tail(__in));
360         return *this;
361       }
362
363       template<typename... _UElements>
364         _Tuple_impl&
365         operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
366         {
367           _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
368           _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
369           return *this;
370         }
371
372       template<typename _UHead, typename... _UTails>
373         _Tuple_impl&
374         operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
375         {
376           _M_head(*this) = std::forward<_UHead>
377             (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
378           _M_tail(*this) = std::move
379             (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
380           return *this;
381         }
382
383     protected:
384       void
385       _M_swap(_Tuple_impl& __in)
386       noexcept(noexcept(swap(std::declval<_Head&>(),
387                              std::declval<_Head&>()))
388                && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
389       {
390         using std::swap;
391         swap(_M_head(*this), _M_head(__in));
392         _Inherited::_M_swap(_M_tail(__in));
393       }
394     };
395
396   /// Primary class template, tuple
397   template<typename... _Elements> 
398     class tuple : public _Tuple_impl<0, _Elements...>
399     {
400       typedef _Tuple_impl<0, _Elements...> _Inherited;
401
402     public:
403       constexpr tuple()
404       : _Inherited() { }
405
406       explicit
407       constexpr tuple(const _Elements&... __elements)
408       : _Inherited(__elements...) { }
409
410       template<typename... _UElements,
411                typename = typename enable_if<sizeof...(_UElements)
412                                              == sizeof...(_Elements)>::type,
413                typename = typename
414                  enable_if<__all_convertible<__conv_types<_UElements...>,
415                                              __conv_types<_Elements...> >::value
416                            >::type>
417         explicit
418         constexpr tuple(_UElements&&... __elements)
419         : _Inherited(std::forward<_UElements>(__elements)...) { }
420
421       constexpr tuple(const tuple&) = default;
422
423       constexpr tuple(tuple&&) = default; 
424
425       template<typename... _UElements, typename = typename
426         enable_if<__and_<integral_constant<bool, sizeof...(_UElements)
427                                            == sizeof...(_Elements)>,
428                          __all_convertible<__conv_types<const _UElements&...>,
429                                            __conv_types<_Elements...>>
430                          >::value>::type>
431         constexpr tuple(const tuple<_UElements...>& __in)
432         : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
433         { }
434
435       template<typename... _UElements, typename = typename
436         enable_if<__and_<integral_constant<bool, sizeof...(_UElements)
437                                            == sizeof...(_Elements)>,
438                          __all_convertible<__conv_types<_UElements...>,
439                                            __conv_types<_Elements...>>
440                          >::value>::type>
441         constexpr tuple(tuple<_UElements...>&& __in)
442         : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
443
444       // Allocator-extended constructors.
445
446       template<typename _Alloc>
447         tuple(allocator_arg_t __tag, const _Alloc& __a)
448         : _Inherited(__tag, __a) { }
449
450       template<typename _Alloc>
451         tuple(allocator_arg_t __tag, const _Alloc& __a,
452               const _Elements&... __elements)
453         : _Inherited(__tag, __a, __elements...) { }
454
455       template<typename _Alloc, typename... _UElements, typename = typename
456                enable_if<sizeof...(_UElements)
457                          == sizeof...(_Elements)>::type>
458         tuple(allocator_arg_t __tag, const _Alloc& __a,
459               _UElements&&... __elements)
460         : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
461         { }
462
463       template<typename _Alloc>
464         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
465         : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
466
467       template<typename _Alloc>
468         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
469         : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
470
471       template<typename _Alloc, typename... _UElements, typename = typename
472                enable_if<sizeof...(_UElements)
473                          == sizeof...(_Elements)>::type>
474         tuple(allocator_arg_t __tag, const _Alloc& __a,
475               const tuple<_UElements...>& __in)
476         : _Inherited(__tag, __a,
477                      static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
478         { }
479
480       template<typename _Alloc, typename... _UElements, typename = typename
481                enable_if<sizeof...(_UElements)
482                          == sizeof...(_Elements)>::type>
483         tuple(allocator_arg_t __tag, const _Alloc& __a,
484               tuple<_UElements...>&& __in)
485         : _Inherited(__tag, __a,
486                      static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
487         { }
488
489       tuple&
490       operator=(const tuple& __in)
491       {
492         static_cast<_Inherited&>(*this) = __in;
493         return *this;
494       }
495
496       tuple&
497       operator=(tuple&& __in)
498       noexcept(is_nothrow_move_assignable<_Inherited>::value)
499       {
500         static_cast<_Inherited&>(*this) = std::move(__in);
501         return *this;
502       }
503
504       template<typename... _UElements, typename = typename
505                enable_if<sizeof...(_UElements)
506                          == sizeof...(_Elements)>::type>
507         tuple&
508         operator=(const tuple<_UElements...>& __in)
509         {
510           static_cast<_Inherited&>(*this) = __in;
511           return *this;
512         }
513
514       template<typename... _UElements, typename = typename
515                enable_if<sizeof...(_UElements)
516                          == sizeof...(_Elements)>::type>
517         tuple&
518         operator=(tuple<_UElements...>&& __in)
519         {
520           static_cast<_Inherited&>(*this) = std::move(__in);
521           return *this;
522         }
523
524       void
525       swap(tuple& __in)
526       noexcept(noexcept(__in._M_swap(__in)))
527       { _Inherited::_M_swap(__in); }
528     };
529
530   // Explicit specialization, zero-element tuple.
531   template<>  
532     class tuple<>
533     {
534     public:
535       void swap(tuple&) noexcept { /* no-op */ }
536     };
537
538   /// Partial specialization, 2-element tuple.
539   /// Includes construction and assignment from a pair.
540   template<typename _T1, typename _T2>
541     class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
542     {
543       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
544
545     public:
546       constexpr tuple()
547       : _Inherited() { }
548
549       explicit
550       constexpr tuple(const _T1& __a1, const _T2& __a2)
551       : _Inherited(__a1, __a2) { }
552
553       template<typename _U1, typename _U2, typename = typename
554                enable_if<__and_<is_convertible<_U1, _T1>,
555                                 is_convertible<_U2, _T2>>::value>::type>
556         explicit
557         constexpr tuple(_U1&& __a1, _U2&& __a2)
558         : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
559
560       constexpr tuple(const tuple&) = default;
561
562       constexpr tuple(tuple&&) = default;
563
564       template<typename _U1, typename _U2, typename = typename
565         enable_if<__and_<is_convertible<const _U1&, _T1>,
566                          is_convertible<const _U2&, _T2>>::value>::type>
567         constexpr tuple(const tuple<_U1, _U2>& __in)
568         : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
569
570       template<typename _U1, typename _U2, typename = typename
571                enable_if<__and_<is_convertible<_U1, _T1>,
572                                 is_convertible<_U2, _T2>>::value>::type>
573         constexpr tuple(tuple<_U1, _U2>&& __in)
574         : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
575
576       template<typename _U1, typename _U2, typename = typename
577         enable_if<__and_<is_convertible<const _U1&, _T1>,
578                          is_convertible<const _U2&, _T2>>::value>::type>
579         constexpr tuple(const pair<_U1, _U2>& __in)
580         : _Inherited(__in.first, __in.second) { }
581
582       template<typename _U1, typename _U2, typename = typename
583                enable_if<__and_<is_convertible<_U1, _T1>,
584                                 is_convertible<_U2, _T2>>::value>::type>
585         constexpr tuple(pair<_U1, _U2>&& __in)
586         : _Inherited(std::forward<_U1>(__in.first),
587                      std::forward<_U2>(__in.second)) { }
588
589       // Allocator-extended constructors.
590
591       template<typename _Alloc>
592         tuple(allocator_arg_t __tag, const _Alloc& __a)
593         : _Inherited(__tag, __a) { }
594
595       template<typename _Alloc>
596         tuple(allocator_arg_t __tag, const _Alloc& __a,
597               const _T1& __a1, const _T2& __a2)
598         : _Inherited(__tag, __a, __a1, __a2) { }
599
600       template<typename _Alloc, typename _U1, typename _U2>
601         tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
602         : _Inherited(__tag, __a, std::forward<_U1>(__a1),
603                      std::forward<_U2>(__a2)) { }
604
605       template<typename _Alloc>
606         tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
607         : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
608
609       template<typename _Alloc>
610         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
611         : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
612
613       template<typename _Alloc, typename _U1, typename _U2>
614         tuple(allocator_arg_t __tag, const _Alloc& __a,
615               const tuple<_U1, _U2>& __in)
616         : _Inherited(__tag, __a,
617                      static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
618         { }
619
620       template<typename _Alloc, typename _U1, typename _U2>
621         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
622         : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
623         { }
624
625       template<typename _Alloc, typename _U1, typename _U2>
626         tuple(allocator_arg_t __tag, const _Alloc& __a,
627               const pair<_U1, _U2>& __in)
628         : _Inherited(__tag, __a, __in.first, __in.second) { }
629
630       template<typename _Alloc, typename _U1, typename _U2>
631         tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
632         : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
633                      std::forward<_U2>(__in.second)) { }
634
635       tuple&
636       operator=(const tuple& __in)
637       {
638         static_cast<_Inherited&>(*this) = __in;
639         return *this;
640       }
641
642       tuple&
643       operator=(tuple&& __in)
644       noexcept(is_nothrow_move_assignable<_Inherited>::value)
645       {
646         static_cast<_Inherited&>(*this) = std::move(__in);
647         return *this;
648       }
649
650       template<typename _U1, typename _U2>
651         tuple&
652         operator=(const tuple<_U1, _U2>& __in)
653         {
654           static_cast<_Inherited&>(*this) = __in;
655           return *this;
656         }
657
658       template<typename _U1, typename _U2>
659         tuple&
660         operator=(tuple<_U1, _U2>&& __in)
661         {
662           static_cast<_Inherited&>(*this) = std::move(__in);
663           return *this;
664         }
665
666       template<typename _U1, typename _U2>
667         tuple&
668         operator=(const pair<_U1, _U2>& __in)
669         {
670           this->_M_head(*this) = __in.first;
671           this->_M_tail(*this)._M_head(*this) = __in.second;
672           return *this;
673         }
674
675       template<typename _U1, typename _U2>
676         tuple&
677         operator=(pair<_U1, _U2>&& __in)
678         {
679           this->_M_head(*this) = std::forward<_U1>(__in.first);
680           this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
681           return *this;
682         }
683
684       void
685       swap(tuple& __in)
686       noexcept(noexcept(__in._M_swap(__in)))
687       { _Inherited::_M_swap(__in); }
688     };
689
690
691   /// Gives the type of the ith element of a given tuple type.
692   template<std::size_t __i, typename _Tp>
693     struct tuple_element;
694
695   /**
696    * Recursive case for tuple_element: strip off the first element in
697    * the tuple and retrieve the (i-1)th element of the remaining tuple.
698    */
699   template<std::size_t __i, typename _Head, typename... _Tail>
700     struct tuple_element<__i, tuple<_Head, _Tail...> >
701     : tuple_element<__i - 1, tuple<_Tail...> > { };
702
703   /**
704    * Basis case for tuple_element: The first element is the one we're seeking.
705    */
706   template<typename _Head, typename... _Tail>
707     struct tuple_element<0, tuple<_Head, _Tail...> >
708     {
709       typedef _Head type;
710     };
711
712   template<std::size_t __i, typename _Tp>
713     struct tuple_element<__i, const _Tp>
714     {
715       typedef typename
716       add_const<typename tuple_element<__i, _Tp>::type>::type type;
717     };
718
719   template<std::size_t __i, typename _Tp>
720     struct tuple_element<__i, volatile _Tp>
721     {
722       typedef typename
723       add_volatile<typename tuple_element<__i, _Tp>::type>::type type;
724     };
725
726   template<std::size_t __i, typename _Tp>
727     struct tuple_element<__i, const volatile _Tp>
728     {
729       typedef typename
730       add_cv<typename tuple_element<__i, _Tp>::type>::type type;
731     };
732
733   /// Finds the size of a given tuple type.
734   template<typename _Tp>
735     struct tuple_size;
736
737   template<typename _Tp>
738     struct tuple_size<const _Tp>
739     : public integral_constant<
740              typename remove_cv<decltype(tuple_size<_Tp>::value)>::type,
741              tuple_size<_Tp>::value> { };
742
743   template<typename _Tp>
744     struct tuple_size<volatile _Tp>
745     : public integral_constant<
746              typename remove_cv<decltype(tuple_size<_Tp>::value)>::type,
747              tuple_size<_Tp>::value> { };
748
749   template<typename _Tp>
750     struct tuple_size<const volatile _Tp>
751     : public integral_constant<
752              typename remove_cv<decltype(tuple_size<_Tp>::value)>::type,
753              tuple_size<_Tp>::value> { };
754
755   /// class tuple_size
756   template<typename... _Elements>
757     struct tuple_size<tuple<_Elements...>>
758     : public integral_constant<std::size_t, sizeof...(_Elements)> { };
759
760   template<std::size_t __i, typename _Head, typename... _Tail>
761     constexpr typename __add_ref<_Head>::type
762     __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
763     { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
764
765   template<std::size_t __i, typename _Head, typename... _Tail>
766     constexpr typename __add_c_ref<_Head>::type
767     __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
768     { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
769
770   // Return a reference (const reference, rvalue reference) to the ith element
771   // of a tuple.  Any const or non-const ref elements are returned with their
772   // original type.
773   template<std::size_t __i, typename... _Elements>
774     constexpr typename __add_ref<
775                       typename tuple_element<__i, tuple<_Elements...>>::type
776                     >::type
777     get(tuple<_Elements...>& __t) noexcept
778     { return __get_helper<__i>(__t); }
779
780   template<std::size_t __i, typename... _Elements>
781     constexpr typename __add_c_ref<
782                       typename tuple_element<__i, tuple<_Elements...>>::type
783                     >::type
784     get(const tuple<_Elements...>& __t) noexcept
785     { return __get_helper<__i>(__t); }
786
787   template<std::size_t __i, typename... _Elements>
788     constexpr typename __add_r_ref<
789                       typename tuple_element<__i, tuple<_Elements...>>::type
790                     >::type
791     get(tuple<_Elements...>&& __t) noexcept
792     { return std::forward<typename tuple_element<__i,
793         tuple<_Elements...>>::type&&>(get<__i>(__t)); }
794
795   // This class helps construct the various comparison operations on tuples
796   template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
797            typename _Tp, typename _Up>
798     struct __tuple_compare;
799
800   template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
801     struct __tuple_compare<0, __i, __j, _Tp, _Up>
802     {
803       static bool 
804       __eq(const _Tp& __t, const _Up& __u)
805       {
806         return (get<__i>(__t) == get<__i>(__u) &&
807                 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
808       }
809      
810       static bool 
811       __less(const _Tp& __t, const _Up& __u)
812       {
813         return ((get<__i>(__t) < get<__i>(__u))
814                 || !(get<__i>(__u) < get<__i>(__t)) &&
815                 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
816       }
817     };
818
819   template<std::size_t __i, typename _Tp, typename _Up>
820     struct __tuple_compare<0, __i, __i, _Tp, _Up>
821     {
822       static bool 
823       __eq(const _Tp&, const _Up&) { return true; }
824      
825       static bool 
826       __less(const _Tp&, const _Up&) { return false; }
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>::__eq(__t, __u));
838     }
839
840   template<typename... _TElements, typename... _UElements>
841     bool
842     operator<(const tuple<_TElements...>& __t,
843               const tuple<_UElements...>& __u)
844     {
845       typedef tuple<_TElements...> _Tp;
846       typedef tuple<_UElements...> _Up;
847       return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
848               0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
849     }
850
851   template<typename... _TElements, typename... _UElements>
852     inline bool
853     operator!=(const tuple<_TElements...>& __t,
854                const tuple<_UElements...>& __u)
855     { return !(__t == __u); }
856
857   template<typename... _TElements, typename... _UElements>
858     inline bool
859     operator>(const tuple<_TElements...>& __t,
860               const tuple<_UElements...>& __u)
861     { return __u < __t; }
862
863   template<typename... _TElements, typename... _UElements>
864     inline bool
865     operator<=(const tuple<_TElements...>& __t,
866                const tuple<_UElements...>& __u)
867     { return !(__u < __t); }
868
869   template<typename... _TElements, typename... _UElements>
870     inline bool
871     operator>=(const tuple<_TElements...>& __t,
872                const tuple<_UElements...>& __u)
873     { return !(__t < __u); }
874
875   // NB: DR 705.
876   template<typename... _Elements>
877     constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
878     make_tuple(_Elements&&... __args)
879     {
880       typedef tuple<typename __decay_and_strip<_Elements>::__type...>
881         __result_type;
882       return __result_type(std::forward<_Elements>(__args)...);
883     }
884
885   template<typename... _Elements>
886     constexpr tuple<_Elements&&...>
887     forward_as_tuple(_Elements&&... __args) noexcept
888     { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
889
890
891   template<typename, std::size_t> struct array;
892
893   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
894     constexpr _Tp& get(array<_Tp, _Nm>&) noexcept;
895
896   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
897     constexpr _Tp&& get(array<_Tp, _Nm>&&) noexcept;
898
899   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
900     constexpr const _Tp& get(const array<_Tp, _Nm>&) noexcept;
901
902   template<typename>
903     struct __is_tuple_like_impl : false_type
904     { };
905
906   template<typename... _Tps>
907     struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
908     { };
909
910   template<typename _T1, typename _T2>
911     struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
912     { };
913
914   template<typename _Tp, std::size_t _Nm>
915     struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
916     { };
917
918   // Internal type trait that allows us to sfinae-protect tuple_cat.
919   template<typename _Tp>
920     struct __is_tuple_like
921     : public __is_tuple_like_impl<typename std::remove_cv
922             <typename std::remove_reference<_Tp>::type>::type>::type
923     { };
924
925   // Stores a tuple of indices.  Also used by bind() to extract the elements
926   // in a tuple. 
927   template<std::size_t... _Indexes>
928     struct _Index_tuple
929     {
930       typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
931     };
932
933   // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
934   template<std::size_t _Num>
935     struct _Build_index_tuple
936     {
937       typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type;
938     };
939
940   template<>
941     struct _Build_index_tuple<0>
942     {
943       typedef _Index_tuple<> __type;
944     };
945
946   template<std::size_t, typename, typename, std::size_t>
947     struct __make_tuple_impl;
948
949   template<std::size_t _Idx, typename _Tuple, typename... _Tp,
950            std::size_t _Nm>
951     struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
952     {
953       typedef typename __make_tuple_impl<_Idx + 1, tuple<_Tp...,
954         typename std::tuple_element<_Idx, _Tuple>::type>, _Tuple, _Nm>::__type
955       __type;
956     };
957
958   template<std::size_t _Nm, typename _Tuple, typename... _Tp>
959     struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
960     {
961       typedef tuple<_Tp...> __type;
962     };
963
964   template<typename _Tuple>
965     struct __do_make_tuple
966     : public __make_tuple_impl<0, tuple<>, _Tuple,
967                                std::tuple_size<_Tuple>::value>
968     { };
969
970   // Returns the std::tuple equivalent of a tuple-like type.
971   template<typename _Tuple>
972     struct __make_tuple
973     : public __do_make_tuple<typename std::remove_cv
974             <typename std::remove_reference<_Tuple>::type>::type>
975     { };
976
977   // Combines several std::tuple's into a single one.
978   template<typename...>
979     struct __combine_tuples;
980
981   template<>
982     struct __combine_tuples<>
983     {
984       typedef tuple<> __type;
985     };
986
987   template<typename... _Ts>
988     struct __combine_tuples<tuple<_Ts...>>
989     {
990       typedef tuple<_Ts...> __type;
991     };
992
993   template<typename... _T1s, typename... _T2s, typename... _Rem>
994     struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
995     {
996       typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
997                                         _Rem...>::__type __type;
998     };
999
1000   // Computes the result type of tuple_cat given a set of tuple-like types.
1001   template<typename... _Tpls>
1002     struct __tuple_cat_result
1003     {
1004       typedef typename __combine_tuples
1005         <typename __make_tuple<_Tpls>::__type...>::__type __type;
1006     };
1007
1008   // Helper to determine the index set for the first tuple-like
1009   // type of a given set.
1010   template<typename...>
1011     struct __make_1st_indices;
1012
1013   template<>
1014     struct __make_1st_indices<>
1015     {
1016       typedef std::_Index_tuple<> __type;
1017     };
1018
1019   template<typename _Tp, typename... _Tpls>
1020     struct __make_1st_indices<_Tp, _Tpls...>
1021     {
1022       typedef typename std::_Build_index_tuple<std::tuple_size<
1023         typename std::remove_reference<_Tp>::type>::value>::__type __type;
1024     };
1025
1026   // Performs the actual concatenation by step-wise expanding tuple-like
1027   // objects into the elements,  which are finally forwarded into the
1028   // result tuple.
1029   template<typename _Ret, typename _Indices, typename... _Tpls>
1030     struct __tuple_concater;
1031
1032   template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1033     struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1034     {
1035       template<typename... _Us>
1036         static constexpr _Ret
1037         _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1038         {
1039           typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1040           typedef __tuple_concater<_Ret, __idx, _Tpls...>      __next;
1041           return __next::_S_do(std::forward<_Tpls>(__tps)...,
1042                                std::forward<_Us>(__us)...,
1043                                std::get<_Is>(std::forward<_Tp>(__tp))...);
1044         }
1045     };
1046
1047   template<typename _Ret>
1048     struct __tuple_concater<_Ret, std::_Index_tuple<>>
1049     {
1050       template<typename... _Us>
1051         static constexpr _Ret
1052         _S_do(_Us&&... __us)
1053         {
1054           return _Ret(std::forward<_Us>(__us)...);
1055         }
1056     };
1057
1058   template<typename... _Tpls, typename = typename
1059            enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1060     constexpr auto
1061     tuple_cat(_Tpls&&... __tpls)
1062     -> typename __tuple_cat_result<_Tpls...>::__type
1063     {
1064       typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1065       typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1066       typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1067       return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1068     }
1069
1070   template<typename... _Elements>
1071     inline tuple<_Elements&...>
1072     tie(_Elements&... __args) noexcept
1073     { return tuple<_Elements&...>(__args...); }
1074
1075   template<typename... _Elements>
1076     inline void 
1077     swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1078     noexcept(noexcept(__x.swap(__y)))
1079     { __x.swap(__y); }
1080
1081   // A class (and instance) which can be used in 'tie' when an element
1082   // of a tuple is not required
1083   struct _Swallow_assign
1084   {
1085     template<class _Tp>
1086       const _Swallow_assign&
1087       operator=(const _Tp&) const
1088       { return *this; }
1089   };
1090
1091   const _Swallow_assign ignore{};
1092
1093   /// Partial specialization for tuples
1094   template<typename... _Types, typename _Alloc>
1095     struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1096
1097   // See stl_pair.h...
1098   template<class _T1, class _T2>
1099     template<typename _Tp, typename... _Args>
1100       inline _Tp
1101       pair<_T1, _T2>::__cons(tuple<_Args...>&& __tuple)
1102       {
1103         typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1104           _Indexes;
1105         return __do_cons<_Tp>(std::move(__tuple), _Indexes());
1106       }
1107
1108   template<class _T1, class _T2>
1109     template<typename _Tp, typename... _Args, std::size_t... _Indexes>
1110       inline _Tp
1111       pair<_T1, _T2>::__do_cons(tuple<_Args...>&& __tuple,
1112                                 const _Index_tuple<_Indexes...>&)
1113       { return _Tp(std::forward<_Args>(get<_Indexes>(__tuple))...); }
1114
1115 _GLIBCXX_END_NAMESPACE_VERSION
1116 } // namespace
1117
1118 #endif // __GXX_EXPERIMENTAL_CXX0X__
1119
1120 #endif // _GLIBCXX_TUPLE