3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
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)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/tuple
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_TUPLE
30 #define _GLIBCXX_TUPLE 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
39 #include <bits/uses_allocator.h>
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 // Adds a const reference to a non-reference type.
46 template<typename _Tp>
48 { typedef const _Tp& type; };
50 template<typename _Tp>
51 struct __add_c_ref<_Tp&>
52 { typedef _Tp& type; };
54 // Adds a reference to a non-reference type.
55 template<typename _Tp>
57 { typedef _Tp& type; };
59 template<typename _Tp>
60 struct __add_ref<_Tp&>
61 { typedef _Tp& type; };
63 // Adds an rvalue reference to a non-reference type.
64 template<typename _Tp>
66 { typedef _Tp&& type; };
68 template<typename _Tp>
69 struct __add_r_ref<_Tp&>
70 { typedef _Tp& type; };
72 // To work around c++/49225 aka c++/48322.
74 struct __conv_types { };
76 template<typename _Tuple1, typename _Tuple2>
77 struct __one_by_one_convertible
78 : public false_type { };
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 { };
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
92 template<typename _Tuple1, typename _Tuple2>
93 struct __all_convertible;
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 { };
101 template<std::size_t _Idx, typename _Head, bool _IsEmpty>
104 template<std::size_t _Idx, typename _Head>
105 struct _Head_base<_Idx, _Head, true>
108 constexpr _Head_base()
111 constexpr _Head_base(const _Head& __h)
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)) { }
120 _Head_base(__uses_alloc0)
123 template<typename _Alloc>
124 _Head_base(__uses_alloc1<_Alloc> __a)
125 : _Head(allocator_arg, *__a._M_a) { }
127 template<typename _Alloc>
128 _Head_base(__uses_alloc2<_Alloc> __a)
129 : _Head(*__a._M_a) { }
131 template<typename _UHead>
132 _Head_base(__uses_alloc0, _UHead&& __uhead)
133 : _Head(std::forward<_UHead>(__uhead)) { }
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)) { }
139 template<typename _Alloc, typename _UHead>
140 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
141 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
143 static constexpr _Head&
144 _M_head(_Head_base& __b) noexcept { return __b; }
146 static constexpr const _Head&
147 _M_head(const _Head_base& __b) noexcept { return __b; }
150 template<std::size_t _Idx, typename _Head>
151 struct _Head_base<_Idx, _Head, false>
153 constexpr _Head_base()
156 constexpr _Head_base(const _Head& __h)
157 : _M_head_impl(__h) { }
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)) { }
165 _Head_base(__uses_alloc0)
168 template<typename _Alloc>
169 _Head_base(__uses_alloc1<_Alloc> __a)
170 : _M_head_impl(allocator_arg, *__a._M_a) { }
172 template<typename _Alloc>
173 _Head_base(__uses_alloc2<_Alloc> __a)
174 : _M_head_impl(*__a._M_a) { }
176 template<typename _UHead>
177 _Head_base(__uses_alloc0, _UHead&& __uhead)
178 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
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))
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) { }
189 static constexpr _Head&
190 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
192 static constexpr const _Head&
193 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
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
206 template<std::size_t _Idx, typename... _Elements>
210 * Zero-element tuple implementation. This is the basis case for the
211 * inheritance recursion.
213 template<std::size_t _Idx>
214 struct _Tuple_impl<_Idx>
216 template<std::size_t, typename...> friend class _Tuple_impl;
218 _Tuple_impl() = default;
220 template<typename _Alloc>
221 _Tuple_impl(allocator_arg_t, const _Alloc&) { }
223 template<typename _Alloc>
224 _Tuple_impl(allocator_arg_t, const _Alloc&, const _Tuple_impl&) { }
226 template<typename _Alloc>
227 _Tuple_impl(allocator_arg_t, const _Alloc&, _Tuple_impl&&) { }
230 void _M_swap(_Tuple_impl&) noexcept { /* no-op */ }
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).
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>
243 template<std::size_t, typename...> friend class _Tuple_impl;
245 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
246 typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
248 static constexpr _Head&
249 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
251 static constexpr const _Head&
252 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
254 static constexpr _Inherited&
255 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
257 static constexpr const _Inherited&
258 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
260 constexpr _Tuple_impl()
261 : _Inherited(), _Base() { }
264 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
265 : _Inherited(__tail...), _Base(__head) { }
267 template<typename _UHead, typename... _UTail, typename = typename
268 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
270 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
271 : _Inherited(std::forward<_UTail>(__tail)...),
272 _Base(std::forward<_UHead>(__head)) { }
274 constexpr _Tuple_impl(const _Tuple_impl&) = default;
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))) { }
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)) { }
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))) { }
295 template<typename _Alloc>
296 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
297 : _Inherited(__tag, __a),
298 _Base(__use_alloc<_Head>(__a)) { }
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) { }
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)) { }
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)) { }
321 template<typename _Alloc>
322 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
324 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
325 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
326 std::forward<_Head>(_M_head(__in))) { }
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)) { }
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),
343 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
346 operator=(const _Tuple_impl& __in)
348 _M_head(*this) = _M_head(__in);
349 _M_tail(*this) = _M_tail(__in);
354 operator=(_Tuple_impl&& __in)
355 noexcept(__and_<is_nothrow_move_assignable<_Head>,
356 is_nothrow_move_assignable<_Inherited>>::value)
358 _M_head(*this) = std::forward<_Head>(_M_head(__in));
359 _M_tail(*this) = std::move(_M_tail(__in));
363 template<typename... _UElements>
365 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
367 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
368 _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
372 template<typename _UHead, typename... _UTails>
374 operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
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));
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))))
391 swap(_M_head(*this), _M_head(__in));
392 _Inherited::_M_swap(_M_tail(__in));
396 /// Primary class template, tuple
397 template<typename... _Elements>
398 class tuple : public _Tuple_impl<0, _Elements...>
400 typedef _Tuple_impl<0, _Elements...> _Inherited;
407 constexpr tuple(const _Elements&... __elements)
408 : _Inherited(__elements...) { }
410 template<typename... _UElements,
411 typename = typename enable_if<sizeof...(_UElements)
412 == sizeof...(_Elements)>::type,
414 enable_if<__all_convertible<__conv_types<_UElements...>,
415 __conv_types<_Elements...> >::value
418 constexpr tuple(_UElements&&... __elements)
419 : _Inherited(std::forward<_UElements>(__elements)...) { }
421 constexpr tuple(const tuple&) = default;
423 constexpr tuple(tuple&&) = default;
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...>>
431 constexpr tuple(const tuple<_UElements...>& __in)
432 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
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...>>
441 constexpr tuple(tuple<_UElements...>&& __in)
442 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
444 // Allocator-extended constructors.
446 template<typename _Alloc>
447 tuple(allocator_arg_t __tag, const _Alloc& __a)
448 : _Inherited(__tag, __a) { }
450 template<typename _Alloc>
451 tuple(allocator_arg_t __tag, const _Alloc& __a,
452 const _Elements&... __elements)
453 : _Inherited(__tag, __a, __elements...) { }
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)...)
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)) { }
467 template<typename _Alloc>
468 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
469 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
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))
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))
490 operator=(const tuple& __in)
492 static_cast<_Inherited&>(*this) = __in;
497 operator=(tuple&& __in)
498 noexcept(is_nothrow_move_assignable<_Inherited>::value)
500 static_cast<_Inherited&>(*this) = std::move(__in);
504 template<typename... _UElements, typename = typename
505 enable_if<sizeof...(_UElements)
506 == sizeof...(_Elements)>::type>
508 operator=(const tuple<_UElements...>& __in)
510 static_cast<_Inherited&>(*this) = __in;
514 template<typename... _UElements, typename = typename
515 enable_if<sizeof...(_UElements)
516 == sizeof...(_Elements)>::type>
518 operator=(tuple<_UElements...>&& __in)
520 static_cast<_Inherited&>(*this) = std::move(__in);
526 noexcept(noexcept(__in._M_swap(__in)))
527 { _Inherited::_M_swap(__in); }
530 // Explicit specialization, zero-element tuple.
535 void swap(tuple&) noexcept { /* no-op */ }
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>
543 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
550 constexpr tuple(const _T1& __a1, const _T2& __a2)
551 : _Inherited(__a1, __a2) { }
553 template<typename _U1, typename _U2, typename = typename
554 enable_if<__and_<is_convertible<_U1, _T1>,
555 is_convertible<_U2, _T2>>::value>::type>
557 constexpr tuple(_U1&& __a1, _U2&& __a2)
558 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
560 constexpr tuple(const tuple&) = default;
562 constexpr tuple(tuple&&) = default;
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)) { }
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)) { }
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) { }
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)) { }
589 // Allocator-extended constructors.
591 template<typename _Alloc>
592 tuple(allocator_arg_t __tag, const _Alloc& __a)
593 : _Inherited(__tag, __a) { }
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) { }
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)) { }
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)) { }
609 template<typename _Alloc>
610 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
611 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
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))
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))
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) { }
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)) { }
636 operator=(const tuple& __in)
638 static_cast<_Inherited&>(*this) = __in;
643 operator=(tuple&& __in)
644 noexcept(is_nothrow_move_assignable<_Inherited>::value)
646 static_cast<_Inherited&>(*this) = std::move(__in);
650 template<typename _U1, typename _U2>
652 operator=(const tuple<_U1, _U2>& __in)
654 static_cast<_Inherited&>(*this) = __in;
658 template<typename _U1, typename _U2>
660 operator=(tuple<_U1, _U2>&& __in)
662 static_cast<_Inherited&>(*this) = std::move(__in);
666 template<typename _U1, typename _U2>
668 operator=(const pair<_U1, _U2>& __in)
670 this->_M_head(*this) = __in.first;
671 this->_M_tail(*this)._M_head(*this) = __in.second;
675 template<typename _U1, typename _U2>
677 operator=(pair<_U1, _U2>&& __in)
679 this->_M_head(*this) = std::forward<_U1>(__in.first);
680 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
686 noexcept(noexcept(__in._M_swap(__in)))
687 { _Inherited::_M_swap(__in); }
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;
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.
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...> > { };
704 * Basis case for tuple_element: The first element is the one we're seeking.
706 template<typename _Head, typename... _Tail>
707 struct tuple_element<0, tuple<_Head, _Tail...> >
712 template<std::size_t __i, typename _Tp>
713 struct tuple_element<__i, const _Tp>
716 add_const<typename tuple_element<__i, _Tp>::type>::type type;
719 template<std::size_t __i, typename _Tp>
720 struct tuple_element<__i, volatile _Tp>
723 add_volatile<typename tuple_element<__i, _Tp>::type>::type type;
726 template<std::size_t __i, typename _Tp>
727 struct tuple_element<__i, const volatile _Tp>
730 add_cv<typename tuple_element<__i, _Tp>::type>::type type;
733 /// Finds the size of a given tuple type.
734 template<typename _Tp>
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> { };
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> { };
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> { };
756 template<typename... _Elements>
757 struct tuple_size<tuple<_Elements...>>
758 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
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); }
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); }
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
773 template<std::size_t __i, typename... _Elements>
774 constexpr typename __add_ref<
775 typename tuple_element<__i, tuple<_Elements...>>::type
777 get(tuple<_Elements...>& __t) noexcept
778 { return __get_helper<__i>(__t); }
780 template<std::size_t __i, typename... _Elements>
781 constexpr typename __add_c_ref<
782 typename tuple_element<__i, tuple<_Elements...>>::type
784 get(const tuple<_Elements...>& __t) noexcept
785 { return __get_helper<__i>(__t); }
787 template<std::size_t __i, typename... _Elements>
788 constexpr typename __add_r_ref<
789 typename tuple_element<__i, tuple<_Elements...>>::type
791 get(tuple<_Elements...>&& __t) noexcept
792 { return std::forward<typename tuple_element<__i,
793 tuple<_Elements...>>::type&&>(get<__i>(__t)); }
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;
800 template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
801 struct __tuple_compare<0, __i, __j, _Tp, _Up>
804 __eq(const _Tp& __t, const _Up& __u)
806 return (get<__i>(__t) == get<__i>(__u) &&
807 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
811 __less(const _Tp& __t, const _Up& __u)
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));
819 template<std::size_t __i, typename _Tp, typename _Up>
820 struct __tuple_compare<0, __i, __i, _Tp, _Up>
823 __eq(const _Tp&, const _Up&) { return true; }
826 __less(const _Tp&, const _Up&) { return false; }
829 template<typename... _TElements, typename... _UElements>
831 operator==(const tuple<_TElements...>& __t,
832 const tuple<_UElements...>& __u)
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));
840 template<typename... _TElements, typename... _UElements>
842 operator<(const tuple<_TElements...>& __t,
843 const tuple<_UElements...>& __u)
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));
851 template<typename... _TElements, typename... _UElements>
853 operator!=(const tuple<_TElements...>& __t,
854 const tuple<_UElements...>& __u)
855 { return !(__t == __u); }
857 template<typename... _TElements, typename... _UElements>
859 operator>(const tuple<_TElements...>& __t,
860 const tuple<_UElements...>& __u)
861 { return __u < __t; }
863 template<typename... _TElements, typename... _UElements>
865 operator<=(const tuple<_TElements...>& __t,
866 const tuple<_UElements...>& __u)
867 { return !(__u < __t); }
869 template<typename... _TElements, typename... _UElements>
871 operator>=(const tuple<_TElements...>& __t,
872 const tuple<_UElements...>& __u)
873 { return !(__t < __u); }
876 template<typename... _Elements>
877 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
878 make_tuple(_Elements&&... __args)
880 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
882 return __result_type(std::forward<_Elements>(__args)...);
885 template<typename... _Elements>
886 constexpr tuple<_Elements&&...>
887 forward_as_tuple(_Elements&&... __args) noexcept
888 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
891 template<typename, std::size_t> struct array;
893 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
894 constexpr _Tp& get(array<_Tp, _Nm>&) noexcept;
896 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
897 constexpr _Tp&& get(array<_Tp, _Nm>&&) noexcept;
899 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
900 constexpr const _Tp& get(const array<_Tp, _Nm>&) noexcept;
903 struct __is_tuple_like_impl : false_type
906 template<typename... _Tps>
907 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
910 template<typename _T1, typename _T2>
911 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
914 template<typename _Tp, std::size_t _Nm>
915 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
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
925 // Stores a tuple of indices. Also used by bind() to extract the elements
927 template<std::size_t... _Indexes>
930 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
933 // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
934 template<std::size_t _Num>
935 struct _Build_index_tuple
937 typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type;
941 struct _Build_index_tuple<0>
943 typedef _Index_tuple<> __type;
946 template<std::size_t, typename, typename, std::size_t>
947 struct __make_tuple_impl;
949 template<std::size_t _Idx, typename _Tuple, typename... _Tp,
951 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
953 typedef typename __make_tuple_impl<_Idx + 1, tuple<_Tp...,
954 typename std::tuple_element<_Idx, _Tuple>::type>, _Tuple, _Nm>::__type
958 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
959 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
961 typedef tuple<_Tp...> __type;
964 template<typename _Tuple>
965 struct __do_make_tuple
966 : public __make_tuple_impl<0, tuple<>, _Tuple,
967 std::tuple_size<_Tuple>::value>
970 // Returns the std::tuple equivalent of a tuple-like type.
971 template<typename _Tuple>
973 : public __do_make_tuple<typename std::remove_cv
974 <typename std::remove_reference<_Tuple>::type>::type>
977 // Combines several std::tuple's into a single one.
978 template<typename...>
979 struct __combine_tuples;
982 struct __combine_tuples<>
984 typedef tuple<> __type;
987 template<typename... _Ts>
988 struct __combine_tuples<tuple<_Ts...>>
990 typedef tuple<_Ts...> __type;
993 template<typename... _T1s, typename... _T2s, typename... _Rem>
994 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
996 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
997 _Rem...>::__type __type;
1000 // Computes the result type of tuple_cat given a set of tuple-like types.
1001 template<typename... _Tpls>
1002 struct __tuple_cat_result
1004 typedef typename __combine_tuples
1005 <typename __make_tuple<_Tpls>::__type...>::__type __type;
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;
1014 struct __make_1st_indices<>
1016 typedef std::_Index_tuple<> __type;
1019 template<typename _Tp, typename... _Tpls>
1020 struct __make_1st_indices<_Tp, _Tpls...>
1022 typedef typename std::_Build_index_tuple<std::tuple_size<
1023 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1026 // Performs the actual concatenation by step-wise expanding tuple-like
1027 // objects into the elements, which are finally forwarded into the
1029 template<typename _Ret, typename _Indices, typename... _Tpls>
1030 struct __tuple_concater;
1032 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1033 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1035 template<typename... _Us>
1036 static constexpr _Ret
1037 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
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))...);
1047 template<typename _Ret>
1048 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1050 template<typename... _Us>
1051 static constexpr _Ret
1052 _S_do(_Us&&... __us)
1054 return _Ret(std::forward<_Us>(__us)...);
1058 template<typename... _Tpls, typename = typename
1059 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1061 tuple_cat(_Tpls&&... __tpls)
1062 -> typename __tuple_cat_result<_Tpls...>::__type
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)...);
1070 template<typename... _Elements>
1071 inline tuple<_Elements&...>
1072 tie(_Elements&... __args) noexcept
1073 { return tuple<_Elements&...>(__args...); }
1075 template<typename... _Elements>
1077 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1078 noexcept(noexcept(__x.swap(__y)))
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
1086 const _Swallow_assign&
1087 operator=(const _Tp&) const
1091 const _Swallow_assign ignore{};
1093 /// Partial specialization for tuples
1094 template<typename... _Types, typename _Alloc>
1095 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1097 // See stl_pair.h...
1098 template<class _T1, class _T2>
1099 template<typename _Tp, typename... _Args>
1101 pair<_T1, _T2>::__cons(tuple<_Args...>&& __tuple)
1103 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1105 return __do_cons<_Tp>(std::move(__tuple), _Indexes());
1108 template<class _T1, class _T2>
1109 template<typename _Tp, typename... _Args, std::size_t... _Indexes>
1111 pair<_T1, _T2>::__do_cons(tuple<_Args...>&& __tuple,
1112 const _Index_tuple<_Indexes...>&)
1113 { return _Tp(std::forward<_Args>(get<_Indexes>(__tuple))...); }
1115 _GLIBCXX_END_NAMESPACE_VERSION
1118 #endif // __GXX_EXPERIMENTAL_CXX0X__
1120 #endif // _GLIBCXX_TUPLE