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