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