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