]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/tuple
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / tuple
CommitLineData
af13a7a6
BK
1// <tuple> -*- C++ -*-
2
8d9254fc 3// Copyright (C) 2007-2020 Free Software Foundation, Inc.
af13a7a6
BK
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
af13a7a6
BK
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
748086b7
JJ
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/>.
af13a7a6
BK
24
25/** @file include/tuple
26 * This is a Standard C++ Library header.
27 */
28
4514bed6
BK
29#ifndef _GLIBCXX_TUPLE
30#define _GLIBCXX_TUPLE 1
af13a7a6
BK
31
32#pragma GCC system_header
33
734f5023 34#if __cplusplus < 201103L
ab65a4c7 35# include <bits/c++0x_warning.h>
57317d2a 36#else
af13a7a6 37
e133ace8 38#include <utility>
0611ce44 39#include <array>
b8214660 40#include <bits/uses_allocator.h>
3e9f67e6 41#include <bits/invoke.h>
e133ace8 42
12ffa228
BK
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
53dc5044 46
c0ffa2ba
BK
47 /**
48 * @addtogroup utilities
49 * @{
50 */
51
373c0095
FD
52 template<typename... _Elements>
53 class tuple;
54
55 template<typename _Tp>
56 struct __is_empty_non_tuple : is_empty<_Tp> { };
57
58 // Using EBO for elements that are tuples causes ambiguous base errors.
59 template<typename _El0, typename... _El>
60 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
61
62 // Use the Empty Base-class Optimization for empty, non-final types.
63 template<typename _Tp>
64 using __empty_not_final
65 = typename conditional<__is_final(_Tp), false_type,
66 __is_empty_non_tuple<_Tp>>::type;
67
68 template<std::size_t _Idx, typename _Head,
69 bool = __empty_not_final<_Head>::value>
ba60f6f9
PC
70 struct _Head_base;
71
740508be 72 template<std::size_t _Idx, typename _Head>
894d0b15
CF
73 struct _Head_base<_Idx, _Head, true>
74 : public _Head
75 {
0e6ac87e 76 constexpr _Head_base()
894d0b15
CF
77 : _Head() { }
78
094a14ef 79 constexpr _Head_base(const _Head& __h)
894d0b15
CF
80 : _Head(__h) { }
81
f4081d13
JW
82 constexpr _Head_base(const _Head_base&) = default;
83 constexpr _Head_base(_Head_base&&) = default;
84
85 template<typename _UHead>
2c4caf0a 86 constexpr _Head_base(_UHead&& __h)
fe960d92 87 : _Head(std::forward<_UHead>(__h)) { }
894d0b15 88
f4081d13 89 _Head_base(allocator_arg_t, __uses_alloc0)
b8214660
JW
90 : _Head() { }
91
92 template<typename _Alloc>
f4081d13 93 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
b8214660
JW
94 : _Head(allocator_arg, *__a._M_a) { }
95
96 template<typename _Alloc>
f4081d13 97 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
b8214660
JW
98 : _Head(*__a._M_a) { }
99
100 template<typename _UHead>
101 _Head_base(__uses_alloc0, _UHead&& __uhead)
102 : _Head(std::forward<_UHead>(__uhead)) { }
103
104 template<typename _Alloc, typename _UHead>
105 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
106 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
107
108 template<typename _Alloc, typename _UHead>
109 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
110 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
111
9480716c
DK
112 static constexpr _Head&
113 _M_head(_Head_base& __b) noexcept { return __b; }
2c4caf0a 114
9480716c
DK
115 static constexpr const _Head&
116 _M_head(const _Head_base& __b) noexcept { return __b; }
894d0b15
CF
117 };
118
740508be 119 template<std::size_t _Idx, typename _Head>
894d0b15
CF
120 struct _Head_base<_Idx, _Head, false>
121 {
0e6ac87e 122 constexpr _Head_base()
894d0b15
CF
123 : _M_head_impl() { }
124
094a14ef 125 constexpr _Head_base(const _Head& __h)
894d0b15
CF
126 : _M_head_impl(__h) { }
127
f4081d13
JW
128 constexpr _Head_base(const _Head_base&) = default;
129 constexpr _Head_base(_Head_base&&) = default;
130
131 template<typename _UHead>
2c4caf0a 132 constexpr _Head_base(_UHead&& __h)
fe960d92 133 : _M_head_impl(std::forward<_UHead>(__h)) { }
894d0b15 134
6d1402f0 135 _GLIBCXX20_CONSTEXPR
f4081d13 136 _Head_base(allocator_arg_t, __uses_alloc0)
b8214660
JW
137 : _M_head_impl() { }
138
139 template<typename _Alloc>
f4081d13 140 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
b8214660
JW
141 : _M_head_impl(allocator_arg, *__a._M_a) { }
142
143 template<typename _Alloc>
f4081d13 144 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
b8214660
JW
145 : _M_head_impl(*__a._M_a) { }
146
147 template<typename _UHead>
6d1402f0 148 _GLIBCXX20_CONSTEXPR
b8214660
JW
149 _Head_base(__uses_alloc0, _UHead&& __uhead)
150 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
151
152 template<typename _Alloc, typename _UHead>
153 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
154 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
155 { }
156
157 template<typename _Alloc, typename _UHead>
158 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
159 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
160
9480716c
DK
161 static constexpr _Head&
162 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
2c4caf0a 163
9480716c
DK
164 static constexpr const _Head&
165 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
894d0b15 166
9480716c 167 _Head _M_head_impl;
894d0b15
CF
168 };
169
170 /**
894d0b15
CF
171 * Contains the actual implementation of the @c tuple template, stored
172 * as a recursive inheritance hierarchy from the first element (most
173 * derived class) to the last (least derived class). The @c Idx
174 * parameter gives the 0-based index of the element stored at this
175 * point in the hierarchy; we use it to implement a constant-time
176 * get() operation.
894d0b15 177 */
740508be 178 template<std::size_t _Idx, typename... _Elements>
33ac58d5 179 struct _Tuple_impl;
894d0b15 180
894d0b15 181 /**
894d0b15
CF
182 * Recursive tuple implementation. Here we store the @c Head element
183 * and derive from a @c Tuple_impl containing the remaining elements
184 * (which contains the @c Tail).
894d0b15 185 */
740508be 186 template<std::size_t _Idx, typename _Head, typename... _Tail>
894d0b15
CF
187 struct _Tuple_impl<_Idx, _Head, _Tail...>
188 : public _Tuple_impl<_Idx + 1, _Tail...>,
373c0095 189 private _Head_base<_Idx, _Head>
894d0b15 190 {
1aa1114b
PC
191 template<std::size_t, typename...> friend class _Tuple_impl;
192
894d0b15 193 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
373c0095 194 typedef _Head_base<_Idx, _Head> _Base;
894d0b15 195
33ac58d5 196 static constexpr _Head&
9480716c 197 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
2c4caf0a 198
9480716c
DK
199 static constexpr const _Head&
200 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
894d0b15 201
9480716c
DK
202 static constexpr _Inherited&
203 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
2c4caf0a 204
9480716c
DK
205 static constexpr const _Inherited&
206 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
894d0b15 207
0e6ac87e 208 constexpr _Tuple_impl()
894d0b15
CF
209 : _Inherited(), _Base() { }
210
33ac58d5 211 explicit
094a14ef 212 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
894d0b15
CF
213 : _Inherited(__tail...), _Base(__head) { }
214
b8214660 215 template<typename _UHead, typename... _UTail, typename = typename
33ac58d5 216 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
ba60f6f9 217 explicit
2c4caf0a 218 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
b5b5e640
PC
219 : _Inherited(std::forward<_UTail>(__tail)...),
220 _Base(std::forward<_UHead>(__head)) { }
894d0b15 221
094a14ef 222 constexpr _Tuple_impl(const _Tuple_impl&) = default;
894d0b15 223
a2c5e1ae
JW
224 // _GLIBCXX_RESOLVE_LIB_DEFECTS
225 // 2729. Missing SFINAE on std::pair::operator=
226 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
227
fae3f459 228 constexpr
3fe2ddae
VV
229 _Tuple_impl(_Tuple_impl&& __in)
230 noexcept(__and_<is_nothrow_move_constructible<_Head>,
dd7b175e 231 is_nothrow_move_constructible<_Inherited>>::value)
33ac58d5 232 : _Inherited(std::move(_M_tail(__in))),
9480716c 233 _Base(std::forward<_Head>(_M_head(__in))) { }
ba60f6f9 234
894d0b15 235 template<typename... _UElements>
a7d0c94e 236 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
9480716c
DK
237 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
238 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
ba60f6f9 239
78a869ec 240 template<typename _UHead, typename... _UTails>
3fe2ddae 241 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
fae3f459
PC
242 : _Inherited(std::move
243 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
9480716c
DK
244 _Base(std::forward<_UHead>
245 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
894d0b15 246
b8214660 247 template<typename _Alloc>
6d1402f0 248 _GLIBCXX20_CONSTEXPR
b8214660
JW
249 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
250 : _Inherited(__tag, __a),
f4081d13 251 _Base(__tag, __use_alloc<_Head>(__a)) { }
b8214660
JW
252
253 template<typename _Alloc>
254 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
dd7b175e 255 const _Head& __head, const _Tail&... __tail)
b8214660
JW
256 : _Inherited(__tag, __a, __tail...),
257 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
258
259 template<typename _Alloc, typename _UHead, typename... _UTail,
dd7b175e
PC
260 typename = typename enable_if<sizeof...(_Tail)
261 == sizeof...(_UTail)>::type>
6d1402f0 262 _GLIBCXX20_CONSTEXPR
b8214660
JW
263 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
264 _UHead&& __head, _UTail&&... __tail)
265 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
266 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
267 std::forward<_UHead>(__head)) { }
268
269 template<typename _Alloc>
6d1402f0 270 _GLIBCXX20_CONSTEXPR
b8214660
JW
271 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
272 const _Tuple_impl& __in)
33ac58d5 273 : _Inherited(__tag, __a, _M_tail(__in)),
9480716c 274 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
b8214660
JW
275
276 template<typename _Alloc>
6d1402f0 277 _GLIBCXX20_CONSTEXPR
b8214660
JW
278 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
279 _Tuple_impl&& __in)
33ac58d5 280 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
b8214660 281 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
9480716c 282 std::forward<_Head>(_M_head(__in))) { }
b8214660
JW
283
284 template<typename _Alloc, typename... _UElements>
6d1402f0 285 _GLIBCXX20_CONSTEXPR
b8214660
JW
286 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
287 const _Tuple_impl<_Idx, _UElements...>& __in)
9480716c
DK
288 : _Inherited(__tag, __a,
289 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
290 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
291 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
b8214660
JW
292
293 template<typename _Alloc, typename _UHead, typename... _UTails>
6d1402f0 294 _GLIBCXX20_CONSTEXPR
b8214660
JW
295 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
296 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
9480716c
DK
297 : _Inherited(__tag, __a, std::move
298 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
b8214660 299 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
9480716c
DK
300 std::forward<_UHead>
301 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
b8214660 302
ba60f6f9 303 template<typename... _UElements>
6d1402f0 304 _GLIBCXX20_CONSTEXPR
a2c5e1ae
JW
305 void
306 _M_assign(const _Tuple_impl<_Idx, _UElements...>& __in)
ba60f6f9 307 {
9480716c 308 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
a2c5e1ae
JW
309 _M_tail(*this)._M_assign(
310 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in));
ba60f6f9
PC
311 }
312
78a869ec 313 template<typename _UHead, typename... _UTails>
6d1402f0 314 _GLIBCXX20_CONSTEXPR
a2c5e1ae
JW
315 void
316 _M_assign(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
ba60f6f9 317 {
9480716c
DK
318 _M_head(*this) = std::forward<_UHead>
319 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
a2c5e1ae
JW
320 _M_tail(*this)._M_assign(
321 std::move(_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in)));
ba60f6f9 322 }
3e93b275 323
fe960d92 324 protected:
6d1402f0 325 _GLIBCXX20_CONSTEXPR
3e93b275 326 void
1aa1114b 327 _M_swap(_Tuple_impl& __in)
3e93b275 328 {
1aa1114b 329 using std::swap;
9480716c
DK
330 swap(_M_head(*this), _M_head(__in));
331 _Inherited::_M_swap(_M_tail(__in));
3e93b275 332 }
894d0b15
CF
333 };
334
3ecec1ef
JW
335 // Basis case of inheritance recursion.
336 template<std::size_t _Idx, typename _Head>
337 struct _Tuple_impl<_Idx, _Head>
373c0095 338 : private _Head_base<_Idx, _Head>
3ecec1ef
JW
339 {
340 template<std::size_t, typename...> friend class _Tuple_impl;
341
373c0095 342 typedef _Head_base<_Idx, _Head> _Base;
3ecec1ef
JW
343
344 static constexpr _Head&
345 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
346
347 static constexpr const _Head&
348 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
349
350 constexpr _Tuple_impl()
351 : _Base() { }
352
353 explicit
354 constexpr _Tuple_impl(const _Head& __head)
355 : _Base(__head) { }
356
3fe2ddae 357 template<typename _UHead>
3ecec1ef
JW
358 explicit
359 constexpr _Tuple_impl(_UHead&& __head)
360 : _Base(std::forward<_UHead>(__head)) { }
361
362 constexpr _Tuple_impl(const _Tuple_impl&) = default;
363
a2c5e1ae
JW
364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
365 // 2729. Missing SFINAE on std::pair::operator=
366 _Tuple_impl& operator=(const _Tuple_impl&) = delete;
367
3ecec1ef 368 constexpr
3fe2ddae 369 _Tuple_impl(_Tuple_impl&& __in)
3ecec1ef
JW
370 noexcept(is_nothrow_move_constructible<_Head>::value)
371 : _Base(std::forward<_Head>(_M_head(__in))) { }
372
3fe2ddae 373 template<typename _UHead>
3ecec1ef
JW
374 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
375 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
376
3fe2ddae 377 template<typename _UHead>
3ecec1ef
JW
378 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
379 : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
380 { }
381
382 template<typename _Alloc>
6d1402f0 383 _GLIBCXX20_CONSTEXPR
3ecec1ef
JW
384 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
385 : _Base(__tag, __use_alloc<_Head>(__a)) { }
386
387 template<typename _Alloc>
388 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
389 const _Head& __head)
390 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
391
392 template<typename _Alloc, typename _UHead>
6d1402f0 393 _GLIBCXX20_CONSTEXPR
3ecec1ef
JW
394 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
395 _UHead&& __head)
396 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
397 std::forward<_UHead>(__head)) { }
398
399 template<typename _Alloc>
6d1402f0 400 _GLIBCXX20_CONSTEXPR
3ecec1ef
JW
401 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
402 const _Tuple_impl& __in)
403 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
404
405 template<typename _Alloc>
6d1402f0 406 _GLIBCXX20_CONSTEXPR
3ecec1ef
JW
407 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
408 _Tuple_impl&& __in)
409 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
410 std::forward<_Head>(_M_head(__in))) { }
411
412 template<typename _Alloc, typename _UHead>
6d1402f0 413 _GLIBCXX20_CONSTEXPR
3ecec1ef
JW
414 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
415 const _Tuple_impl<_Idx, _UHead>& __in)
416 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
417 _Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
418
419 template<typename _Alloc, typename _UHead>
6d1402f0 420 _GLIBCXX20_CONSTEXPR
3ecec1ef
JW
421 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
422 _Tuple_impl<_Idx, _UHead>&& __in)
423 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
424 std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
425 { }
426
3ecec1ef 427 template<typename _UHead>
6d1402f0 428 _GLIBCXX20_CONSTEXPR
a2c5e1ae
JW
429 void
430 _M_assign(const _Tuple_impl<_Idx, _UHead>& __in)
3ecec1ef
JW
431 {
432 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
3ecec1ef
JW
433 }
434
435 template<typename _UHead>
6d1402f0 436 _GLIBCXX20_CONSTEXPR
a2c5e1ae
JW
437 void
438 _M_assign(_Tuple_impl<_Idx, _UHead>&& __in)
3ecec1ef
JW
439 {
440 _M_head(*this)
441 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
3ecec1ef
JW
442 }
443
444 protected:
6d1402f0 445 _GLIBCXX20_CONSTEXPR
3ecec1ef
JW
446 void
447 _M_swap(_Tuple_impl& __in)
3ecec1ef
JW
448 {
449 using std::swap;
450 swap(_M_head(*this), _M_head(__in));
451 }
452 };
453
bf7818bf
VV
454 // Concept utility functions, reused in conditionally-explicit
455 // constructors.
d355635e
JW
456 template<bool, typename... _Types>
457 struct _TupleConstraints
bf7818bf 458 {
d355635e
JW
459 // Constraint for a non-explicit constructor.
460 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
461 // and every Ui is implicitly convertible to Ti.
462 template<typename... _UTypes>
463 static constexpr bool __is_implicitly_constructible()
464 {
465 return __and_<is_constructible<_Types, _UTypes>...,
466 is_convertible<_UTypes, _Types>...
467 >::value;
468 }
bf7818bf 469
d355635e
JW
470 // Constraint for a non-explicit constructor.
471 // True iff each Ti in _Types... can be constructed from Ui in _UTypes...
472 // but not every Ui is implicitly convertible to Ti.
473 template<typename... _UTypes>
474 static constexpr bool __is_explicitly_constructible()
475 {
476 return __and_<is_constructible<_Types, _UTypes>...,
477 __not_<__and_<is_convertible<_UTypes, _Types>...>>
478 >::value;
479 }
bf7818bf 480
d355635e
JW
481 static constexpr bool __is_implicitly_default_constructible()
482 {
483 return __and_<std::__is_implicitly_default_constructible<_Types>...
484 >::value;
485 }
bf7818bf 486
d355635e
JW
487 static constexpr bool __is_explicitly_default_constructible()
488 {
489 return __and_<is_default_constructible<_Types>...,
490 __not_<__and_<
491 std::__is_implicitly_default_constructible<_Types>...>
492 >>::value;
493 }
494 };
5e2f2cd5 495
d355635e
JW
496 // Partial specialization used when a required precondition isn't met,
497 // e.g. when sizeof...(_Types) != sizeof...(_UTypes).
498 template<typename... _Types>
499 struct _TupleConstraints<false, _Types...>
5e2f2cd5 500 {
d355635e
JW
501 template<typename... _UTypes>
502 static constexpr bool __is_implicitly_constructible()
503 { return false; }
a2c5e1ae 504
d355635e
JW
505 template<typename... _UTypes>
506 static constexpr bool __is_explicitly_constructible()
507 { return false; }
508 };
bf7818bf 509
2c4caf0a 510 /// Primary class template, tuple
33ac58d5 511 template<typename... _Elements>
a2c5e1ae 512 class tuple : public _Tuple_impl<0, _Elements...>
894d0b15
CF
513 {
514 typedef _Tuple_impl<0, _Elements...> _Inherited;
515
d355635e
JW
516 template<bool _Cond>
517 using _TCC = _TupleConstraints<_Cond, _Elements...>;
518
519 // Constraint for non-explicit default constructor
520 template<bool _Dummy>
521 using _ImplicitDefaultCtor = __enable_if_t<
522 _TCC<_Dummy>::__is_implicitly_default_constructible(),
523 bool>;
524
525 // Constraint for explicit default constructor
526 template<bool _Dummy>
527 using _ExplicitDefaultCtor = __enable_if_t<
528 _TCC<_Dummy>::__is_explicitly_default_constructible(),
529 bool>;
530
531 // Constraint for non-explicit constructors
532 template<bool _Cond, typename... _Args>
533 using _ImplicitCtor = __enable_if_t<
534 _TCC<_Cond>::template __is_implicitly_constructible<_Args...>(),
535 bool>;
536
537 // Constraint for non-explicit constructors
538 template<bool _Cond, typename... _Args>
539 using _ExplicitCtor = __enable_if_t<
540 _TCC<_Cond>::template __is_explicitly_constructible<_Args...>(),
541 bool>;
bf7818bf 542
478490f6
JW
543 template<typename... _UElements>
544 static constexpr
545 __enable_if_t<sizeof...(_UElements) == sizeof...(_Elements), bool>
546 __assignable()
547 { return __and_<is_assignable<_Elements&, _UElements>...>::value; }
548
d355635e 549 // Condition for noexcept-specifier of an assignment operator.
478490f6
JW
550 template<typename... _UElements>
551 static constexpr bool __nothrow_assignable()
552 {
553 return
554 __and_<is_nothrow_assignable<_Elements&, _UElements>...>::value;
555 }
d355635e
JW
556
557 // Condition for noexcept-specifier of a constructor.
ec087ba3
NDR
558 template<typename... _UElements>
559 static constexpr bool __nothrow_constructible()
560 {
561 return
562 __and_<is_nothrow_constructible<_Elements, _UElements>...>::value;
563 }
478490f6 564
d355635e
JW
565 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) == 1.
566 template<typename _Up>
567 static constexpr bool __valid_args()
568 {
569 return sizeof...(_Elements) == 1
570 && !is_same<tuple, __remove_cvref_t<_Up>>::value;
571 }
894d0b15 572
d355635e
JW
573 // Constraint for tuple(_UTypes&&...) where sizeof...(_UTypes) > 1.
574 template<typename, typename, typename... _Tail>
575 static constexpr bool __valid_args()
576 { return (sizeof...(_Tail) + 2) == sizeof...(_Elements); }
577
578 /* Constraint for constructors with a tuple<UTypes...> parameter ensures
579 * that the constructor is only viable when it would not interfere with
580 * tuple(UTypes&&...) or tuple(const tuple&) or tuple(tuple&&).
581 * Such constructors are only viable if:
582 * either sizeof...(Types) != 1,
583 * or (when Types... expands to T and UTypes... expands to U)
584 * is_convertible_v<TUPLE, T>, is_constructible_v<T, TUPLE>,
585 * and is_same_v<T, U> are all false.
586 */
587 template<typename _Tuple, typename = tuple,
588 typename = __remove_cvref_t<_Tuple>>
589 struct _UseOtherCtor
590 : false_type
591 { };
592 // If TUPLE is convertible to the single element in *this,
593 // then TUPLE should match tuple(UTypes&&...) instead.
594 template<typename _Tuple, typename _Tp, typename _Up>
595 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Up>>
596 : __or_<is_convertible<_Tuple, _Tp>, is_constructible<_Tp, _Tuple>>
597 { };
598 // If TUPLE and *this each have a single element of the same type,
599 // then TUPLE should match a copy/move constructor instead.
600 template<typename _Tuple, typename _Tp>
601 struct _UseOtherCtor<_Tuple, tuple<_Tp>, tuple<_Tp>>
602 : true_type
603 { };
604
605 // Return true iff sizeof...(Types) == 1 && tuple_size_v<TUPLE> == 1
606 // and the single element in Types can be initialized from TUPLE,
607 // or is the same type as tuple_element_t<0, TUPLE>.
608 template<typename _Tuple>
609 static constexpr bool __use_other_ctor()
610 { return _UseOtherCtor<_Tuple>::value; }
bf7818bf 611
d355635e 612 public:
bf7818bf 613 template<typename _Dummy = void,
d355635e
JW
614 _ImplicitDefaultCtor<is_void<_Dummy>::value> = true>
615 constexpr
616 tuple()
617 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
618 : _Inherited() { }
894d0b15 619
bf7818bf 620 template<typename _Dummy = void,
d355635e
JW
621 _ExplicitDefaultCtor<is_void<_Dummy>::value> = false>
622 explicit constexpr
623 tuple()
624 noexcept(__and_<is_nothrow_default_constructible<_Elements>...>::value)
625 : _Inherited() { }
626
627 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
628 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
629 constexpr
630 tuple(const _Elements&... __elements)
631 noexcept(__nothrow_constructible<const _Elements&...>())
632 : _Inherited(__elements...) { }
633
634 template<bool _NotEmpty = (sizeof...(_Elements) >= 1),
635 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
636 explicit constexpr
637 tuple(const _Elements&... __elements)
638 noexcept(__nothrow_constructible<const _Elements&...>())
639 : _Inherited(__elements...) { }
640
641 template<typename... _UElements,
642 bool _Valid = __valid_args<_UElements...>(),
643 _ImplicitCtor<_Valid, _UElements...> = true>
644 constexpr
645 tuple(_UElements&&... __elements)
646 noexcept(__nothrow_constructible<_UElements...>())
647 : _Inherited(std::forward<_UElements>(__elements)...) { }
648
649 template<typename... _UElements,
650 bool _Valid = __valid_args<_UElements...>(),
651 _ExplicitCtor<_Valid, _UElements...> = false>
652 explicit constexpr
653 tuple(_UElements&&... __elements)
654 noexcept(__nothrow_constructible<_UElements...>())
b5b5e640 655 : _Inherited(std::forward<_UElements>(__elements)...) { }
894d0b15 656
094a14ef 657 constexpr tuple(const tuple&) = default;
2c4caf0a 658
33ac58d5 659 constexpr tuple(tuple&&) = default;
ba60f6f9 660
d355635e
JW
661 template<typename... _UElements,
662 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
663 && !__use_other_ctor<const tuple<_UElements...>&>(),
664 _ImplicitCtor<_Valid, const _UElements&...> = true>
665 constexpr
666 tuple(const tuple<_UElements...>& __in)
667 noexcept(__nothrow_constructible<const _UElements&...>())
668 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
669 { }
670
671 template<typename... _UElements,
672 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
673 && !__use_other_ctor<const tuple<_UElements...>&>(),
674 _ExplicitCtor<_Valid, const _UElements&...> = false>
675 explicit constexpr
676 tuple(const tuple<_UElements...>& __in)
677 noexcept(__nothrow_constructible<const _UElements&...>())
678 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
679 { }
680
681 template<typename... _UElements,
682 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
683 && !__use_other_ctor<tuple<_UElements...>&&>(),
684 _ImplicitCtor<_Valid, _UElements...> = true>
685 constexpr
686 tuple(tuple<_UElements...>&& __in)
687 noexcept(__nothrow_constructible<_UElements...>())
688 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
689
690 template<typename... _UElements,
691 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
692 && !__use_other_ctor<tuple<_UElements...>&&>(),
693 _ExplicitCtor<_Valid, _UElements...> = false>
694 explicit constexpr
695 tuple(tuple<_UElements...>&& __in)
696 noexcept(__nothrow_constructible<_UElements...>())
697 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
bf7818bf 698
2c4caf0a 699 // Allocator-extended constructors.
b8214660 700
d355635e
JW
701 template<typename _Alloc,
702 _ImplicitDefaultCtor<is_object<_Alloc>::value> = true>
6d1402f0 703 _GLIBCXX20_CONSTEXPR
b8214660
JW
704 tuple(allocator_arg_t __tag, const _Alloc& __a)
705 : _Inherited(__tag, __a) { }
706
d355635e
JW
707 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
708 _ImplicitCtor<_NotEmpty, const _Elements&...> = true>
6d1402f0 709 _GLIBCXX20_CONSTEXPR
b8214660
JW
710 tuple(allocator_arg_t __tag, const _Alloc& __a,
711 const _Elements&... __elements)
712 : _Inherited(__tag, __a, __elements...) { }
713
d355635e
JW
714 template<typename _Alloc, bool _NotEmpty = (sizeof...(_Elements) >= 1),
715 _ExplicitCtor<_NotEmpty, const _Elements&...> = false>
6d1402f0 716 _GLIBCXX20_CONSTEXPR
d355635e
JW
717 explicit
718 tuple(allocator_arg_t __tag, const _Alloc& __a,
719 const _Elements&... __elements)
bf7818bf
VV
720 : _Inherited(__tag, __a, __elements...) { }
721
d355635e
JW
722 template<typename _Alloc, typename... _UElements,
723 bool _Valid = __valid_args<_UElements...>(),
724 _ImplicitCtor<_Valid, _UElements...> = true>
6d1402f0 725 _GLIBCXX20_CONSTEXPR
b8214660
JW
726 tuple(allocator_arg_t __tag, const _Alloc& __a,
727 _UElements&&... __elements)
728 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
d355635e
JW
729 { }
730
731 template<typename _Alloc, typename... _UElements,
732 bool _Valid = __valid_args<_UElements...>(),
733 _ExplicitCtor<_Valid, _UElements...> = false>
6d1402f0 734 _GLIBCXX20_CONSTEXPR
d355635e
JW
735 explicit
736 tuple(allocator_arg_t __tag, const _Alloc& __a,
bf7818bf
VV
737 _UElements&&... __elements)
738 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
d355635e 739 { }
bf7818bf 740
b8214660 741 template<typename _Alloc>
6d1402f0 742 _GLIBCXX20_CONSTEXPR
b8214660
JW
743 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
744 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
745
746 template<typename _Alloc>
6d1402f0 747 _GLIBCXX20_CONSTEXPR
b8214660
JW
748 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
749 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
750
d355635e
JW
751 template<typename _Alloc, typename... _UElements,
752 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
753 && !__use_other_ctor<const tuple<_UElements...>&>(),
754 _ImplicitCtor<_Valid, const _UElements&...> = true>
6d1402f0 755 _GLIBCXX20_CONSTEXPR
b8214660
JW
756 tuple(allocator_arg_t __tag, const _Alloc& __a,
757 const tuple<_UElements...>& __in)
758 : _Inherited(__tag, __a,
759 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
760 { }
761
d355635e
JW
762 template<typename _Alloc, typename... _UElements,
763 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
764 && !__use_other_ctor<const tuple<_UElements...>&>(),
765 _ExplicitCtor<_Valid, const _UElements&...> = false>
6d1402f0 766 _GLIBCXX20_CONSTEXPR
d355635e
JW
767 explicit
768 tuple(allocator_arg_t __tag, const _Alloc& __a,
bf7818bf
VV
769 const tuple<_UElements...>& __in)
770 : _Inherited(__tag, __a,
771 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
772 { }
773
d355635e
JW
774 template<typename _Alloc, typename... _UElements,
775 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
776 && !__use_other_ctor<tuple<_UElements...>&&>(),
777 _ImplicitCtor<_Valid, _UElements...> = true>
6d1402f0 778 _GLIBCXX20_CONSTEXPR
b8214660
JW
779 tuple(allocator_arg_t __tag, const _Alloc& __a,
780 tuple<_UElements...>&& __in)
781 : _Inherited(__tag, __a,
782 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
783 { }
784
d355635e
JW
785 template<typename _Alloc, typename... _UElements,
786 bool _Valid = (sizeof...(_Elements) == sizeof...(_UElements))
787 && !__use_other_ctor<tuple<_UElements...>&&>(),
788 _ExplicitCtor<_Valid, _UElements...> = false>
6d1402f0 789 _GLIBCXX20_CONSTEXPR
d355635e
JW
790 explicit
791 tuple(allocator_arg_t __tag, const _Alloc& __a,
bf7818bf
VV
792 tuple<_UElements...>&& __in)
793 : _Inherited(__tag, __a,
794 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
795 { }
796
a2c5e1ae
JW
797 // tuple assignment
798
6d1402f0 799 _GLIBCXX20_CONSTEXPR
894d0b15 800 tuple&
478490f6
JW
801 operator=(typename conditional<__assignable<const _Elements&...>(),
802 const tuple&,
a73d2fa8 803 const __nonesuch&>::type __in)
478490f6 804 noexcept(__nothrow_assignable<const _Elements&...>())
894d0b15 805 {
a2c5e1ae 806 this->_M_assign(__in);
894d0b15
CF
807 return *this;
808 }
ba60f6f9 809
6d1402f0 810 _GLIBCXX20_CONSTEXPR
ba60f6f9 811 tuple&
478490f6
JW
812 operator=(typename conditional<__assignable<_Elements...>(),
813 tuple&&,
a73d2fa8 814 __nonesuch&&>::type __in)
478490f6 815 noexcept(__nothrow_assignable<_Elements...>())
ba60f6f9 816 {
a2c5e1ae 817 this->_M_assign(std::move(__in));
ba60f6f9
PC
818 return *this;
819 }
820
ca9e949f 821 template<typename... _UElements>
6d1402f0 822 _GLIBCXX20_CONSTEXPR
478490f6
JW
823 __enable_if_t<__assignable<const _UElements&...>(), tuple&>
824 operator=(const tuple<_UElements...>& __in)
825 noexcept(__nothrow_assignable<const _UElements&...>())
826 {
a2c5e1ae 827 this->_M_assign(__in);
ba60f6f9
PC
828 return *this;
829 }
830
ca9e949f 831 template<typename... _UElements>
6d1402f0 832 _GLIBCXX20_CONSTEXPR
478490f6
JW
833 __enable_if_t<__assignable<_UElements...>(), tuple&>
834 operator=(tuple<_UElements...>&& __in)
835 noexcept(__nothrow_assignable<_UElements...>())
836 {
a2c5e1ae 837 this->_M_assign(std::move(__in));
ba60f6f9
PC
838 return *this;
839 }
3e93b275 840
a2c5e1ae 841 // tuple swap
6d1402f0 842 _GLIBCXX20_CONSTEXPR
3e93b275 843 void
ff74fd13 844 swap(tuple& __in)
a2c5e1ae 845 noexcept(__and_<__is_nothrow_swappable<_Elements>...>::value)
1aa1114b 846 { _Inherited::_M_swap(__in); }
894d0b15
CF
847 };
848
af181c91
JW
849#if __cpp_deduction_guides >= 201606
850 template<typename... _UTypes>
851 tuple(_UTypes...) -> tuple<_UTypes...>;
852 template<typename _T1, typename _T2>
853 tuple(pair<_T1, _T2>) -> tuple<_T1, _T2>;
854 template<typename _Alloc, typename... _UTypes>
855 tuple(allocator_arg_t, _Alloc, _UTypes...) -> tuple<_UTypes...>;
856 template<typename _Alloc, typename _T1, typename _T2>
857 tuple(allocator_arg_t, _Alloc, pair<_T1, _T2>) -> tuple<_T1, _T2>;
858 template<typename _Alloc, typename... _UTypes>
859 tuple(allocator_arg_t, _Alloc, tuple<_UTypes...>) -> tuple<_UTypes...>;
860#endif
861
2c4caf0a 862 // Explicit specialization, zero-element tuple.
bf7818bf 863 template<>
3e93b275
CF
864 class tuple<>
865 {
866 public:
1aa1114b 867 void swap(tuple&) noexcept { /* no-op */ }
dbc6221f
VV
868 // We need the default since we're going to define no-op
869 // allocator constructors.
870 tuple() = default;
871 // No-op allocator constructors.
872 template<typename _Alloc>
6d1402f0 873 _GLIBCXX20_CONSTEXPR
b1bb4869 874 tuple(allocator_arg_t, const _Alloc&) noexcept { }
dbc6221f 875 template<typename _Alloc>
6d1402f0 876 _GLIBCXX20_CONSTEXPR
b1bb4869 877 tuple(allocator_arg_t, const _Alloc&, const tuple&) noexcept { }
3e93b275 878 };
939759fc 879
2c4caf0a
BK
880 /// Partial specialization, 2-element tuple.
881 /// Includes construction and assignment from a pair.
894d0b15 882 template<typename _T1, typename _T2>
a2c5e1ae 883 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
894d0b15
CF
884 {
885 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
886
d355635e
JW
887 // Constraint for non-explicit default constructor
888 template<bool _Dummy, typename _U1, typename _U2>
889 using _ImplicitDefaultCtor = __enable_if_t<
890 _TupleConstraints<_Dummy, _U1, _U2>::
891 __is_implicitly_default_constructible(),
892 bool>;
893
894 // Constraint for explicit default constructor
895 template<bool _Dummy, typename _U1, typename _U2>
896 using _ExplicitDefaultCtor = __enable_if_t<
897 _TupleConstraints<_Dummy, _U1, _U2>::
898 __is_explicitly_default_constructible(),
899 bool>;
900
901 template<bool _Dummy>
902 using _TCC = _TupleConstraints<_Dummy, _T1, _T2>;
903
904 // Constraint for non-explicit constructors
905 template<bool _Cond, typename _U1, typename _U2>
906 using _ImplicitCtor = __enable_if_t<
907 _TCC<_Cond>::template __is_implicitly_constructible<_U1, _U2>(),
908 bool>;
909
910 // Constraint for non-explicit constructors
911 template<bool _Cond, typename _U1, typename _U2>
912 using _ExplicitCtor = __enable_if_t<
913 _TCC<_Cond>::template __is_explicitly_constructible<_U1, _U2>(),
914 bool>;
915
478490f6
JW
916 template<typename _U1, typename _U2>
917 static constexpr bool __assignable()
918 {
919 return __and_<is_assignable<_T1&, _U1>,
920 is_assignable<_T2&, _U2>>::value;
921 }
922
923 template<typename _U1, typename _U2>
924 static constexpr bool __nothrow_assignable()
925 {
926 return __and_<is_nothrow_assignable<_T1&, _U1>,
927 is_nothrow_assignable<_T2&, _U2>>::value;
928 }
929
ec087ba3 930 template<typename _U1, typename _U2>
b1bb4869
JW
931 static constexpr bool __nothrow_constructible()
932 {
933 return __and_<is_nothrow_constructible<_T1, _U1>,
934 is_nothrow_constructible<_T2, _U2>>::value;
935 }
936
937 static constexpr bool __nothrow_default_constructible()
938 {
939 return __and_<is_nothrow_default_constructible<_T1>,
940 is_nothrow_default_constructible<_T2>>::value;
941 }
ec087ba3 942
d355635e
JW
943 template<typename _U1>
944 static constexpr bool __is_alloc_arg()
945 { return is_same<__remove_cvref_t<_U1>, allocator_arg_t>::value; }
946
894d0b15 947 public:
d355635e
JW
948 template<bool _Dummy = true,
949 _ImplicitDefaultCtor<_Dummy, _T1, _T2> = true>
950 constexpr
951 tuple()
b1bb4869 952 noexcept(__nothrow_default_constructible())
478490f6 953 : _Inherited() { }
894d0b15 954
d355635e
JW
955 template<bool _Dummy = true,
956 _ExplicitDefaultCtor<_Dummy, _T1, _T2> = false>
957 explicit constexpr
958 tuple()
b1bb4869 959 noexcept(__nothrow_default_constructible())
478490f6 960 : _Inherited() { }
f7632193 961
d355635e
JW
962 template<bool _Dummy = true,
963 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
964 constexpr
965 tuple(const _T1& __a1, const _T2& __a2)
966 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
967 : _Inherited(__a1, __a2) { }
968
969 template<bool _Dummy = true,
970 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
971 explicit constexpr
972 tuple(const _T1& __a1, const _T2& __a2)
973 noexcept(__nothrow_constructible<const _T1&, const _T2&>())
974 : _Inherited(__a1, __a2) { }
975
976 template<typename _U1, typename _U2,
977 _ImplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = true>
978 constexpr
979 tuple(_U1&& __a1, _U2&& __a2)
980 noexcept(__nothrow_constructible<_U1, _U2>())
ba60f6f9 981 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
894d0b15 982
d355635e
JW
983 template<typename _U1, typename _U2,
984 _ExplicitCtor<!__is_alloc_arg<_U1>(), _U1, _U2> = false>
985 explicit constexpr
986 tuple(_U1&& __a1, _U2&& __a2)
987 noexcept(__nothrow_constructible<_U1, _U2>())
bf7818bf
VV
988 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
989
094a14ef 990 constexpr tuple(const tuple&) = default;
2c4caf0a 991
a7d0c94e 992 constexpr tuple(tuple&&) = default;
ba60f6f9 993
d355635e
JW
994 template<typename _U1, typename _U2,
995 _ImplicitCtor<true, const _U1&, const _U2&> = true>
996 constexpr
997 tuple(const tuple<_U1, _U2>& __in)
998 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
b5b5e640 999 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
ba60f6f9 1000
d355635e
JW
1001 template<typename _U1, typename _U2,
1002 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1003 explicit constexpr
1004 tuple(const tuple<_U1, _U2>& __in)
1005 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
bf7818bf
VV
1006 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
1007
d355635e
JW
1008 template<typename _U1, typename _U2,
1009 _ImplicitCtor<true, _U1, _U2> = true>
1010 constexpr
1011 tuple(tuple<_U1, _U2>&& __in)
1012 noexcept(__nothrow_constructible<_U1, _U2>())
4c650853 1013 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
ba60f6f9 1014
d355635e
JW
1015 template<typename _U1, typename _U2,
1016 _ExplicitCtor<true, _U1, _U2> = false>
1017 explicit constexpr
1018 tuple(tuple<_U1, _U2>&& __in)
1019 noexcept(__nothrow_constructible<_U1, _U2>())
bf7818bf
VV
1020 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
1021
d355635e
JW
1022 template<typename _U1, typename _U2,
1023 _ImplicitCtor<true, const _U1&, const _U2&> = true>
1024 constexpr
1025 tuple(const pair<_U1, _U2>& __in)
1026 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
b5b5e640 1027 : _Inherited(__in.first, __in.second) { }
ba60f6f9 1028
d355635e
JW
1029 template<typename _U1, typename _U2,
1030 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1031 explicit constexpr
1032 tuple(const pair<_U1, _U2>& __in)
1033 noexcept(__nothrow_constructible<const _U1&, const _U2&>())
bf7818bf
VV
1034 : _Inherited(__in.first, __in.second) { }
1035
d355635e
JW
1036 template<typename _U1, typename _U2,
1037 _ImplicitCtor<true, _U1, _U2> = true>
1038 constexpr
1039 tuple(pair<_U1, _U2>&& __in)
1040 noexcept(__nothrow_constructible<_U1, _U2>())
87b2e746
PC
1041 : _Inherited(std::forward<_U1>(__in.first),
1042 std::forward<_U2>(__in.second)) { }
894d0b15 1043
d355635e
JW
1044 template<typename _U1, typename _U2,
1045 _ExplicitCtor<true, _U1, _U2> = false>
1046 explicit constexpr
1047 tuple(pair<_U1, _U2>&& __in)
1048 noexcept(__nothrow_constructible<_U1, _U2>())
bf7818bf
VV
1049 : _Inherited(std::forward<_U1>(__in.first),
1050 std::forward<_U2>(__in.second)) { }
1051
2c4caf0a 1052 // Allocator-extended constructors.
b8214660 1053
d355635e
JW
1054 template<typename _Alloc,
1055 _ImplicitDefaultCtor<is_object<_Alloc>::value, _T1, _T2> = true>
6d1402f0 1056 _GLIBCXX20_CONSTEXPR
b8214660
JW
1057 tuple(allocator_arg_t __tag, const _Alloc& __a)
1058 : _Inherited(__tag, __a) { }
1059
d355635e
JW
1060 template<typename _Alloc, bool _Dummy = true,
1061 _ImplicitCtor<_Dummy, const _T1&, const _T2&> = true>
6d1402f0 1062 _GLIBCXX20_CONSTEXPR
b8214660
JW
1063 tuple(allocator_arg_t __tag, const _Alloc& __a,
1064 const _T1& __a1, const _T2& __a2)
1065 : _Inherited(__tag, __a, __a1, __a2) { }
1066
d355635e
JW
1067 template<typename _Alloc, bool _Dummy = true,
1068 _ExplicitCtor<_Dummy, const _T1&, const _T2&> = false>
1069 explicit
6d1402f0 1070 _GLIBCXX20_CONSTEXPR
d355635e 1071 tuple(allocator_arg_t __tag, const _Alloc& __a,
bf7818bf
VV
1072 const _T1& __a1, const _T2& __a2)
1073 : _Inherited(__tag, __a, __a1, __a2) { }
1074
d355635e
JW
1075 template<typename _Alloc, typename _U1, typename _U2,
1076 _ImplicitCtor<true, _U1, _U2> = true>
6d1402f0 1077 _GLIBCXX20_CONSTEXPR
b8214660
JW
1078 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
1079 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1080 std::forward<_U2>(__a2)) { }
1081
d355635e
JW
1082 template<typename _Alloc, typename _U1, typename _U2,
1083 _ExplicitCtor<true, _U1, _U2> = false>
1084 explicit
6d1402f0 1085 _GLIBCXX20_CONSTEXPR
d355635e
JW
1086 tuple(allocator_arg_t __tag, const _Alloc& __a,
1087 _U1&& __a1, _U2&& __a2)
bf7818bf
VV
1088 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1089 std::forward<_U2>(__a2)) { }
1090
b8214660 1091 template<typename _Alloc>
6d1402f0 1092 _GLIBCXX20_CONSTEXPR
b8214660
JW
1093 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
1094 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
1095
1096 template<typename _Alloc>
6d1402f0 1097 _GLIBCXX20_CONSTEXPR
b8214660
JW
1098 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
1099 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
1100
d355635e
JW
1101 template<typename _Alloc, typename _U1, typename _U2,
1102 _ImplicitCtor<true, const _U1&, const _U2&> = true>
6d1402f0 1103 _GLIBCXX20_CONSTEXPR
b8214660
JW
1104 tuple(allocator_arg_t __tag, const _Alloc& __a,
1105 const tuple<_U1, _U2>& __in)
1106 : _Inherited(__tag, __a,
1107 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1108 { }
1109
d355635e
JW
1110 template<typename _Alloc, typename _U1, typename _U2,
1111 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1112 explicit
6d1402f0 1113 _GLIBCXX20_CONSTEXPR
d355635e 1114 tuple(allocator_arg_t __tag, const _Alloc& __a,
bf7818bf
VV
1115 const tuple<_U1, _U2>& __in)
1116 : _Inherited(__tag, __a,
1117 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1118 { }
1119
d355635e
JW
1120 template<typename _Alloc, typename _U1, typename _U2,
1121 _ImplicitCtor<true, _U1, _U2> = true>
6d1402f0 1122 _GLIBCXX20_CONSTEXPR
b8214660
JW
1123 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1124 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1125 { }
1126
d355635e
JW
1127 template<typename _Alloc, typename _U1, typename _U2,
1128 _ExplicitCtor<true, _U1, _U2> = false>
1129 explicit
6d1402f0 1130 _GLIBCXX20_CONSTEXPR
d355635e 1131 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
bf7818bf
VV
1132 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1133 { }
1134
d355635e
JW
1135 template<typename _Alloc, typename _U1, typename _U2,
1136 _ImplicitCtor<true, const _U1&, const _U2&> = true>
6d1402f0 1137 _GLIBCXX20_CONSTEXPR
d355635e 1138 tuple(allocator_arg_t __tag, const _Alloc& __a,
b8214660
JW
1139 const pair<_U1, _U2>& __in)
1140 : _Inherited(__tag, __a, __in.first, __in.second) { }
1141
d355635e
JW
1142 template<typename _Alloc, typename _U1, typename _U2,
1143 _ExplicitCtor<true, const _U1&, const _U2&> = false>
1144 explicit
6d1402f0 1145 _GLIBCXX20_CONSTEXPR
d355635e 1146 tuple(allocator_arg_t __tag, const _Alloc& __a,
bf7818bf
VV
1147 const pair<_U1, _U2>& __in)
1148 : _Inherited(__tag, __a, __in.first, __in.second) { }
1149
d355635e
JW
1150 template<typename _Alloc, typename _U1, typename _U2,
1151 _ImplicitCtor<true, _U1, _U2> = true>
6d1402f0 1152 _GLIBCXX20_CONSTEXPR
d355635e 1153 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
b8214660
JW
1154 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1155 std::forward<_U2>(__in.second)) { }
1156
d355635e
JW
1157 template<typename _Alloc, typename _U1, typename _U2,
1158 _ExplicitCtor<true, _U1, _U2> = false>
1159 explicit
6d1402f0 1160 _GLIBCXX20_CONSTEXPR
d355635e 1161 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
bf7818bf
VV
1162 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1163 std::forward<_U2>(__in.second)) { }
1164
6d1402f0
ESR
1165 // Tuple assignment.
1166
1167 _GLIBCXX20_CONSTEXPR
894d0b15 1168 tuple&
478490f6
JW
1169 operator=(typename conditional<__assignable<const _T1&, const _T2&>(),
1170 const tuple&,
a73d2fa8 1171 const __nonesuch&>::type __in)
478490f6 1172 noexcept(__nothrow_assignable<const _T1&, const _T2&>())
894d0b15 1173 {
a2c5e1ae 1174 this->_M_assign(__in);
894d0b15
CF
1175 return *this;
1176 }
1177
6d1402f0 1178 _GLIBCXX20_CONSTEXPR
ba60f6f9 1179 tuple&
478490f6
JW
1180 operator=(typename conditional<__assignable<_T1, _T2>(),
1181 tuple&&,
a73d2fa8 1182 __nonesuch&&>::type __in)
478490f6 1183 noexcept(__nothrow_assignable<_T1, _T2>())
ba60f6f9 1184 {
a2c5e1ae 1185 this->_M_assign(std::move(__in));
ba60f6f9
PC
1186 return *this;
1187 }
1188
1189 template<typename _U1, typename _U2>
6d1402f0 1190 _GLIBCXX20_CONSTEXPR
478490f6
JW
1191 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
1192 operator=(const tuple<_U1, _U2>& __in)
1193 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1194 {
a2c5e1ae 1195 this->_M_assign(__in);
ba60f6f9
PC
1196 return *this;
1197 }
1198
1199 template<typename _U1, typename _U2>
6d1402f0 1200 _GLIBCXX20_CONSTEXPR
478490f6
JW
1201 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
1202 operator=(tuple<_U1, _U2>&& __in)
1203 noexcept(__nothrow_assignable<_U1, _U2>())
1204 {
a2c5e1ae 1205 this->_M_assign(std::move(__in));
ba60f6f9
PC
1206 return *this;
1207 }
1208
894d0b15 1209 template<typename _U1, typename _U2>
6d1402f0 1210 _GLIBCXX20_CONSTEXPR
478490f6
JW
1211 __enable_if_t<__assignable<const _U1&, const _U2&>(), tuple&>
1212 operator=(const pair<_U1, _U2>& __in)
1213 noexcept(__nothrow_assignable<const _U1&, const _U2&>())
1214 {
9480716c
DK
1215 this->_M_head(*this) = __in.first;
1216 this->_M_tail(*this)._M_head(*this) = __in.second;
894d0b15
CF
1217 return *this;
1218 }
ba60f6f9
PC
1219
1220 template<typename _U1, typename _U2>
6d1402f0 1221 _GLIBCXX20_CONSTEXPR
478490f6
JW
1222 __enable_if_t<__assignable<_U1, _U2>(), tuple&>
1223 operator=(pair<_U1, _U2>&& __in)
1224 noexcept(__nothrow_assignable<_U1, _U2>())
1225 {
9480716c
DK
1226 this->_M_head(*this) = std::forward<_U1>(__in.first);
1227 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
ba60f6f9
PC
1228 return *this;
1229 }
3e93b275 1230
6d1402f0 1231 _GLIBCXX20_CONSTEXPR
3e93b275 1232 void
ff74fd13 1233 swap(tuple& __in)
a2c5e1ae
JW
1234 noexcept(__and_<__is_nothrow_swappable<_T1>,
1235 __is_nothrow_swappable<_T2>>::value)
1aa1114b 1236 { _Inherited::_M_swap(__in); }
894d0b15
CF
1237 };
1238
1239
3c040fa4
JW
1240 /// class tuple_size
1241 template<typename... _Elements>
1242 struct tuple_size<tuple<_Elements...>>
1243 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
1244
1245#if __cplusplus > 201402L
1246 template <typename _Tp>
288695f7 1247 inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
3c040fa4
JW
1248#endif
1249
894d0b15 1250 /**
894d0b15
CF
1251 * Recursive case for tuple_element: strip off the first element in
1252 * the tuple and retrieve the (i-1)th element of the remaining tuple.
894d0b15 1253 */
740508be 1254 template<std::size_t __i, typename _Head, typename... _Tail>
894d0b15
CF
1255 struct tuple_element<__i, tuple<_Head, _Tail...> >
1256 : tuple_element<__i - 1, tuple<_Tail...> > { };
1257
1258 /**
894d0b15 1259 * Basis case for tuple_element: The first element is the one we're seeking.
894d0b15
CF
1260 */
1261 template<typename _Head, typename... _Tail>
1262 struct tuple_element<0, tuple<_Head, _Tail...> >
1263 {
1264 typedef _Head type;
1265 };
1266
3c040fa4
JW
1267 /**
1268 * Error case for tuple_element: invalid index.
1269 */
1270 template<size_t __i>
1271 struct tuple_element<__i, tuple<>>
1272 {
1273 static_assert(__i < tuple_size<tuple<>>::value,
1274 "tuple index is in range");
1275 };
137422c8 1276
740508be 1277 template<std::size_t __i, typename _Head, typename... _Tail>
af222e74 1278 constexpr _Head&
173f26ae 1279 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
9480716c 1280 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
894d0b15 1281
740508be 1282 template<std::size_t __i, typename _Head, typename... _Tail>
af222e74 1283 constexpr const _Head&
173f26ae 1284 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
9480716c 1285 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
894d0b15 1286
af222e74 1287 /// Return a reference to the ith element of a tuple.
740508be 1288 template<std::size_t __i, typename... _Elements>
af222e74 1289 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
1aa1114b 1290 get(tuple<_Elements...>& __t) noexcept
39e6a690 1291 { return std::__get_helper<__i>(__t); }
894d0b15 1292
af222e74 1293 /// Return a const reference to the ith element of a const tuple.
740508be 1294 template<std::size_t __i, typename... _Elements>
af222e74 1295 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
1aa1114b 1296 get(const tuple<_Elements...>& __t) noexcept
39e6a690 1297 { return std::__get_helper<__i>(__t); }
894d0b15 1298
af222e74 1299 /// Return an rvalue reference to the ith element of a tuple rvalue.
1aa1114b 1300 template<std::size_t __i, typename... _Elements>
af222e74 1301 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
1aa1114b 1302 get(tuple<_Elements...>&& __t) noexcept
af222e74
JW
1303 {
1304 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1305 return std::forward<__element_type&&>(std::get<__i>(__t));
1306 }
1aa1114b 1307
ccbbf8df
VV
1308 /// Return a const rvalue reference to the ith element of a const tuple rvalue.
1309 template<std::size_t __i, typename... _Elements>
1310 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&&
1311 get(const tuple<_Elements...>&& __t) noexcept
1312 {
1313 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1314 return std::forward<const __element_type&&>(std::get<__i>(__t));
1315 }
1316
74755c6a 1317#if __cplusplus >= 201402L
a15f7cb8
ESR
1318
1319#define __cpp_lib_tuples_by_type 201304
1320
b5a8fed6 1321 template<typename _Head, size_t __i, typename... _Tail>
af222e74 1322 constexpr _Head&
b5a8fed6
JW
1323 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1324 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1325
1326 template<typename _Head, size_t __i, typename... _Tail>
af222e74 1327 constexpr const _Head&
b5a8fed6
JW
1328 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1329 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1330
af222e74 1331 /// Return a reference to the unique element of type _Tp of a tuple.
b5a8fed6
JW
1332 template <typename _Tp, typename... _Types>
1333 constexpr _Tp&
1334 get(tuple<_Types...>& __t) noexcept
39e6a690 1335 { return std::__get_helper2<_Tp>(__t); }
b5a8fed6 1336
af222e74 1337 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
b5a8fed6
JW
1338 template <typename _Tp, typename... _Types>
1339 constexpr _Tp&&
1340 get(tuple<_Types...>&& __t) noexcept
af222e74 1341 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
b5a8fed6 1342
af222e74 1343 /// Return a const reference to the unique element of type _Tp of a tuple.
b5a8fed6
JW
1344 template <typename _Tp, typename... _Types>
1345 constexpr const _Tp&
1346 get(const tuple<_Types...>& __t) noexcept
39e6a690 1347 { return std::__get_helper2<_Tp>(__t); }
ccbbf8df
VV
1348
1349 /// Return a const reference to the unique element of type _Tp of
1350 /// a const tuple rvalue.
1351 template <typename _Tp, typename... _Types>
1352 constexpr const _Tp&&
1353 get(const tuple<_Types...>&& __t) noexcept
1354 { return std::forward<const _Tp&&>(std::__get_helper2<_Tp>(__t)); }
b5a8fed6
JW
1355#endif
1356
af222e74
JW
1357 // This class performs the comparison operations on tuples
1358 template<typename _Tp, typename _Up, size_t __i, size_t __size>
1359 struct __tuple_compare
894d0b15 1360 {
af222e74 1361 static constexpr bool
a7d0c94e 1362 __eq(const _Tp& __t, const _Up& __u)
894d0b15 1363 {
75e75a08 1364 return bool(std::get<__i>(__t) == std::get<__i>(__u))
af222e74 1365 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
894d0b15 1366 }
33ac58d5 1367
af222e74 1368 static constexpr bool
a7d0c94e 1369 __less(const _Tp& __t, const _Up& __u)
894d0b15 1370 {
75e75a08 1371 return bool(std::get<__i>(__t) < std::get<__i>(__u))
af222e74
JW
1372 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
1373 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
894d0b15
CF
1374 }
1375 };
1376
af222e74
JW
1377 template<typename _Tp, typename _Up, size_t __size>
1378 struct __tuple_compare<_Tp, _Up, __size, __size>
894d0b15 1379 {
af222e74 1380 static constexpr bool
a7d0c94e 1381 __eq(const _Tp&, const _Up&) { return true; }
33ac58d5 1382
af222e74 1383 static constexpr bool
a7d0c94e 1384 __less(const _Tp&, const _Up&) { return false; }
894d0b15
CF
1385 };
1386
1387 template<typename... _TElements, typename... _UElements>
a9ba8ba5 1388 constexpr bool
894d0b15
CF
1389 operator==(const tuple<_TElements...>& __t,
1390 const tuple<_UElements...>& __u)
1391 {
af222e74
JW
1392 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1393 "tuple objects can only be compared if they have equal sizes.");
1394 using __compare = __tuple_compare<tuple<_TElements...>,
1395 tuple<_UElements...>,
1396 0, sizeof...(_TElements)>;
1397 return __compare::__eq(__t, __u);
894d0b15
CF
1398 }
1399
1400 template<typename... _TElements, typename... _UElements>
a9ba8ba5 1401 constexpr bool
894d0b15
CF
1402 operator<(const tuple<_TElements...>& __t,
1403 const tuple<_UElements...>& __u)
1404 {
af222e74
JW
1405 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1406 "tuple objects can only be compared if they have equal sizes.");
1407 using __compare = __tuple_compare<tuple<_TElements...>,
1408 tuple<_UElements...>,
1409 0, sizeof...(_TElements)>;
1410 return __compare::__less(__t, __u);
894d0b15
CF
1411 }
1412
1413 template<typename... _TElements, typename... _UElements>
fa409833 1414 constexpr bool
894d0b15
CF
1415 operator!=(const tuple<_TElements...>& __t,
1416 const tuple<_UElements...>& __u)
1417 { return !(__t == __u); }
1418
1419 template<typename... _TElements, typename... _UElements>
fa409833 1420 constexpr bool
894d0b15
CF
1421 operator>(const tuple<_TElements...>& __t,
1422 const tuple<_UElements...>& __u)
1423 { return __u < __t; }
1424
1425 template<typename... _TElements, typename... _UElements>
fa409833 1426 constexpr bool
894d0b15
CF
1427 operator<=(const tuple<_TElements...>& __t,
1428 const tuple<_UElements...>& __u)
1429 { return !(__u < __t); }
1430
1431 template<typename... _TElements, typename... _UElements>
fa409833 1432 constexpr bool
894d0b15
CF
1433 operator>=(const tuple<_TElements...>& __t,
1434 const tuple<_UElements...>& __u)
1435 { return !(__t < __u); }
1436
1437 // NB: DR 705.
1438 template<typename... _Elements>
fae3f459 1439 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
894d0b15
CF
1440 make_tuple(_Elements&&... __args)
1441 {
1442 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
1443 __result_type;
1444 return __result_type(std::forward<_Elements>(__args)...);
1445 }
1446
cb2ef49e
JW
1447 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1448 // 2275. Why is forward_as_tuple not constexpr?
74755c6a 1449 /// std::forward_as_tuple
5e108459 1450 template<typename... _Elements>
cb2ef49e 1451 constexpr tuple<_Elements&&...>
1aa1114b 1452 forward_as_tuple(_Elements&&... __args) noexcept
00e9a944 1453 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
5e108459 1454
af222e74 1455 template<size_t, typename, typename, size_t>
ac65b7d2
DK
1456 struct __make_tuple_impl;
1457
af222e74 1458 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
ac65b7d2 1459 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
af222e74
JW
1460 : __make_tuple_impl<_Idx + 1,
1461 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
1462 _Tuple, _Nm>
1463 { };
894d0b15 1464
ac65b7d2
DK
1465 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
1466 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
894d0b15 1467 {
ac65b7d2
DK
1468 typedef tuple<_Tp...> __type;
1469 };
894d0b15 1470
ac65b7d2
DK
1471 template<typename _Tuple>
1472 struct __do_make_tuple
af222e74 1473 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
ac65b7d2
DK
1474 { };
1475
1476 // Returns the std::tuple equivalent of a tuple-like type.
1477 template<typename _Tuple>
1478 struct __make_tuple
6791489e 1479 : public __do_make_tuple<__remove_cvref_t<_Tuple>>
ac65b7d2
DK
1480 { };
1481
1482 // Combines several std::tuple's into a single one.
1483 template<typename...>
1484 struct __combine_tuples;
1485
1486 template<>
1487 struct __combine_tuples<>
1488 {
1489 typedef tuple<> __type;
1490 };
1491
1492 template<typename... _Ts>
1493 struct __combine_tuples<tuple<_Ts...>>
894d0b15 1494 {
ac65b7d2
DK
1495 typedef tuple<_Ts...> __type;
1496 };
1497
1498 template<typename... _T1s, typename... _T2s, typename... _Rem>
1499 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
1500 {
1501 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
1502 _Rem...>::__type __type;
1503 };
1504
1505 // Computes the result type of tuple_cat given a set of tuple-like types.
1506 template<typename... _Tpls>
1507 struct __tuple_cat_result
1508 {
1509 typedef typename __combine_tuples
1510 <typename __make_tuple<_Tpls>::__type...>::__type __type;
1511 };
1512
1513 // Helper to determine the index set for the first tuple-like
1514 // type of a given set.
1515 template<typename...>
1516 struct __make_1st_indices;
1517
1518 template<>
1519 struct __make_1st_indices<>
1520 {
1521 typedef std::_Index_tuple<> __type;
1522 };
1523
1524 template<typename _Tp, typename... _Tpls>
1525 struct __make_1st_indices<_Tp, _Tpls...>
1526 {
1527 typedef typename std::_Build_index_tuple<std::tuple_size<
1528 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1529 };
1530
1531 // Performs the actual concatenation by step-wise expanding tuple-like
1532 // objects into the elements, which are finally forwarded into the
1533 // result tuple.
1534 template<typename _Ret, typename _Indices, typename... _Tpls>
1535 struct __tuple_concater;
1536
1537 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1538 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1539 {
1540 template<typename... _Us>
9480716c 1541 static constexpr _Ret
ac65b7d2
DK
1542 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1543 {
1544 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1545 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
1546 return __next::_S_do(std::forward<_Tpls>(__tps)...,
1547 std::forward<_Us>(__us)...,
1548 std::get<_Is>(std::forward<_Tp>(__tp))...);
1549 }
1550 };
1551
1552 template<typename _Ret>
1553 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1554 {
1555 template<typename... _Us>
9480716c 1556 static constexpr _Ret
ac65b7d2
DK
1557 _S_do(_Us&&... __us)
1558 {
1559 return _Ret(std::forward<_Us>(__us)...);
1560 }
1561 };
1562
c0ffa2ba 1563 /// tuple_cat
9480716c
DK
1564 template<typename... _Tpls, typename = typename
1565 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1566 constexpr auto
ac65b7d2 1567 tuple_cat(_Tpls&&... __tpls)
9480716c 1568 -> typename __tuple_cat_result<_Tpls...>::__type
ac65b7d2
DK
1569 {
1570 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1571 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1572 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1573 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
894d0b15
CF
1574 }
1575
cb2ef49e
JW
1576 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1577 // 2301. Why is tie not constexpr?
c0ffa2ba 1578 /// tie
894d0b15 1579 template<typename... _Elements>
cb2ef49e 1580 constexpr tuple<_Elements&...>
1aa1114b 1581 tie(_Elements&... __args) noexcept
894d0b15
CF
1582 { return tuple<_Elements&...>(__args...); }
1583
c0ffa2ba 1584 /// swap
3e93b275 1585 template<typename... _Elements>
6d1402f0 1586 _GLIBCXX20_CONSTEXPR
6b9539e2
DK
1587 inline
1588#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1589 // Constrained free swap overload, see p0185r1
1590 typename enable_if<__and_<__is_swappable<_Elements>...>::value
1591 >::type
1592#else
1593 void
1594#endif
3e93b275 1595 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1aa1114b 1596 noexcept(noexcept(__x.swap(__y)))
3e93b275
CF
1597 { __x.swap(__y); }
1598
a2863bde
VV
1599#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
1600 template<typename... _Elements>
6d1402f0 1601 _GLIBCXX20_CONSTEXPR
a2863bde
VV
1602 typename enable_if<!__and_<__is_swappable<_Elements>...>::value>::type
1603 swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
1604#endif
1605
894d0b15 1606 // A class (and instance) which can be used in 'tie' when an element
288695f7
DK
1607 // of a tuple is not required.
1608 // _GLIBCXX14_CONSTEXPR
1609 // 2933. PR for LWG 2773 could be clearer
894d0b15
CF
1610 struct _Swallow_assign
1611 {
1612 template<class _Tp>
288695f7 1613 _GLIBCXX14_CONSTEXPR const _Swallow_assign&
cff90044 1614 operator=(const _Tp&) const
894d0b15
CF
1615 { return *this; }
1616 };
1617
288695f7
DK
1618 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1619 // 2773. Making std::ignore constexpr
1620 _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
5e108459 1621
b8214660
JW
1622 /// Partial specialization for tuples
1623 template<typename... _Types, typename _Alloc>
1624 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1625
5e108459 1626 // See stl_pair.h...
0e7bd559
JW
1627 /** "piecewise construction" using a tuple of arguments for each member.
1628 *
1629 * @param __first Arguments for the first member of the pair.
1630 * @param __second Arguments for the second member of the pair.
1631 *
1632 * The elements of each tuple will be used as the constructor arguments
1633 * for the data members of the pair.
1634 */
5e108459 1635 template<class _T1, class _T2>
62b547b5 1636 template<typename... _Args1, typename... _Args2>
6d1402f0
ESR
1637 _GLIBCXX20_CONSTEXPR
1638 inline
62b547b5
JW
1639 pair<_T1, _T2>::
1640 pair(piecewise_construct_t,
1641 tuple<_Args1...> __first, tuple<_Args2...> __second)
1642 : pair(__first, __second,
1643 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1644 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1645 { }
5e108459
PC
1646
1647 template<class _T1, class _T2>
62b547b5
JW
1648 template<typename... _Args1, std::size_t... _Indexes1,
1649 typename... _Args2, std::size_t... _Indexes2>
7a9942f5 1650 _GLIBCXX20_CONSTEXPR inline
62b547b5
JW
1651 pair<_T1, _T2>::
1652 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1653 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1654 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1655 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1656 { }
53dc5044 1657
81c7cf71 1658#if __cplusplus >= 201703L
6fd4b25b
JW
1659
1660 // Unpack a std::tuple into a type trait and use its value.
1661 // For cv std::tuple<_Up> the result is _Trait<_Tp, cv _Up...>::value.
1662 // For cv std::tuple<_Up>& the result is _Trait<_Tp, cv _Up&...>::value.
1663 // Otherwise the result is false (because we don't know if std::get throws).
1664 template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
1665 inline constexpr bool __unpack_std_tuple = false;
1666
1667 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1668 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>>
1669 = _Trait<_Tp, _Up...>::value;
1670
1671 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1672 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>&>
1673 = _Trait<_Tp, _Up&...>::value;
1674
1675 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1676 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>>
1677 = _Trait<_Tp, const _Up...>::value;
1678
1679 template<template<typename...> class _Trait, typename _Tp, typename... _Up>
1680 inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const tuple<_Up...>&>
1681 = _Trait<_Tp, const _Up&...>::value;
1682
3e9f67e6
JW
1683# define __cpp_lib_apply 201603
1684
1685 template <typename _Fn, typename _Tuple, size_t... _Idx>
1686 constexpr decltype(auto)
1687 __apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
1688 {
1689 return std::__invoke(std::forward<_Fn>(__f),
1690 std::get<_Idx>(std::forward<_Tuple>(__t))...);
1691 }
1692
1693 template <typename _Fn, typename _Tuple>
1694 constexpr decltype(auto)
1695 apply(_Fn&& __f, _Tuple&& __t)
6fd4b25b 1696 noexcept(__unpack_std_tuple<is_nothrow_invocable, _Fn, _Tuple>)
3e9f67e6 1697 {
81c7cf71
JW
1698 using _Indices
1699 = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
285ee2fb
JW
1700 return std::__apply_impl(std::forward<_Fn>(__f),
1701 std::forward<_Tuple>(__t),
1702 _Indices{});
3e9f67e6 1703 }
ca1ab6be 1704
24299f65
VV
1705#define __cpp_lib_make_from_tuple 201606
1706
ca1ab6be
VV
1707 template <typename _Tp, typename _Tuple, size_t... _Idx>
1708 constexpr _Tp
1709 __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
24299f65 1710 { return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...); }
ca1ab6be
VV
1711
1712 template <typename _Tp, typename _Tuple>
1713 constexpr _Tp
1714 make_from_tuple(_Tuple&& __t)
6fd4b25b 1715 noexcept(__unpack_std_tuple<is_nothrow_constructible, _Tp, _Tuple>)
ca1ab6be
VV
1716 {
1717 return __make_from_tuple_impl<_Tp>(
1718 std::forward<_Tuple>(__t),
32a1aa70 1719 make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>{});
ca1ab6be 1720 }
3e9f67e6
JW
1721#endif // C++17
1722
c0ffa2ba
BK
1723 /// @}
1724
12ffa228 1725_GLIBCXX_END_NAMESPACE_VERSION
c0ffa2ba 1726} // namespace std
af13a7a6 1727
734f5023 1728#endif // C++11
57317d2a 1729
4514bed6 1730#endif // _GLIBCXX_TUPLE