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