]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/variant
libstdc++: Fix doxygen markup for group close commands
[thirdparty/gcc.git] / libstdc++-v3 / include / std / variant
CommitLineData
197c757c
TS
1// <variant> -*- C++ -*-
2
99dee823 3// Copyright (C) 2016-2021 Free Software Foundation, Inc.
197c757c
TS
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 variant
f0b88346 26 * This is the `<variant>` C++ Library header.
197c757c
TS
27 */
28
29#ifndef _GLIBCXX_VARIANT
30#define _GLIBCXX_VARIANT 1
31
32#pragma GCC system_header
33
c6888c62 34#if __cplusplus >= 201703L
197c757c
TS
35
36#include <type_traits>
37#include <utility>
38#include <bits/enable_special_members.h>
45b510b3 39#include <bits/functexcept.h>
f75d5999 40#include <bits/move.h>
6964bb3e 41#include <bits/functional_hash.h>
b01af236 42#include <bits/invoke.h>
9189f559 43#include <ext/aligned_buffer.h>
f3df0b3c 44#include <bits/parse_numbers.h>
94895bd9
JW
45#include <bits/stl_iterator_base_types.h>
46#include <bits/stl_iterator_base_funcs.h>
47#include <bits/stl_construct.h>
9e589880
JW
48#if __cplusplus > 201703L
49# include <compare>
50#endif
197c757c
TS
51
52namespace std _GLIBCXX_VISIBILITY(default)
53{
4a15d842
FD
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
55
b01af236
TS
56namespace __detail
57{
58namespace __variant
59{
b01af236
TS
60 template<size_t _Np, typename... _Types>
61 struct _Nth_type;
62
63 template<size_t _Np, typename _First, typename... _Rest>
64 struct _Nth_type<_Np, _First, _Rest...>
65 : _Nth_type<_Np-1, _Rest...> { };
66
67 template<typename _First, typename... _Rest>
68 struct _Nth_type<0, _First, _Rest...>
69 { using type = _First; };
70
b01af236
TS
71} // namespace __variant
72} // namespace __detail
73
56a9eaf9 74#define __cpp_lib_variant 201606L
c6888c62 75
197c757c
TS
76 template<typename... _Types> class tuple;
77 template<typename... _Types> class variant;
78 template <typename> struct hash;
79
80 template<typename _Variant>
81 struct variant_size;
82
83 template<typename _Variant>
84 struct variant_size<const _Variant> : variant_size<_Variant> {};
85
86 template<typename _Variant>
87 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
88
89 template<typename _Variant>
90 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
91
92 template<typename... _Types>
93 struct variant_size<variant<_Types...>>
94 : std::integral_constant<size_t, sizeof...(_Types)> {};
95
96 template<typename _Variant>
288695f7 97 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
197c757c
TS
98
99 template<size_t _Np, typename _Variant>
100 struct variant_alternative;
101
102 template<size_t _Np, typename _First, typename... _Rest>
103 struct variant_alternative<_Np, variant<_First, _Rest...>>
104 : variant_alternative<_Np-1, variant<_Rest...>> {};
105
106 template<typename _First, typename... _Rest>
107 struct variant_alternative<0, variant<_First, _Rest...>>
108 { using type = _First; };
109
110 template<size_t _Np, typename _Variant>
111 using variant_alternative_t =
112 typename variant_alternative<_Np, _Variant>::type;
113
3203ed5f
TS
114 template<size_t _Np, typename _Variant>
115 struct variant_alternative<_Np, const _Variant>
116 { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
117
118 template<size_t _Np, typename _Variant>
119 struct variant_alternative<_Np, volatile _Variant>
120 { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
121
122 template<size_t _Np, typename _Variant>
123 struct variant_alternative<_Np, const volatile _Variant>
124 { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
125
288695f7 126 inline constexpr size_t variant_npos = -1;
197c757c 127
b01af236
TS
128 template<size_t _Np, typename... _Types>
129 constexpr variant_alternative_t<_Np, variant<_Types...>>&
130 get(variant<_Types...>&);
131
132 template<size_t _Np, typename... _Types>
133 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
134 get(variant<_Types...>&&);
135
136 template<size_t _Np, typename... _Types>
137 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
138 get(const variant<_Types...>&);
139
140 template<size_t _Np, typename... _Types>
141 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
142 get(const variant<_Types...>&&);
143
956a62aa 144 template<typename _Result_type, typename _Visitor, typename... _Variants>
669a6fdc
VV
145 constexpr decltype(auto)
146 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
147
148 template <typename... _Types, typename _Tp>
9d89b73c
JW
149 decltype(auto)
150 __variant_cast(_Tp&& __rhs)
669a6fdc
VV
151 {
152 if constexpr (is_lvalue_reference_v<_Tp>)
153 {
154 if constexpr (is_const_v<remove_reference_t<_Tp>>)
155 return static_cast<const variant<_Types...>&>(__rhs);
156 else
157 return static_cast<variant<_Types...>&>(__rhs);
158 }
159 else
160 return static_cast<variant<_Types...>&&>(__rhs);
161 }
162
197c757c
TS
163namespace __detail
164{
165namespace __variant
166{
c98fc4eb 167 // Returns the first appearance of _Tp in _Types.
197c757c
TS
168 // Returns sizeof...(_Types) if _Tp is not in _Types.
169 template<typename _Tp, typename... _Types>
170 struct __index_of : std::integral_constant<size_t, 0> {};
171
172 template<typename _Tp, typename... _Types>
288695f7 173 inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
197c757c
TS
174
175 template<typename _Tp, typename _First, typename... _Rest>
176 struct __index_of<_Tp, _First, _Rest...> :
177 std::integral_constant<size_t, is_same_v<_Tp, _First>
178 ? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
179
669a6fdc
VV
180 // used for raw visitation
181 struct __variant_cookie {};
f1ba6c5a 182 // used for raw visitation with indices passed in
8701cb5e 183 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
956a62aa 184 // Used to enable deduction (and same-type checking) for std::visit:
3427e313 185 template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
956a62aa
JW
186
187 // Visit variants that might be valueless.
188 template<typename _Visitor, typename... _Variants>
189 constexpr void
190 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
191 {
192 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
193 std::forward<_Variants>(__variants)...);
194 }
195
196 // Visit variants that might be valueless, passing indices to the visitor.
197 template<typename _Visitor, typename... _Variants>
198 constexpr void
199 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
200 {
201 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
202 std::forward<_Variants>(__variants)...);
203 }
669a6fdc 204
24b54628
VV
205 // _Uninitialized<T> is guaranteed to be a trivially destructible type,
206 // even if T is not.
207 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
197c757c
TS
208 struct _Uninitialized;
209
210 template<typename _Type>
211 struct _Uninitialized<_Type, true>
212 {
197c757c 213 template<typename... _Args>
9d89b73c
JW
214 constexpr
215 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
216 : _M_storage(std::forward<_Args>(__args)...)
217 { }
197c757c 218
be46043e 219 constexpr const _Type& _M_get() const & noexcept
9189f559
TS
220 { return _M_storage; }
221
be46043e 222 constexpr _Type& _M_get() & noexcept
9189f559
TS
223 { return _M_storage; }
224
be46043e 225 constexpr const _Type&& _M_get() const && noexcept
9189f559
TS
226 { return std::move(_M_storage); }
227
be46043e 228 constexpr _Type&& _M_get() && noexcept
9189f559
TS
229 { return std::move(_M_storage); }
230
197c757c
TS
231 _Type _M_storage;
232 };
233
234 template<typename _Type>
235 struct _Uninitialized<_Type, false>
236 {
197c757c 237 template<typename... _Args>
9d89b73c
JW
238 constexpr
239 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
240 {
241 ::new ((void*)std::addressof(_M_storage))
242 _Type(std::forward<_Args>(__args)...);
243 }
197c757c 244
be46043e 245 const _Type& _M_get() const & noexcept
9189f559
TS
246 { return *_M_storage._M_ptr(); }
247
be46043e 248 _Type& _M_get() & noexcept
9189f559
TS
249 { return *_M_storage._M_ptr(); }
250
be46043e 251 const _Type&& _M_get() const && noexcept
9189f559
TS
252 { return std::move(*_M_storage._M_ptr()); }
253
be46043e 254 _Type&& _M_get() && noexcept
9189f559
TS
255 { return std::move(*_M_storage._M_ptr()); }
256
257 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
197c757c
TS
258 };
259
9189f559 260 template<typename _Union>
be46043e
JW
261 constexpr decltype(auto)
262 __get(in_place_index_t<0>, _Union&& __u) noexcept
9189f559
TS
263 { return std::forward<_Union>(__u)._M_first._M_get(); }
264
265 template<size_t _Np, typename _Union>
be46043e
JW
266 constexpr decltype(auto)
267 __get(in_place_index_t<_Np>, _Union&& __u) noexcept
aafaa325
JW
268 {
269 return __variant::__get(in_place_index<_Np-1>,
270 std::forward<_Union>(__u)._M_rest);
271 }
9189f559
TS
272
273 // Returns the typed storage for __v.
274 template<size_t _Np, typename _Variant>
be46043e
JW
275 constexpr decltype(auto)
276 __get(_Variant&& __v) noexcept
9189f559 277 {
aafaa325
JW
278 return __variant::__get(std::in_place_index<_Np>,
279 std::forward<_Variant>(__v)._M_u);
9189f559
TS
280 }
281
70503724
TS
282 template<typename... _Types>
283 struct _Traits
284 {
285 static constexpr bool _S_default_ctor =
4a15d842 286 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
70503724 287 static constexpr bool _S_copy_ctor =
4a15d842 288 (is_copy_constructible_v<_Types> && ...);
70503724 289 static constexpr bool _S_move_ctor =
4a15d842 290 (is_move_constructible_v<_Types> && ...);
70503724 291 static constexpr bool _S_copy_assign =
5f00d0d5 292 _S_copy_ctor
4a15d842 293 && (is_copy_assignable_v<_Types> && ...);
70503724 294 static constexpr bool _S_move_assign =
4a15d842
FD
295 _S_move_ctor
296 && (is_move_assignable_v<_Types> && ...);
70503724
TS
297
298 static constexpr bool _S_trivial_dtor =
4a15d842 299 (is_trivially_destructible_v<_Types> && ...);
70503724 300 static constexpr bool _S_trivial_copy_ctor =
4a15d842 301 (is_trivially_copy_constructible_v<_Types> && ...);
70503724 302 static constexpr bool _S_trivial_move_ctor =
4a15d842 303 (is_trivially_move_constructible_v<_Types> && ...);
70503724 304 static constexpr bool _S_trivial_copy_assign =
038bc9bf
JW
305 _S_trivial_dtor && _S_trivial_copy_ctor
306 && (is_trivially_copy_assignable_v<_Types> && ...);
70503724 307 static constexpr bool _S_trivial_move_assign =
038bc9bf
JW
308 _S_trivial_dtor && _S_trivial_move_ctor
309 && (is_trivially_move_assignable_v<_Types> && ...);
70503724
TS
310
311 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
312 // are always nothrow.
313 static constexpr bool _S_nothrow_default_ctor =
4a15d842
FD
314 is_nothrow_default_constructible_v<
315 typename _Nth_type<0, _Types...>::type>;
70503724
TS
316 static constexpr bool _S_nothrow_copy_ctor = false;
317 static constexpr bool _S_nothrow_move_ctor =
4a15d842 318 (is_nothrow_move_constructible_v<_Types> && ...);
70503724
TS
319 static constexpr bool _S_nothrow_copy_assign = false;
320 static constexpr bool _S_nothrow_move_assign =
038bc9bf
JW
321 _S_nothrow_move_ctor
322 && (is_nothrow_move_assignable_v<_Types> && ...);
70503724
TS
323 };
324
9189f559 325 // Defines members and ctors.
197c757c 326 template<typename... _Types>
9189f559 327 union _Variadic_union { };
197c757c 328
197c757c 329 template<typename _First, typename... _Rest>
9189f559 330 union _Variadic_union<_First, _Rest...>
197c757c 331 {
9189f559
TS
332 constexpr _Variadic_union() : _M_rest() { }
333
334 template<typename... _Args>
335 constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
336 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
337 { }
338
339 template<size_t _Np, typename... _Args>
340 constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
341 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
342 { }
343
344 _Uninitialized<_First> _M_first;
345 _Variadic_union<_Rest...> _M_rest;
346 };
347
10f26de9
JW
348 // _Never_valueless_alt is true for variant alternatives that can
349 // always be placed in a variant without it becoming valueless.
350
351 // For suitably-small, trivially copyable types we can create temporaries
352 // on the stack and then memcpy them into place.
353 template<typename _Tp>
354 struct _Never_valueless_alt
355 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
356 { };
357
358 // Specialize _Never_valueless_alt for other types which have a
47a468bd
JW
359 // non-throwing and cheap move construction and move assignment operator,
360 // so that emplacing the type will provide the strong exception-safety
361 // guarantee, by creating and moving a temporary.
10f26de9
JW
362 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
363 // variant using that alternative, so we can't change the value later!
364
365 // True if every alternative in _Types... can be emplaced in a variant
366 // without it becoming valueless. If this is true, variant<_Types...>
367 // can never be valueless, which enables some minor optimizations.
da97b98a 368 template <typename... _Types>
10f26de9
JW
369 constexpr bool __never_valueless()
370 {
47a468bd
JW
371 return _Traits<_Types...>::_S_move_assign
372 && (_Never_valueless_alt<_Types>::value && ...);
10f26de9 373 }
da97b98a 374
9189f559
TS
375 // Defines index and the dtor, possibly trivial.
376 template<bool __trivially_destructible, typename... _Types>
377 struct _Variant_storage;
378
f3df0b3c 379 template <typename... _Types>
9d89b73c
JW
380 using __select_index =
381 typename __select_int::_Select_int_base<sizeof...(_Types),
382 unsigned char,
383 unsigned short>::type::value_type;
f3df0b3c 384
9189f559
TS
385 template<typename... _Types>
386 struct _Variant_storage<false, _Types...>
387 {
074436cf
JW
388 constexpr
389 _Variant_storage()
390 : _M_index(static_cast<__index_type>(variant_npos))
391 { }
197c757c 392
41501d1a 393 template<size_t _Np, typename... _Args>
074436cf
JW
394 constexpr
395 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
9189f559 396 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
074436cf 397 _M_index{_Np}
197c757c
TS
398 { }
399
b62dcd16 400 void _M_reset()
669a6fdc 401 {
b62dcd16
JW
402 if (!_M_valid()) [[unlikely]]
403 return;
404
405 std::__do_visit<void>([](auto&& __this_mem) mutable
669a6fdc 406 {
b62dcd16 407 std::_Destroy(std::__addressof(__this_mem));
669a6fdc 408 }, __variant_cast<_Types...>(*this));
197c757c 409
074436cf 410 _M_index = static_cast<__index_type>(variant_npos);
458ef690
TS
411 }
412
9189f559 413 ~_Variant_storage()
458ef690 414 { _M_reset(); }
197c757c 415
70503724 416 void*
4b7a3ab8 417 _M_storage() const noexcept
70503724
TS
418 {
419 return const_cast<void*>(static_cast<const void*>(
420 std::addressof(_M_u)));
421 }
422
423 constexpr bool
424 _M_valid() const noexcept
425 {
d306dee3 426 if constexpr (__variant::__never_valueless<_Types...>())
47a468bd 427 return true;
70503724
TS
428 return this->_M_index != __index_type(variant_npos);
429 }
430
9189f559 431 _Variadic_union<_Types...> _M_u;
f3df0b3c
VV
432 using __index_type = __select_index<_Types...>;
433 __index_type _M_index;
197c757c
TS
434 };
435
9189f559
TS
436 template<typename... _Types>
437 struct _Variant_storage<true, _Types...>
197c757c 438 {
074436cf
JW
439 constexpr
440 _Variant_storage()
441 : _M_index(static_cast<__index_type>(variant_npos))
442 { }
197c757c 443
9189f559 444 template<size_t _Np, typename... _Args>
074436cf
JW
445 constexpr
446 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
9189f559 447 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
074436cf 448 _M_index{_Np}
9189f559
TS
449 { }
450
4b7a3ab8 451 void _M_reset() noexcept
074436cf 452 { _M_index = static_cast<__index_type>(variant_npos); }
458ef690 453
70503724 454 void*
4b7a3ab8 455 _M_storage() const noexcept
70503724
TS
456 {
457 return const_cast<void*>(static_cast<const void*>(
458 std::addressof(_M_u)));
459 }
460
461 constexpr bool
462 _M_valid() const noexcept
463 {
d306dee3 464 if constexpr (__variant::__never_valueless<_Types...>())
16d30bbd 465 return true;
074436cf 466 return this->_M_index != static_cast<__index_type>(variant_npos);
70503724
TS
467 }
468
9189f559 469 _Variadic_union<_Types...> _M_u;
f3df0b3c
VV
470 using __index_type = __select_index<_Types...>;
471 __index_type _M_index;
197c757c
TS
472 };
473
197c757c 474 template<typename... _Types>
70503724 475 using _Variant_storage_alias =
4a15d842 476 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
197c757c 477
337d1fec
VV
478 template<typename _Tp, typename _Up>
479 void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
480 {
481 void* __storage = std::addressof(__lhs._M_u);
482 using _Type = remove_reference_t<decltype(__rhs_mem)>;
483 if constexpr (!is_same_v<_Type, __variant_cookie>)
484 ::new (__storage)
485 _Type(std::forward<decltype(__rhs_mem)>(__rhs_mem));
486 }
487
669a6fdc
VV
488 template<typename... _Types, typename _Tp, typename _Up>
489 void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
490 {
491 __lhs._M_index = __rhs._M_index;
956a62aa 492 __variant::__raw_visit([&__lhs](auto&& __rhs_mem) mutable
669a6fdc 493 {
337d1fec 494 __variant_construct_single(std::forward<_Tp>(__lhs),
86a57ce1 495 std::forward<decltype(__rhs_mem)>(__rhs_mem));
86a57ce1 496 }, __variant_cast<_Types...>(std::forward<_Up>(__rhs)));
669a6fdc
VV
497 }
498
70503724
TS
499 // The following are (Copy|Move) (ctor|assign) layers for forwarding
500 // triviality and handling non-trivial SMF behaviors.
501
502 template<bool, typename... _Types>
503 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
504 {
505 using _Base = _Variant_storage_alias<_Types...>;
506 using _Base::_Base;
197c757c 507
70503724 508 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
4a15d842 509 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
197c757c 510 {
669a6fdc 511 __variant_construct<_Types...>(*this, __rhs);
197c757c
TS
512 }
513
70503724
TS
514 _Copy_ctor_base(_Copy_ctor_base&&) = default;
515 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
516 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
517 };
518
519 template<typename... _Types>
520 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
521 {
522 using _Base = _Variant_storage_alias<_Types...>;
523 using _Base::_Base;
524 };
525
526 template<typename... _Types>
527 using _Copy_ctor_alias =
4a15d842 528 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
70503724
TS
529
530 template<bool, typename... _Types>
531 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
532 {
533 using _Base = _Copy_ctor_alias<_Types...>;
534 using _Base::_Base;
535
536 _Move_ctor_base(_Move_ctor_base&& __rhs)
4a15d842 537 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
197c757c 538 {
86a57ce1 539 __variant_construct<_Types...>(*this, std::move(__rhs));
669a6fdc
VV
540 }
541
337d1fec
VV
542 template<typename _Up>
543 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
544 {
545 this->_M_reset();
86a57ce1 546 __variant_construct_single(*this, std::forward<_Up>(__rhs));
337d1fec 547 this->_M_index = __rhs_index;
337d1fec 548 }
197c757c 549
337d1fec
VV
550 template<typename _Up>
551 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
552 {
553 this->_M_reset();
86a57ce1 554 __variant_construct_single(*this, __rhs);
337d1fec 555 this->_M_index = __rhs_index;
337d1fec 556 }
0ec78a93 557
70503724
TS
558 _Move_ctor_base(const _Move_ctor_base&) = default;
559 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
560 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
561 };
562
563 template<typename... _Types>
564 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
565 {
566 using _Base = _Copy_ctor_alias<_Types...>;
567 using _Base::_Base;
0ec78a93 568
337d1fec
VV
569 template<typename _Up>
570 void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
571 {
572 this->_M_reset();
86a57ce1 573 __variant_construct_single(*this, std::forward<_Up>(__rhs));
337d1fec 574 this->_M_index = __rhs_index;
337d1fec 575 }
9d89b73c 576
337d1fec
VV
577 template<typename _Up>
578 void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
579 {
580 this->_M_reset();
337d1fec 581 __variant_construct_single(*this, __rhs);
86a57ce1 582 this->_M_index = __rhs_index;
337d1fec 583 }
70503724
TS
584 };
585
586 template<typename... _Types>
587 using _Move_ctor_alias =
4a15d842 588 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
70503724
TS
589
590 template<bool, typename... _Types>
591 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
592 {
593 using _Base = _Move_ctor_alias<_Types...>;
594 using _Base::_Base;
197c757c 595
70503724
TS
596 _Copy_assign_base&
597 operator=(const _Copy_assign_base& __rhs)
4a15d842 598 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
197c757c 599 {
956a62aa
JW
600 __variant::__raw_idx_visit(
601 [this](auto&& __rhs_mem, auto __rhs_index) mutable
197c757c 602 {
f1ba6c5a 603 if constexpr (__rhs_index != variant_npos)
197c757c 604 {
f1ba6c5a 605 if (this->_M_index == __rhs_index)
86a57ce1 606 __variant::__get<__rhs_index>(*this) = __rhs_mem;
f1ba6c5a
VV
607 else
608 {
86a57ce1
JW
609 using __rhs_type = __remove_cvref_t<decltype(__rhs_mem)>;
610 if constexpr (is_nothrow_copy_constructible_v<__rhs_type>
611 || !is_nothrow_move_constructible_v<__rhs_type>)
612 // The standard says this->emplace<__rhs_type>(__rhs_mem)
613 // should be used here, but _M_destructive_copy is
614 // equivalent in this case. Either copy construction
615 // doesn't throw, so _M_destructive_copy gives strong
616 // exception safety guarantee, or both copy construction
617 // and move construction can throw, so emplace only gives
618 // basic exception safety anyway.
619 this->_M_destructive_copy(__rhs_index, __rhs_mem);
669a6fdc 620 else
86a57ce1 621 __variant_cast<_Types...>(*this)
dbca7a69
VV
622 = variant<_Types...>(std::in_place_index<__rhs_index>,
623 __rhs_mem);
669a6fdc 624 }
669a6fdc 625 }
f1ba6c5a
VV
626 else
627 this->_M_reset();
f1ba6c5a 628 }, __variant_cast<_Types...>(__rhs));
197c757c
TS
629 return *this;
630 }
631
70503724
TS
632 _Copy_assign_base(const _Copy_assign_base&) = default;
633 _Copy_assign_base(_Copy_assign_base&&) = default;
634 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
635 };
636
637 template<typename... _Types>
638 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
639 {
640 using _Base = _Move_ctor_alias<_Types...>;
641 using _Base::_Base;
642 };
643
644 template<typename... _Types>
645 using _Copy_assign_alias =
038bc9bf 646 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
70503724
TS
647
648 template<bool, typename... _Types>
649 struct _Move_assign_base : _Copy_assign_alias<_Types...>
650 {
651 using _Base = _Copy_assign_alias<_Types...>;
652 using _Base::_Base;
653
70503724
TS
654 _Move_assign_base&
655 operator=(_Move_assign_base&& __rhs)
4a15d842 656 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
197c757c 657 {
956a62aa
JW
658 __variant::__raw_idx_visit(
659 [this](auto&& __rhs_mem, auto __rhs_index) mutable
197c757c 660 {
f1ba6c5a 661 if constexpr (__rhs_index != variant_npos)
197c757c 662 {
f1ba6c5a 663 if (this->_M_index == __rhs_index)
86a57ce1 664 __variant::__get<__rhs_index>(*this) = std::move(__rhs_mem);
f1ba6c5a 665 else
86a57ce1
JW
666 __variant_cast<_Types...>(*this)
667 .template emplace<__rhs_index>(std::move(__rhs_mem));
197c757c 668 }
669a6fdc 669 else
f1ba6c5a 670 this->_M_reset();
f1ba6c5a 671 }, __variant_cast<_Types...>(__rhs));
197c757c
TS
672 return *this;
673 }
674
70503724
TS
675 _Move_assign_base(const _Move_assign_base&) = default;
676 _Move_assign_base(_Move_assign_base&&) = default;
677 _Move_assign_base& operator=(const _Move_assign_base&) = default;
678 };
197c757c 679
70503724
TS
680 template<typename... _Types>
681 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
682 {
683 using _Base = _Copy_assign_alias<_Types...>;
684 using _Base::_Base;
685 };
686
687 template<typename... _Types>
688 using _Move_assign_alias =
9d89b73c 689 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
70503724
TS
690
691 template<typename... _Types>
692 struct _Variant_base : _Move_assign_alias<_Types...>
693 {
694 using _Base = _Move_assign_alias<_Types...>;
695
696 constexpr
697 _Variant_base()
4a15d842 698 noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
70503724
TS
699 : _Variant_base(in_place_index<0>) { }
700
701 template<size_t _Np, typename... _Args>
702 constexpr explicit
703 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
704 : _Base(__i, std::forward<_Args>(__args)...)
705 { }
706
707 _Variant_base(const _Variant_base&) = default;
708 _Variant_base(_Variant_base&&) = default;
709 _Variant_base& operator=(const _Variant_base&) = default;
710 _Variant_base& operator=(_Variant_base&&) = default;
197c757c
TS
711 };
712
713 // For how many times does _Tp appear in _Tuple?
714 template<typename _Tp, typename _Tuple>
715 struct __tuple_count;
716
717 template<typename _Tp, typename _Tuple>
288695f7
DK
718 inline constexpr size_t __tuple_count_v =
719 __tuple_count<_Tp, _Tuple>::value;
197c757c
TS
720
721 template<typename _Tp, typename... _Types>
722 struct __tuple_count<_Tp, tuple<_Types...>>
723 : integral_constant<size_t, 0> { };
724
725 template<typename _Tp, typename _First, typename... _Rest>
726 struct __tuple_count<_Tp, tuple<_First, _Rest...>>
727 : integral_constant<
728 size_t,
729 __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
730
731 // TODO: Reuse this in <tuple> ?
732 template<typename _Tp, typename... _Types>
288695f7
DK
733 inline constexpr bool __exactly_once =
734 __tuple_count_v<_Tp, tuple<_Types...>> == 1;
197c757c 735
d069df01
JW
736 // Helper used to check for valid conversions that don't involve narrowing.
737 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
197c757c 738
c98fc4eb
JW
739 // "Build an imaginary function FUN(Ti) for each alternative type Ti"
740 template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
d069df01 741 struct _Build_FUN
197c757c 742 {
d069df01
JW
743 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
744 // but only static functions will be considered in the call below.
745 void _S_fun();
197c757c
TS
746 };
747
c98fc4eb 748 // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
d069df01 749 template<size_t _Ind, typename _Tp, typename _Ti>
c98fc4eb 750 struct _Build_FUN<_Ind, _Tp, _Ti,
d069df01 751 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
197c757c 752 {
d069df01
JW
753 // This is the FUN function for type _Ti, with index _Ind
754 static integral_constant<size_t, _Ind> _S_fun(_Ti);
197c757c
TS
755 };
756
d069df01
JW
757 template<typename _Tp, typename _Variant,
758 typename = make_index_sequence<variant_size_v<_Variant>>>
759 struct _Build_FUNs;
760
761 template<typename _Tp, typename... _Ti, size_t... _Ind>
762 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
763 : _Build_FUN<_Ind, _Tp, _Ti>...
197c757c 764 {
d069df01 765 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
197c757c
TS
766 };
767
d069df01
JW
768 // The index j of the overload FUN(Tj) selected by overload resolution
769 // for FUN(std::forward<_Tp>(t))
770 template<typename _Tp, typename _Variant>
771 using _FUN_type
772 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
773
774 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
775 template<typename _Tp, typename _Variant, typename = void>
776 struct __accepted_index
777 : integral_constant<size_t, variant_npos>
778 { };
779
780 template<typename _Tp, typename _Variant>
781 struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
782 : _FUN_type<_Tp, _Variant>
783 { };
784
197c757c
TS
785 // Returns the raw storage for __v.
786 template<typename _Variant>
4b7a3ab8 787 void* __get_storage(_Variant&& __v) noexcept
197c757c
TS
788 { return __v._M_storage(); }
789
da97b98a 790 template <typename _Maybe_variant_cookie, typename _Variant>
10f26de9
JW
791 struct _Extra_visit_slot_needed
792 {
793 template <typename> struct _Variant_never_valueless;
da97b98a 794
10f26de9
JW
795 template <typename... _Types>
796 struct _Variant_never_valueless<variant<_Types...>>
d306dee3 797 : bool_constant<__variant::__never_valueless<_Types...>()> {};
da97b98a 798
10f26de9
JW
799 static constexpr bool value =
800 (is_same_v<_Maybe_variant_cookie, __variant_cookie>
801 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
802 && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
803 };
da97b98a 804
9d89b73c 805 // Used for storing a multi-dimensional vtable.
197c757c 806 template<typename _Tp, size_t... _Dimensions>
9d89b73c
JW
807 struct _Multi_array;
808
809 // Partial specialization with rank zero, stores a single _Tp element.
810 template<typename _Tp>
811 struct _Multi_array<_Tp>
197c757c 812 {
956a62aa
JW
813 template<typename>
814 struct __untag_result
815 : false_type
816 { using element_type = _Tp; };
817
818 template <typename... _Args>
819 struct __untag_result<const void(*)(_Args...)>
820 : false_type
821 { using element_type = void(*)(_Args...); };
822
823 template <typename... _Args>
824 struct __untag_result<__variant_cookie(*)(_Args...)>
825 : false_type
826 { using element_type = void(*)(_Args...); };
827
828 template <typename... _Args>
829 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
830 : false_type
831 { using element_type = void(*)(_Args...); };
832
833 template <typename _Res, typename... _Args>
834 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
835 : true_type
836 { using element_type = _Res(*)(_Args...); };
837
838 using __result_is_deduced = __untag_result<_Tp>;
839
840 constexpr const typename __untag_result<_Tp>::element_type&
197c757c
TS
841 _M_access() const
842 { return _M_data; }
843
956a62aa 844 typename __untag_result<_Tp>::element_type _M_data;
197c757c
TS
845 };
846
9d89b73c 847 // Partial specialization with rank >= 1.
669a6fdc
VV
848 template<typename _Ret,
849 typename _Visitor,
850 typename... _Variants,
851 size_t __first, size_t... __rest>
852 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
197c757c 853 {
da97b98a
VV
854 static constexpr size_t __index =
855 sizeof...(_Variants) - sizeof...(__rest) - 1;
9d89b73c 856
da97b98a 857 using _Variant = typename _Nth_type<__index, _Variants...>::type;
9d89b73c 858
669a6fdc 859 static constexpr int __do_cookie =
da97b98a 860 _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
9d89b73c 861
669a6fdc 862 using _Tp = _Ret(*)(_Visitor, _Variants...);
9d89b73c 863
197c757c 864 template<typename... _Args>
956a62aa 865 constexpr decltype(auto)
197c757c 866 _M_access(size_t __first_index, _Args... __rest_indices) const
9d89b73c
JW
867 {
868 return _M_arr[__first_index + __do_cookie]
869 ._M_access(__rest_indices...);
870 }
197c757c 871
669a6fdc 872 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
197c757c
TS
873 };
874
875 // Creates a multi-dimensional vtable recursively.
197c757c
TS
876 //
877 // For example,
878 // visit([](auto, auto){},
b01af236
TS
879 // variant<int, char>(), // typedef'ed as V1
880 // variant<float, double, long double>()) // typedef'ed as V2
197c757c 881 // will trigger instantiations of:
956a62aa 882 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
b01af236 883 // tuple<V1&&, V2&&>, std::index_sequence<>>
956a62aa 884 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
b01af236 885 // tuple<V1&&, V2&&>, std::index_sequence<0>>
956a62aa 886 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
b01af236 887 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
956a62aa 888 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
b01af236 889 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
956a62aa 890 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
b01af236 891 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
956a62aa 892 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
b01af236 893 // tuple<V1&&, V2&&>, std::index_sequence<1>>
956a62aa 894 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
b01af236 895 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
956a62aa 896 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
b01af236 897 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
956a62aa 898 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
b01af236 899 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
197c757c
TS
900 // The returned multi-dimensional vtable can be fast accessed by the visitor
901 // using index calculation.
956a62aa 902 template<typename _Array_type, typename _Index_seq>
197c757c
TS
903 struct __gen_vtable_impl;
904
9d89b73c
JW
905 // Defines the _S_apply() member that returns a _Multi_array populated
906 // with function pointers that perform the visitation expressions e(m)
907 // for each valid pack of indexes into the variant types _Variants.
908 //
909 // This partial specialization builds up the index sequences by recursively
910 // calling _S_apply() on the next specialization of __gen_vtable_impl.
911 // The base case of the recursion defines the actual function pointers.
956a62aa 912 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
b01af236
TS
913 typename... _Variants, size_t... __indices>
914 struct __gen_vtable_impl<
0f9cf7ff 915 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
956a62aa 916 std::index_sequence<__indices...>>
197c757c 917 {
b01af236
TS
918 using _Next =
919 remove_reference_t<typename _Nth_type<sizeof...(__indices),
920 _Variants...>::type>;
921 using _Array_type =
0f9cf7ff
TS
922 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
923 __dimensions...>;
b01af236 924
197c757c
TS
925 static constexpr _Array_type
926 _S_apply()
927 {
928 _Array_type __vtable{};
929 _S_apply_all_alts(
b01af236 930 __vtable, make_index_sequence<variant_size_v<_Next>>());
197c757c
TS
931 return __vtable;
932 }
933
b01af236 934 template<size_t... __var_indices>
197c757c 935 static constexpr void
b01af236
TS
936 _S_apply_all_alts(_Array_type& __vtable,
937 std::index_sequence<__var_indices...>)
938 {
da97b98a 939 if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
669a6fdc
VV
940 (_S_apply_single_alt<true, __var_indices>(
941 __vtable._M_arr[__var_indices + 1],
942 &(__vtable._M_arr[0])), ...);
943 else
944 (_S_apply_single_alt<false, __var_indices>(
945 __vtable._M_arr[__var_indices]), ...);
b01af236 946 }
197c757c 947
669a6fdc 948 template<bool __do_cookie, size_t __index, typename _Tp>
197c757c 949 static constexpr void
669a6fdc 950 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
197c757c 951 {
669a6fdc
VV
952 if constexpr (__do_cookie)
953 {
954 __element = __gen_vtable_impl<
955 _Tp,
669a6fdc
VV
956 std::index_sequence<__indices..., __index>>::_S_apply();
957 *__cookie_element = __gen_vtable_impl<
958 _Tp,
669a6fdc
VV
959 std::index_sequence<__indices..., variant_npos>>::_S_apply();
960 }
961 else
962 {
1f65bf2a 963 auto __tmp_element = __gen_vtable_impl<
956a62aa 964 remove_reference_t<decltype(__element)>,
669a6fdc 965 std::index_sequence<__indices..., __index>>::_S_apply();
1f65bf2a
VV
966 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
967 "std::visit requires the visitor to have the same "
968 "return type for all alternatives of a variant");
969 __element = __tmp_element;
669a6fdc 970 }
197c757c
TS
971 }
972 };
973
9d89b73c
JW
974 // This partial specialization is the base case for the recursion.
975 // It populates a _Multi_array element with the address of a function
976 // that invokes the visitor with the alternatives specified by __indices.
956a62aa 977 template<typename _Result_type, typename _Visitor, typename... _Variants,
b01af236 978 size_t... __indices>
197c757c 979 struct __gen_vtable_impl<
b01af236 980 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
956a62aa 981 std::index_sequence<__indices...>>
197c757c
TS
982 {
983 using _Array_type =
9d89b73c 984 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
b01af236 985
669a6fdc
VV
986 template<size_t __index, typename _Variant>
987 static constexpr decltype(auto)
be46043e 988 __element_by_index_or_cookie(_Variant&& __var) noexcept
669a6fdc
VV
989 {
990 if constexpr (__index != variant_npos)
991 return __variant::__get<__index>(std::forward<_Variant>(__var));
992 else
993 return __variant_cookie{};
994 }
995
469218a3 996 static constexpr decltype(auto)
956a62aa 997 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
b01af236 998 {
9d89b73c 999 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
956a62aa
JW
1000 // For raw visitation using indices, pass the indices to the visitor
1001 // and discard the return value:
1002 std::__invoke(std::forward<_Visitor>(__visitor),
9d89b73c
JW
1003 __element_by_index_or_cookie<__indices>(
1004 std::forward<_Variants>(__vars))...,
1005 integral_constant<size_t, __indices>()...);
956a62aa
JW
1006 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1007 // For raw visitation without indices, and discard the return value:
1008 std::__invoke(std::forward<_Visitor>(__visitor),
9d89b73c
JW
1009 __element_by_index_or_cookie<__indices>(
1010 std::forward<_Variants>(__vars))...);
956a62aa
JW
1011 else if constexpr (_Array_type::__result_is_deduced::value)
1012 // For the usual std::visit case deduce the return value:
f1ba6c5a 1013 return std::__invoke(std::forward<_Visitor>(__visitor),
9d89b73c
JW
1014 __element_by_index_or_cookie<__indices>(
1015 std::forward<_Variants>(__vars))...);
956a62aa
JW
1016 else // for std::visit<R> use INVOKE<R>
1017 return std::__invoke_r<_Result_type>(
1018 std::forward<_Visitor>(__visitor),
1019 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
3d01c7c2
VV
1020 }
1021
197c757c
TS
1022 static constexpr auto
1023 _S_apply()
3427e313
VV
1024 {
1025 if constexpr (_Array_type::__result_is_deduced::value)
1026 {
1027 constexpr bool __visit_ret_type_mismatch =
1028 !is_same_v<typename _Result_type::type,
1029 decltype(__visit_invoke(std::declval<_Visitor>(),
1030 std::declval<_Variants>()...))>;
1031 if constexpr (__visit_ret_type_mismatch)
1032 {
1f65bf2a
VV
1033 struct __cannot_match {};
1034 return __cannot_match{};
3427e313
VV
1035 }
1036 else
1037 return _Array_type{&__visit_invoke};
1038 }
1039 else
1040 return _Array_type{&__visit_invoke};
1041 }
197c757c
TS
1042 };
1043
956a62aa 1044 template<typename _Result_type, typename _Visitor, typename... _Variants>
197c757c
TS
1045 struct __gen_vtable
1046 {
197c757c 1047 using _Array_type =
9d89b73c 1048 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
11767f80 1049 variant_size_v<remove_reference_t<_Variants>>...>;
197c757c 1050
9d89b73c 1051 static constexpr _Array_type _S_vtable
956a62aa 1052 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
197c757c
TS
1053 };
1054
458ef690
TS
1055 template<size_t _Np, typename _Tp>
1056 struct _Base_dedup : public _Tp { };
1057
1058 template<typename _Variant, typename __indices>
1059 struct _Variant_hash_base;
1060
1061 template<typename... _Types, size_t... __indices>
1062 struct _Variant_hash_base<variant<_Types...>,
1063 std::index_sequence<__indices...>>
1064 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1065
197c757c
TS
1066} // namespace __variant
1067} // namespace __detail
1068
669a6fdc
VV
1069 template<size_t _Np, typename _Variant, typename... _Args>
1070 void __variant_construct_by_index(_Variant& __v, _Args&&... __args)
1071 {
1072 __v._M_index = _Np;
1073 auto&& __storage = __detail::__variant::__get<_Np>(__v);
1074 ::new ((void*)std::addressof(__storage))
1075 remove_reference_t<decltype(__storage)>
1076 (std::forward<_Args>(__args)...);
1077 }
1078
197c757c 1079 template<typename _Tp, typename... _Types>
469218a3
JW
1080 constexpr bool
1081 holds_alternative(const variant<_Types...>& __v) noexcept
197c757c
TS
1082 {
1083 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
c43c3af2 1084 "T must occur exactly once in alternatives");
197c757c
TS
1085 return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
1086 }
1087
197c757c 1088 template<typename _Tp, typename... _Types>
469218a3 1089 constexpr _Tp& get(variant<_Types...>& __v)
197c757c
TS
1090 {
1091 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
c43c3af2 1092 "T must occur exactly once in alternatives");
801b2266 1093 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
5dbbf899 1094 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
197c757c
TS
1095 }
1096
1097 template<typename _Tp, typename... _Types>
469218a3 1098 constexpr _Tp&& get(variant<_Types...>&& __v)
197c757c
TS
1099 {
1100 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
c43c3af2 1101 "T must occur exactly once in alternatives");
801b2266 1102 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
5dbbf899 1103 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
197c757c
TS
1104 std::move(__v));
1105 }
1106
1107 template<typename _Tp, typename... _Types>
469218a3 1108 constexpr const _Tp& get(const variant<_Types...>& __v)
197c757c
TS
1109 {
1110 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
c43c3af2 1111 "T must occur exactly once in alternatives");
801b2266 1112 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
5dbbf899 1113 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
197c757c
TS
1114 }
1115
1116 template<typename _Tp, typename... _Types>
469218a3 1117 constexpr const _Tp&& get(const variant<_Types...>&& __v)
197c757c
TS
1118 {
1119 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
c43c3af2 1120 "T must occur exactly once in alternatives");
801b2266 1121 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
5dbbf899 1122 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
197c757c
TS
1123 std::move(__v));
1124 }
1125
1126 template<size_t _Np, typename... _Types>
469218a3 1127 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
197c757c
TS
1128 get_if(variant<_Types...>* __ptr) noexcept
1129 {
1130 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1131 static_assert(_Np < sizeof...(_Types),
801b2266
JW
1132 "The index must be in [0, number of alternatives)");
1133 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
197c757c 1134 if (__ptr && __ptr->index() == _Np)
669a6fdc 1135 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
197c757c
TS
1136 return nullptr;
1137 }
1138
1139 template<size_t _Np, typename... _Types>
469218a3 1140 constexpr
9189f559 1141 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
197c757c
TS
1142 get_if(const variant<_Types...>* __ptr) noexcept
1143 {
1144 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1145 static_assert(_Np < sizeof...(_Types),
801b2266
JW
1146 "The index must be in [0, number of alternatives)");
1147 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
197c757c 1148 if (__ptr && __ptr->index() == _Np)
669a6fdc 1149 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
197c757c
TS
1150 return nullptr;
1151 }
1152
1153 template<typename _Tp, typename... _Types>
469218a3 1154 constexpr add_pointer_t<_Tp>
9189f559 1155 get_if(variant<_Types...>* __ptr) noexcept
197c757c
TS
1156 {
1157 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
c43c3af2 1158 "T must occur exactly once in alternatives");
801b2266 1159 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
5dbbf899
JW
1160 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1161 __ptr);
197c757c
TS
1162 }
1163
1164 template<typename _Tp, typename... _Types>
469218a3 1165 constexpr add_pointer_t<const _Tp>
be46043e 1166 get_if(const variant<_Types...>* __ptr) noexcept
197c757c
TS
1167 {
1168 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
c43c3af2 1169 "T must occur exactly once in alternatives");
801b2266 1170 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
5dbbf899
JW
1171 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1172 __ptr);
197c757c
TS
1173 }
1174
d255829b 1175 struct monostate { };
197c757c 1176
d255829b
TS
1177#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1178 template<typename... _Types> \
1179 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1180 const variant<_Types...>& __rhs) \
1181 { \
669a6fdc 1182 bool __ret = true; \
956a62aa
JW
1183 __detail::__variant::__raw_idx_visit( \
1184 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
669a6fdc 1185 { \
f1ba6c5a
VV
1186 if constexpr (__rhs_index != variant_npos) \
1187 { \
1188 if (__lhs.index() == __rhs_index) \
1189 { \
1190 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1191 __ret = __this_mem __OP __rhs_mem; \
1192 } \
1193 else \
1194 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1195 } \
1196 else \
1197 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
f1ba6c5a 1198 }, __rhs); \
669a6fdc 1199 return __ret; \
9e589880 1200 }
d255829b
TS
1201
1202 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1203 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1204 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1205 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1206 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1207 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1208
1209#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
197c757c 1210
9e589880
JW
1211 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1212
1213#ifdef __cpp_lib_three_way_comparison
1214 template<typename... _Types>
1215 requires (three_way_comparable<_Types> && ...)
1216 constexpr
1217 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1218 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1219 {
1220 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1221 = strong_ordering::equal;
1222
1223 __detail::__variant::__raw_idx_visit(
1224 [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1225 {
1226 if constexpr (__w_index != variant_npos)
1227 {
1228 if (__v.index() == __w_index)
1229 {
1230 auto& __this_mem = std::get<__w_index>(__v);
1231 __ret = __this_mem <=> __w_mem;
1232 return;
1233 }
1234 }
1235 __ret = (__v.index() + 1) <=> (__w_index + 1);
1236 }, __w);
1237 return __ret;
1238 }
1239
1240 constexpr strong_ordering
1241 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1242#else
1243 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1244 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1245 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1246 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1247 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1248#endif
1249
197c757c 1250 template<typename _Visitor, typename... _Variants>
b01af236 1251 constexpr decltype(auto) visit(_Visitor&&, _Variants&&...);
197c757c 1252
197c757c 1253 template<typename... _Types>
458ef690
TS
1254 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1255 && (is_swappable_v<_Types> && ...)>
a2863bde
VV
1256 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1257 noexcept(noexcept(__lhs.swap(__rhs)))
197c757c
TS
1258 { __lhs.swap(__rhs); }
1259
a2863bde 1260 template<typename... _Types>
258ee761
BA
1261 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1262 && (is_swappable_v<_Types> && ...))>
a2863bde
VV
1263 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1264
197c757c
TS
1265 class bad_variant_access : public exception
1266 {
1267 public:
c43c3af2
JW
1268 bad_variant_access() noexcept { }
1269
197c757c
TS
1270 const char* what() const noexcept override
1271 { return _M_reason; }
1272
1273 private:
c43c3af2 1274 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
197c757c 1275
c43c3af2
JW
1276 // Must point to a string with static storage duration:
1277 const char* _M_reason = "bad variant access";
197c757c
TS
1278
1279 friend void __throw_bad_variant_access(const char* __what);
1280 };
1281
c43c3af2 1282 // Must only be called with a string literal
197c757c
TS
1283 inline void
1284 __throw_bad_variant_access(const char* __what)
1285 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1286
c43c3af2
JW
1287 inline void
1288 __throw_bad_variant_access(bool __valueless)
1289 {
1290 if (__valueless) [[__unlikely__]]
1291 __throw_bad_variant_access("std::get: variant is valueless");
1292 else
1293 __throw_bad_variant_access("std::get: wrong index for variant");
1294 }
1295
197c757c
TS
1296 template<typename... _Types>
1297 class variant
1298 : private __detail::__variant::_Variant_base<_Types...>,
1299 private _Enable_default_constructor<
4a15d842 1300 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
70503724 1301 variant<_Types...>>,
197c757c 1302 private _Enable_copy_move<
4a15d842
FD
1303 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1304 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1305 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1306 __detail::__variant::_Traits<_Types...>::_S_move_assign,
197c757c
TS
1307 variant<_Types...>>
1308 {
1309 private:
669a6fdc
VV
1310 template <typename... _UTypes, typename _Tp>
1311 friend decltype(auto) __variant_cast(_Tp&&);
1312 template<size_t _Np, typename _Variant, typename... _Args>
1313 friend void __variant_construct_by_index(_Variant& __v,
1314 _Args&&... __args);
1315
7b277e8b
TS
1316 static_assert(sizeof...(_Types) > 0,
1317 "variant must have at least one alternative");
1318 static_assert(!(std::is_reference_v<_Types> || ...),
1319 "variant must have no reference alternative");
1320 static_assert(!(std::is_void_v<_Types> || ...),
1321 "variant must have no void alternative");
1322
197c757c
TS
1323 using _Base = __detail::__variant::_Variant_base<_Types...>;
1324 using _Default_ctor_enabler =
4a15d842
FD
1325 _Enable_default_constructor<
1326 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1327 variant<_Types...>>;
197c757c 1328
06715e1c
JW
1329 template<typename _Tp>
1330 static constexpr bool __not_self
1331 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1332
197c757c
TS
1333 template<typename _Tp>
1334 static constexpr bool
1335 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1336
1337 template<typename _Tp>
d069df01
JW
1338 static constexpr size_t __accepted_index
1339 = __detail::__variant::__accepted_index<_Tp, variant>::value;
197c757c 1340
06715e1c
JW
1341 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1342 using __to_type = variant_alternative_t<_Np, variant>;
197c757c 1343
06715e1c 1344 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
197c757c
TS
1345 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1346
197c757c
TS
1347 template<typename _Tp>
1348 static constexpr size_t __index_of =
1349 __detail::__variant::__index_of_v<_Tp, _Types...>;
1350
70503724
TS
1351 using _Traits = __detail::__variant::_Traits<_Types...>;
1352
06715e1c
JW
1353 template<typename _Tp>
1354 struct __is_in_place_tag : false_type { };
1355 template<typename _Tp>
1356 struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1357 template<size_t _Np>
1358 struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1359
1360 template<typename _Tp>
1361 static constexpr bool __not_in_place_tag
1362 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1363
197c757c 1364 public:
70503724
TS
1365 variant() = default;
1366 variant(const variant& __rhs) = default;
1367 variant(variant&&) = default;
1368 variant& operator=(const variant&) = default;
1369 variant& operator=(variant&&) = default;
1370 ~variant() = default;
197c757c
TS
1371
1372 template<typename _Tp,
06715e1c
JW
1373 typename = enable_if_t<sizeof...(_Types) != 0>,
1374 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1375 typename _Tj = __accepted_type<_Tp&&>,
1376 typename = enable_if_t<__exactly_once<_Tj>
1377 && is_constructible_v<_Tj, _Tp>>>
197c757c
TS
1378 constexpr
1379 variant(_Tp&& __t)
06715e1c 1380 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
d069df01 1381 : variant(in_place_index<__accepted_index<_Tp>>,
4a15d842 1382 std::forward<_Tp>(__t))
59e36c85 1383 { }
197c757c
TS
1384
1385 template<typename _Tp, typename... _Args,
1386 typename = enable_if_t<__exactly_once<_Tp>
06715e1c 1387 && is_constructible_v<_Tp, _Args...>>>
197c757c
TS
1388 constexpr explicit
1389 variant(in_place_type_t<_Tp>, _Args&&... __args)
70503724 1390 : variant(in_place_index<__index_of<_Tp>>,
4a15d842 1391 std::forward<_Args>(__args)...)
59e36c85 1392 { }
197c757c
TS
1393
1394 template<typename _Tp, typename _Up, typename... _Args,
1395 typename = enable_if_t<__exactly_once<_Tp>
06715e1c
JW
1396 && is_constructible_v<_Tp,
1397 initializer_list<_Up>&, _Args...>>>
197c757c
TS
1398 constexpr explicit
1399 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1400 _Args&&... __args)
627a2f59 1401 : variant(in_place_index<__index_of<_Tp>>, __il,
64626fca 1402 std::forward<_Args>(__args)...)
59e36c85 1403 { }
197c757c
TS
1404
1405 template<size_t _Np, typename... _Args,
06715e1c
JW
1406 typename _Tp = __to_type<_Np>,
1407 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
197c757c
TS
1408 constexpr explicit
1409 variant(in_place_index_t<_Np>, _Args&&... __args)
627a2f59 1410 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
197c757c 1411 _Default_ctor_enabler(_Enable_default_constructor_tag{})
59e36c85 1412 { }
197c757c
TS
1413
1414 template<size_t _Np, typename _Up, typename... _Args,
06715e1c
JW
1415 typename _Tp = __to_type<_Np>,
1416 typename = enable_if_t<is_constructible_v<_Tp,
1417 initializer_list<_Up>&,
1418 _Args...>>>
197c757c
TS
1419 constexpr explicit
1420 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1421 _Args&&... __args)
627a2f59 1422 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
197c757c 1423 _Default_ctor_enabler(_Enable_default_constructor_tag{})
59e36c85 1424 { }
197c757c 1425
197c757c
TS
1426 template<typename _Tp>
1427 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
06715e1c
JW
1428 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1429 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1430 variant&>
197c757c 1431 operator=(_Tp&& __rhs)
06715e1c
JW
1432 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1433 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
197c757c 1434 {
d069df01 1435 constexpr auto __index = __accepted_index<_Tp>;
197c757c 1436 if (index() == __index)
c42bc5d7 1437 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
197c757c 1438 else
86a57ce1
JW
1439 {
1440 using _Tj = __accepted_type<_Tp&&>;
1441 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1442 || !is_nothrow_move_constructible_v<_Tj>)
1443 this->emplace<__index>(std::forward<_Tp>(__rhs));
1444 else
1445 operator=(variant(std::forward<_Tp>(__rhs)));
1446 }
197c757c
TS
1447 return *this;
1448 }
1449
1450 template<typename _Tp, typename... _Args>
df990182
VV
1451 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1452 _Tp&>
458ef690 1453 emplace(_Args&&... __args)
197c757c 1454 {
59e36c85
JW
1455 constexpr size_t __index = __index_of<_Tp>;
1456 return this->emplace<__index>(std::forward<_Args>(__args)...);
197c757c
TS
1457 }
1458
1459 template<typename _Tp, typename _Up, typename... _Args>
458ef690 1460 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
df990182
VV
1461 && __exactly_once<_Tp>,
1462 _Tp&>
458ef690 1463 emplace(initializer_list<_Up> __il, _Args&&... __args)
197c757c 1464 {
59e36c85
JW
1465 constexpr size_t __index = __index_of<_Tp>;
1466 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
197c757c
TS
1467 }
1468
1469 template<size_t _Np, typename... _Args>
458ef690 1470 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
df990182
VV
1471 _Args...>,
1472 variant_alternative_t<_Np, variant>&>
458ef690 1473 emplace(_Args&&... __args)
197c757c
TS
1474 {
1475 static_assert(_Np < sizeof...(_Types),
801b2266 1476 "The index must be in [0, number of alternatives)");
caf80d87 1477 using type = variant_alternative_t<_Np, variant>;
10f26de9
JW
1478 // Provide the strong exception-safety guarantee when possible,
1479 // to avoid becoming valueless.
1480 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1481 {
1482 this->_M_reset();
1483 __variant_construct_by_index<_Np>(*this,
1484 std::forward<_Args>(__args)...);
1485 }
1486 else if constexpr (is_scalar_v<type>)
caf80d87 1487 {
10f26de9
JW
1488 // This might invoke a potentially-throwing conversion operator:
1489 const type __tmp(std::forward<_Args>(__args)...);
1490 // But these steps won't throw:
1491 this->_M_reset();
1492 __variant_construct_by_index<_Np>(*this, __tmp);
1493 }
47a468bd
JW
1494 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1495 && _Traits::_S_move_assign)
10f26de9
JW
1496 {
1497 // This construction might throw:
caf80d87
JW
1498 variant __tmp(in_place_index<_Np>,
1499 std::forward<_Args>(__args)...);
10f26de9 1500 // But _Never_valueless_alt<type> means this won't:
caf80d87 1501 *this = std::move(__tmp);
197c757c 1502 }
10f26de9 1503 else
197c757c 1504 {
10f26de9
JW
1505 // This case only provides the basic exception-safety guarantee,
1506 // i.e. the variant can become valueless.
1507 this->_M_reset();
1508 __try
1509 {
1510 __variant_construct_by_index<_Np>(*this,
1511 std::forward<_Args>(__args)...);
1512 }
1513 __catch (...)
1514 {
d7aa21a3
JW
1515 using __index_type = decltype(this->_M_index);
1516 this->_M_index = static_cast<__index_type>(variant_npos);
10f26de9
JW
1517 __throw_exception_again;
1518 }
197c757c 1519 }
df990182 1520 return std::get<_Np>(*this);
197c757c
TS
1521 }
1522
1523 template<size_t _Np, typename _Up, typename... _Args>
458ef690 1524 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
df990182
VV
1525 initializer_list<_Up>&, _Args...>,
1526 variant_alternative_t<_Np, variant>&>
458ef690 1527 emplace(initializer_list<_Up> __il, _Args&&... __args)
197c757c
TS
1528 {
1529 static_assert(_Np < sizeof...(_Types),
801b2266 1530 "The index must be in [0, number of alternatives)");
caf80d87 1531 using type = variant_alternative_t<_Np, variant>;
10f26de9
JW
1532 // Provide the strong exception-safety guarantee when possible,
1533 // to avoid becoming valueless.
1534 if constexpr (is_nothrow_constructible_v<type,
1535 initializer_list<_Up>&,
1536 _Args...>)
caf80d87 1537 {
10f26de9
JW
1538 this->_M_reset();
1539 __variant_construct_by_index<_Np>(*this, __il,
1540 std::forward<_Args>(__args)...);
1541 }
47a468bd
JW
1542 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1543 && _Traits::_S_move_assign)
10f26de9
JW
1544 {
1545 // This construction might throw:
caf80d87
JW
1546 variant __tmp(in_place_index<_Np>, __il,
1547 std::forward<_Args>(__args)...);
10f26de9 1548 // But _Never_valueless_alt<type> means this won't:
caf80d87 1549 *this = std::move(__tmp);
197c757c 1550 }
10f26de9 1551 else
197c757c 1552 {
10f26de9
JW
1553 // This case only provides the basic exception-safety guarantee,
1554 // i.e. the variant can become valueless.
1555 this->_M_reset();
1556 __try
1557 {
1558 __variant_construct_by_index<_Np>(*this, __il,
1559 std::forward<_Args>(__args)...);
1560 }
1561 __catch (...)
1562 {
d7aa21a3
JW
1563 using __index_type = decltype(this->_M_index);
1564 this->_M_index = static_cast<__index_type>(variant_npos);
10f26de9
JW
1565 __throw_exception_again;
1566 }
197c757c 1567 }
df990182 1568 return std::get<_Np>(*this);
197c757c
TS
1569 }
1570
1571 constexpr bool valueless_by_exception() const noexcept
1572 { return !this->_M_valid(); }
1573
1574 constexpr size_t index() const noexcept
1e8822d3
JW
1575 {
1576 using __index_type = typename _Base::__index_type;
fe69bee3 1577 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1e8822d3
JW
1578 return this->_M_index;
1579 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1580 return make_signed_t<__index_type>(this->_M_index);
1581 else
1582 return size_t(__index_type(this->_M_index + 1)) - 1;
1583 }
197c757c
TS
1584
1585 void
1586 swap(variant& __rhs)
258ee761 1587 noexcept((__is_nothrow_swappable<_Types>::value && ...)
458ef690 1588 && is_nothrow_move_constructible_v<variant>)
197c757c 1589 {
956a62aa
JW
1590 __detail::__variant::__raw_idx_visit(
1591 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
197c757c 1592 {
f1ba6c5a 1593 if constexpr (__rhs_index != variant_npos)
197c757c 1594 {
f1ba6c5a 1595 if (this->index() == __rhs_index)
669a6fdc 1596 {
f1ba6c5a
VV
1597 auto& __this_mem =
1598 std::get<__rhs_index>(*this);
669a6fdc
VV
1599 using std::swap;
1600 swap(__this_mem, __rhs_mem);
1601 }
f1ba6c5a
VV
1602 else
1603 {
b62dcd16 1604 if (!this->valueless_by_exception()) [[__likely__]]
f1ba6c5a
VV
1605 {
1606 auto __tmp(std::move(__rhs_mem));
1607 __rhs = std::move(*this);
1608 this->_M_destructive_move(__rhs_index,
1609 std::move(__tmp));
1610 }
1611 else
1612 {
1613 this->_M_destructive_move(__rhs_index,
1614 std::move(__rhs_mem));
1615 __rhs._M_reset();
1616 }
1617 }
197c757c 1618 }
669a6fdc
VV
1619 else
1620 {
b62dcd16 1621 if (!this->valueless_by_exception()) [[__likely__]]
669a6fdc 1622 {
f1ba6c5a 1623 __rhs = std::move(*this);
669a6fdc
VV
1624 this->_M_reset();
1625 }
669a6fdc 1626 }
f1ba6c5a 1627 }, __rhs);
197c757c
TS
1628 }
1629
9189f559 1630 private:
9189f559 1631
08233f0d 1632#if defined(__clang__) && __clang_major__ <= 7
aafaa325
JW
1633 public:
1634 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1635 private:
1636#endif
1637
9189f559 1638 template<size_t _Np, typename _Vp>
4b7a3ab8
JW
1639 friend constexpr decltype(auto)
1640 __detail::__variant::__get(_Vp&& __v) noexcept;
9189f559 1641
197c757c 1642 template<typename _Vp>
4b7a3ab8
JW
1643 friend void*
1644 __detail::__variant::__get_storage(_Vp&& __v) noexcept;
9189f559 1645
d255829b
TS
1646#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1647 template<typename... _Tp> \
1648 friend constexpr bool \
1649 operator __OP(const variant<_Tp...>& __lhs, \
1650 const variant<_Tp...>& __rhs);
1651
1652 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1653 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1654 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1655 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1656 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1657 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
9189f559 1658
d255829b 1659#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
197c757c
TS
1660 };
1661
197c757c 1662 template<size_t _Np, typename... _Types>
9189f559 1663 constexpr variant_alternative_t<_Np, variant<_Types...>>&
197c757c
TS
1664 get(variant<_Types...>& __v)
1665 {
1666 static_assert(_Np < sizeof...(_Types),
801b2266 1667 "The index must be in [0, number of alternatives)");
197c757c 1668 if (__v.index() != _Np)
c43c3af2 1669 __throw_bad_variant_access(__v.valueless_by_exception());
9189f559 1670 return __detail::__variant::__get<_Np>(__v);
197c757c
TS
1671 }
1672
1673 template<size_t _Np, typename... _Types>
9189f559 1674 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
197c757c
TS
1675 get(variant<_Types...>&& __v)
1676 {
1677 static_assert(_Np < sizeof...(_Types),
801b2266 1678 "The index must be in [0, number of alternatives)");
197c757c 1679 if (__v.index() != _Np)
c43c3af2 1680 __throw_bad_variant_access(__v.valueless_by_exception());
9189f559 1681 return __detail::__variant::__get<_Np>(std::move(__v));
197c757c
TS
1682 }
1683
1684 template<size_t _Np, typename... _Types>
9189f559 1685 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
197c757c
TS
1686 get(const variant<_Types...>& __v)
1687 {
1688 static_assert(_Np < sizeof...(_Types),
801b2266 1689 "The index must be in [0, number of alternatives)");
197c757c 1690 if (__v.index() != _Np)
c43c3af2 1691 __throw_bad_variant_access(__v.valueless_by_exception());
9189f559 1692 return __detail::__variant::__get<_Np>(__v);
197c757c
TS
1693 }
1694
1695 template<size_t _Np, typename... _Types>
9189f559 1696 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
197c757c
TS
1697 get(const variant<_Types...>&& __v)
1698 {
1699 static_assert(_Np < sizeof...(_Types),
801b2266 1700 "The index must be in [0, number of alternatives)");
197c757c 1701 if (__v.index() != _Np)
c43c3af2 1702 __throw_bad_variant_access(__v.valueless_by_exception());
9189f559 1703 return __detail::__variant::__get<_Np>(std::move(__v));
197c757c
TS
1704 }
1705
956a62aa 1706 template<typename _Result_type, typename _Visitor, typename... _Variants>
b01af236 1707 constexpr decltype(auto)
669a6fdc 1708 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
197c757c 1709 {
b01af236
TS
1710 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1711 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1712
1713 auto __func_ptr = __vtable._M_access(__variants.index()...);
64626fca 1714 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
b01af236 1715 std::forward<_Variants>(__variants)...);
197c757c
TS
1716 }
1717
3427e313
VV
1718 template<typename _Tp, typename... _Types>
1719 constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1720
02cbd79e 1721 template <size_t _Idx, typename _Visitor, typename _Variant>
3427e313
VV
1722 decltype(auto)
1723 __check_visitor_result(_Visitor&& __vis, _Variant&& __variant)
1724 {
1725 return std::__invoke(std::forward<_Visitor>(__vis),
1726 std::get<_Idx>(std::forward<_Variant>(__variant)));
1727 }
1728
02cbd79e 1729 template <typename _Visitor, typename _Variant, size_t... _Idxs>
3427e313
VV
1730 constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1731 {
1732 return __same_types<decltype(__check_visitor_result<_Idxs>(
1733 std::declval<_Visitor>(),
1734 std::declval<_Variant>()))...>;
1735 }
1736
1737
669a6fdc
VV
1738 template<typename _Visitor, typename... _Variants>
1739 constexpr decltype(auto)
1740 visit(_Visitor&& __visitor, _Variants&&... __variants)
1741 {
1742 if ((__variants.valueless_by_exception() || ...))
c43c3af2 1743 __throw_bad_variant_access("std::visit: variant is valueless");
669a6fdc 1744
956a62aa
JW
1745 using _Result_type = std::invoke_result_t<_Visitor,
1746 decltype(std::get<0>(std::declval<_Variants>()))...>;
1747
1748 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1749
3427e313
VV
1750 if constexpr (sizeof...(_Variants) == 1)
1751 {
1752 constexpr bool __visit_rettypes_match =
1753 __check_visitor_results<_Visitor, _Variants...>(
1754 std::make_index_sequence<
1755 std::variant_size<remove_reference_t<_Variants>...>::value>());
1756 if constexpr (!__visit_rettypes_match)
1757 {
1758 static_assert(__visit_rettypes_match,
1759 "std::visit requires the visitor to have the same "
1760 "return type for all alternatives of a variant");
1761 return;
1762 }
1763 else
1764 return std::__do_visit<_Tag>(
1765 std::forward<_Visitor>(__visitor),
1766 std::forward<_Variants>(__variants)...);
1767 }
1768 else
1769 return std::__do_visit<_Tag>(
1770 std::forward<_Visitor>(__visitor),
1771 std::forward<_Variants>(__variants)...);
669a6fdc
VV
1772 }
1773
199b20e3
JW
1774#if __cplusplus > 201703L
1775 template<typename _Res, typename _Visitor, typename... _Variants>
1776 constexpr _Res
1777 visit(_Visitor&& __visitor, _Variants&&... __variants)
1778 {
1779 if ((__variants.valueless_by_exception() || ...))
c43c3af2 1780 __throw_bad_variant_access("std::visit<R>: variant is valueless");
199b20e3 1781
e5d7010b
JW
1782 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1783 std::forward<_Variants>(__variants)...);
199b20e3
JW
1784 }
1785#endif
1786
509912a6
VV
1787 template<bool, typename... _Types>
1788 struct __variant_hash_call_base_impl
197c757c 1789 {
197c757c
TS
1790 size_t
1791 operator()(const variant<_Types...>& __t) const
7dcc645c 1792 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
197c757c 1793 {
669a6fdc 1794 size_t __ret;
956a62aa
JW
1795 __detail::__variant::__raw_visit(
1796 [&__t, &__ret](auto&& __t_mem) mutable
197c757c 1797 {
669a6fdc
VV
1798 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1799 if constexpr (!is_same_v<_Type,
1800 __detail::__variant::__variant_cookie>)
1801 __ret = std::hash<size_t>{}(__t.index())
1802 + std::hash<_Type>{}(__t_mem);
1803 else
1804 __ret = std::hash<size_t>{}(__t.index());
669a6fdc
VV
1805 }, __t);
1806 return __ret;
197c757c
TS
1807 }
1808 };
1809
509912a6
VV
1810 template<typename... _Types>
1811 struct __variant_hash_call_base_impl<false, _Types...> {};
1812
1813 template<typename... _Types>
1814 using __variant_hash_call_base =
1815 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1816 __enable_hash_call &&...), _Types...>;
1817
1818 template<typename... _Types>
1819 struct hash<variant<_Types...>>
1820 : private __detail::__variant::_Variant_hash_base<
4a15d842 1821 variant<_Types...>, std::index_sequence_for<_Types...>>,
509912a6
VV
1822 public __variant_hash_call_base<_Types...>
1823 {
f78958c9
JW
1824 using result_type [[__deprecated__]] = size_t;
1825 using argument_type [[__deprecated__]] = variant<_Types...>;
509912a6
VV
1826 };
1827
197c757c
TS
1828 template<>
1829 struct hash<monostate>
1830 {
f78958c9
JW
1831 using result_type [[__deprecated__]] = size_t;
1832 using argument_type [[__deprecated__]] = monostate;
197c757c
TS
1833
1834 size_t
801b2266 1835 operator()(const monostate&) const noexcept
197c757c
TS
1836 {
1837 constexpr size_t __magic_monostate_hash = -7777;
1838 return __magic_monostate_hash;
1839 }
1840 };
1841
f78958c9
JW
1842 template<typename... _Types>
1843 struct __is_fast_hash<hash<variant<_Types...>>>
1844 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1845 { };
1846
197c757c
TS
1847_GLIBCXX_END_NAMESPACE_VERSION
1848} // namespace std
1849
1850#endif // C++17
1851
1852#endif // _GLIBCXX_VARIANT