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, typename = typename
411 enable_if<__and_<integral_constant<bool, sizeof...(_UElements)
412 == sizeof...(_Elements)>,
413 __all_convertible<__conv_types<_UElements...>,
414 __conv_types<_Elements...>>
417 constexpr tuple(_UElements&&... __elements)
418 : _Inherited(std::forward<_UElements>(__elements)...) { }
420 constexpr tuple(const tuple&) = default;
422 constexpr tuple(tuple&&) = default;
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...>>
430 constexpr tuple(const tuple<_UElements...>& __in)
431 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
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...>>
440 constexpr tuple(tuple<_UElements...>&& __in)
441 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
443 // Allocator-extended constructors.
445 template<typename _Alloc>
446 tuple(allocator_arg_t __tag, const _Alloc& __a)
447 : _Inherited(__tag, __a) { }
449 template<typename _Alloc>
450 tuple(allocator_arg_t __tag, const _Alloc& __a,
451 const _Elements&... __elements)
452 : _Inherited(__tag, __a, __elements...) { }
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)...)
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)) { }
466 template<typename _Alloc>
467 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
468 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
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))
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))
489 operator=(const tuple& __in)
491 static_cast<_Inherited&>(*this) = __in;
496 operator=(tuple&& __in)
497 noexcept(is_nothrow_move_assignable<_Inherited>::value)
499 static_cast<_Inherited&>(*this) = std::move(__in);
503 template<typename... _UElements, typename = typename
504 enable_if<sizeof...(_UElements)
505 == sizeof...(_Elements)>::type>
507 operator=(const tuple<_UElements...>& __in)
509 static_cast<_Inherited&>(*this) = __in;
513 template<typename... _UElements, typename = typename
514 enable_if<sizeof...(_UElements)
515 == sizeof...(_Elements)>::type>
517 operator=(tuple<_UElements...>&& __in)
519 static_cast<_Inherited&>(*this) = std::move(__in);
525 noexcept(noexcept(__in._M_swap(__in)))
526 { _Inherited::_M_swap(__in); }
529 // Explicit specialization, zero-element tuple.
534 void swap(tuple&) noexcept { /* no-op */ }
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>
542 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
549 constexpr tuple(const _T1& __a1, const _T2& __a2)
550 : _Inherited(__a1, __a2) { }
552 template<typename _U1, typename _U2, typename = typename
553 enable_if<__and_<is_convertible<_U1, _T1>,
554 is_convertible<_U2, _T2>>::value>::type>
556 constexpr tuple(_U1&& __a1, _U2&& __a2)
557 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
559 constexpr tuple(const tuple&) = default;
561 constexpr tuple(tuple&&) = default;
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)) { }
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)) { }
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) { }
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)) { }
588 // Allocator-extended constructors.
590 template<typename _Alloc>
591 tuple(allocator_arg_t __tag, const _Alloc& __a)
592 : _Inherited(__tag, __a) { }
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) { }
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)) { }
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)) { }
608 template<typename _Alloc>
609 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
610 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
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))
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))
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) { }
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)) { }
635 operator=(const tuple& __in)
637 static_cast<_Inherited&>(*this) = __in;
642 operator=(tuple&& __in)
643 noexcept(is_nothrow_move_assignable<_Inherited>::value)
645 static_cast<_Inherited&>(*this) = std::move(__in);
649 template<typename _U1, typename _U2>
651 operator=(const tuple<_U1, _U2>& __in)
653 static_cast<_Inherited&>(*this) = __in;
657 template<typename _U1, typename _U2>
659 operator=(tuple<_U1, _U2>&& __in)
661 static_cast<_Inherited&>(*this) = std::move(__in);
665 template<typename _U1, typename _U2>
667 operator=(const pair<_U1, _U2>& __in)
669 this->_M_head(*this) = __in.first;
670 this->_M_tail(*this)._M_head(*this) = __in.second;
674 template<typename _U1, typename _U2>
676 operator=(pair<_U1, _U2>&& __in)
678 this->_M_head(*this) = std::forward<_U1>(__in.first);
679 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
685 noexcept(noexcept(__in._M_swap(__in)))
686 { _Inherited::_M_swap(__in); }
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;
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.
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...> > { };
703 * Basis case for tuple_element: The first element is the one we're seeking.
705 template<typename _Head, typename... _Tail>
706 struct tuple_element<0, tuple<_Head, _Tail...> >
711 template<std::size_t __i, typename _Tp>
712 struct tuple_element<__i, const _Tp>
715 add_const<typename tuple_element<__i, _Tp>::type>::type type;
718 template<std::size_t __i, typename _Tp>
719 struct tuple_element<__i, volatile _Tp>
722 add_volatile<typename tuple_element<__i, _Tp>::type>::type type;
725 template<std::size_t __i, typename _Tp>
726 struct tuple_element<__i, const volatile _Tp>
729 add_cv<typename tuple_element<__i, _Tp>::type>::type type;
732 /// Finds the size of a given tuple type.
733 template<typename _Tp>
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> { };
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> { };
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> { };
755 template<typename... _Elements>
756 struct tuple_size<tuple<_Elements...>>
757 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
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); }
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); }
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
772 template<std::size_t __i, typename... _Elements>
773 constexpr typename __add_ref<
774 typename tuple_element<__i, tuple<_Elements...>>::type
776 get(tuple<_Elements...>& __t) noexcept
777 { return __get_helper<__i>(__t); }
779 template<std::size_t __i, typename... _Elements>
780 constexpr typename __add_c_ref<
781 typename tuple_element<__i, tuple<_Elements...>>::type
783 get(const tuple<_Elements...>& __t) noexcept
784 { return __get_helper<__i>(__t); }
786 template<std::size_t __i, typename... _Elements>
787 constexpr typename __add_r_ref<
788 typename tuple_element<__i, tuple<_Elements...>>::type
790 get(tuple<_Elements...>&& __t) noexcept
791 { return std::forward<typename tuple_element<__i,
792 tuple<_Elements...>>::type&&>(get<__i>(__t)); }
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;
799 template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
800 struct __tuple_compare<0, __i, __j, _Tp, _Up>
803 __eq(const _Tp& __t, const _Up& __u)
805 return (get<__i>(__t) == get<__i>(__u) &&
806 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
810 __less(const _Tp& __t, const _Up& __u)
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));
818 template<std::size_t __i, typename _Tp, typename _Up>
819 struct __tuple_compare<0, __i, __i, _Tp, _Up>
822 __eq(const _Tp&, const _Up&) { return true; }
825 __less(const _Tp&, const _Up&) { return false; }
828 template<typename... _TElements, typename... _UElements>
830 operator==(const tuple<_TElements...>& __t,
831 const tuple<_UElements...>& __u)
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));
839 template<typename... _TElements, typename... _UElements>
841 operator<(const tuple<_TElements...>& __t,
842 const tuple<_UElements...>& __u)
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));
850 template<typename... _TElements, typename... _UElements>
852 operator!=(const tuple<_TElements...>& __t,
853 const tuple<_UElements...>& __u)
854 { return !(__t == __u); }
856 template<typename... _TElements, typename... _UElements>
858 operator>(const tuple<_TElements...>& __t,
859 const tuple<_UElements...>& __u)
860 { return __u < __t; }
862 template<typename... _TElements, typename... _UElements>
864 operator<=(const tuple<_TElements...>& __t,
865 const tuple<_UElements...>& __u)
866 { return !(__u < __t); }
868 template<typename... _TElements, typename... _UElements>
870 operator>=(const tuple<_TElements...>& __t,
871 const tuple<_UElements...>& __u)
872 { return !(__t < __u); }
875 template<typename... _Elements>
876 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
877 make_tuple(_Elements&&... __args)
879 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
881 return __result_type(std::forward<_Elements>(__args)...);
884 template<typename... _Elements>
885 constexpr tuple<_Elements&&...>
886 forward_as_tuple(_Elements&&... __args) noexcept
887 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
890 template<typename, std::size_t> struct array;
892 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
893 constexpr _Tp& get(array<_Tp, _Nm>&) noexcept;
895 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
896 constexpr _Tp&& get(array<_Tp, _Nm>&&) noexcept;
898 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
899 constexpr const _Tp& get(const array<_Tp, _Nm>&) noexcept;
902 struct __is_tuple_like_impl : false_type
905 template<typename... _Tps>
906 struct __is_tuple_like_impl<tuple<_Tps...>> : true_type
909 template<typename _T1, typename _T2>
910 struct __is_tuple_like_impl<pair<_T1, _T2>> : true_type
913 template<typename _Tp, std::size_t _Nm>
914 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
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
924 // Stores a tuple of indices. Also used by bind() to extract the elements
926 template<std::size_t... _Indexes>
929 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
932 // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
933 template<std::size_t _Num>
934 struct _Build_index_tuple
936 typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type;
940 struct _Build_index_tuple<0>
942 typedef _Index_tuple<> __type;
945 template<std::size_t, typename, typename, std::size_t>
946 struct __make_tuple_impl;
948 template<std::size_t _Idx, typename _Tuple, typename... _Tp,
950 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
952 typedef typename __make_tuple_impl<_Idx + 1, tuple<_Tp...,
953 typename std::tuple_element<_Idx, _Tuple>::type>, _Tuple, _Nm>::__type
957 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
958 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
960 typedef tuple<_Tp...> __type;
963 template<typename _Tuple>
964 struct __do_make_tuple
965 : public __make_tuple_impl<0, tuple<>, _Tuple,
966 std::tuple_size<_Tuple>::value>
969 // Returns the std::tuple equivalent of a tuple-like type.
970 template<typename _Tuple>
972 : public __do_make_tuple<typename std::remove_cv
973 <typename std::remove_reference<_Tuple>::type>::type>
976 // Combines several std::tuple's into a single one.
977 template<typename...>
978 struct __combine_tuples;
981 struct __combine_tuples<>
983 typedef tuple<> __type;
986 template<typename... _Ts>
987 struct __combine_tuples<tuple<_Ts...>>
989 typedef tuple<_Ts...> __type;
992 template<typename... _T1s, typename... _T2s, typename... _Rem>
993 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
995 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
996 _Rem...>::__type __type;
999 // Computes the result type of tuple_cat given a set of tuple-like types.
1000 template<typename... _Tpls>
1001 struct __tuple_cat_result
1003 typedef typename __combine_tuples
1004 <typename __make_tuple<_Tpls>::__type...>::__type __type;
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;
1013 struct __make_1st_indices<>
1015 typedef std::_Index_tuple<> __type;
1018 template<typename _Tp, typename... _Tpls>
1019 struct __make_1st_indices<_Tp, _Tpls...>
1021 typedef typename std::_Build_index_tuple<std::tuple_size<
1022 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1025 // Performs the actual concatenation by step-wise expanding tuple-like
1026 // objects into the elements, which are finally forwarded into the
1028 template<typename _Ret, typename _Indices, typename... _Tpls>
1029 struct __tuple_concater;
1031 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1032 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1034 template<typename... _Us>
1035 static constexpr _Ret
1036 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
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))...);
1046 template<typename _Ret>
1047 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1049 template<typename... _Us>
1050 static constexpr _Ret
1051 _S_do(_Us&&... __us)
1053 return _Ret(std::forward<_Us>(__us)...);
1057 template<typename... _Tpls, typename = typename
1058 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1060 tuple_cat(_Tpls&&... __tpls)
1061 -> typename __tuple_cat_result<_Tpls...>::__type
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)...);
1069 template<typename... _Elements>
1070 inline tuple<_Elements&...>
1071 tie(_Elements&... __args) noexcept
1072 { return tuple<_Elements&...>(__args...); }
1074 template<typename... _Elements>
1076 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1077 noexcept(noexcept(__x.swap(__y)))
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
1085 const _Swallow_assign&
1086 operator=(const _Tp&) const
1090 const _Swallow_assign ignore{};
1092 /// Partial specialization for tuples
1093 template<typename... _Types, typename _Alloc>
1094 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1096 // See stl_pair.h...
1097 template<class _T1, class _T2>
1098 template<typename _Tp, typename... _Args>
1100 pair<_T1, _T2>::__cons(tuple<_Args...>&& __tuple)
1102 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1104 return __do_cons<_Tp>(std::move(__tuple), _Indexes());
1107 template<class _T1, class _T2>
1108 template<typename _Tp, typename... _Args, std::size_t... _Indexes>
1110 pair<_T1, _T2>::__do_cons(tuple<_Args...>&& __tuple,
1111 const _Index_tuple<_Indexes...>&)
1112 { return _Tp(std::forward<_Args>(get<_Indexes>(__tuple))...); }
1114 _GLIBCXX_END_NAMESPACE_VERSION
1117 #endif // __GXX_EXPERIMENTAL_CXX0X__
1119 #endif // _GLIBCXX_TUPLE