libstdc++
variant
Go to the documentation of this file.
1// <variant> -*- C++ -*-
2
3// Copyright (C) 2016-2026 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#define __glibcxx_want_constexpr_exceptions
40#include <bits/version.h>
41
42#ifdef __cpp_lib_variant // C++ >= 17
43#include <initializer_list>
44#include <type_traits>
48#include <bits/invoke.h>
49#include <bits/monostate.h>
50#include <bits/parse_numbers.h> // _Select_int
52#include <bits/stl_construct.h>
53#include <bits/utility.h> // in_place_index_t
54#if __cplusplus >= 202002L
55# include <concepts>
56# include <compare>
57#endif
58
59// C++ < 20 || __cpp_concepts < 202002L || __cpp_constexpr < 201811L
60#if __cpp_lib_variant < 202106L
61# include <ext/aligned_buffer.h> // Use __aligned_membuf for storage.
62#endif
63
64namespace std _GLIBCXX_VISIBILITY(default)
65{
66_GLIBCXX_BEGIN_NAMESPACE_VERSION
67
68 template<typename... _Types> class tuple;
69 template<typename... _Types> class variant;
70
71 template<typename _Variant>
72 struct variant_size;
73
74 template<typename _Variant>
75 struct variant_size<const _Variant> : variant_size<_Variant> {};
76
77 template<typename _Variant>
78 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
79
80 template<typename _Variant>
81 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
82
83 template<typename... _Types>
84 struct variant_size<variant<_Types...>>
85 : std::integral_constant<size_t, sizeof...(_Types)> {};
86
87 template<typename _Variant>
88 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
89
90 template<typename... _Types>
91 inline constexpr size_t
92 variant_size_v<variant<_Types...>> = sizeof...(_Types);
93
94 template<typename... _Types>
95 inline constexpr size_t
96 variant_size_v<const variant<_Types...>> = sizeof...(_Types);
97
98 template<size_t _Np, typename _Variant>
99 struct variant_alternative;
100
101 template<size_t _Np, typename... _Types>
102 struct variant_alternative<_Np, variant<_Types...>>
103 {
104 static_assert(_Np < sizeof...(_Types));
105
106 using type = typename _Nth_type<_Np, _Types...>::type;
107 };
108
109 template<size_t _Np, typename _Variant>
110 using variant_alternative_t =
111 typename variant_alternative<_Np, _Variant>::type;
112
113 template<size_t _Np, typename _Variant>
114 struct variant_alternative<_Np, const _Variant>
115 { using type = const variant_alternative_t<_Np, _Variant>; };
116
117 template<size_t _Np, typename _Variant>
118 struct variant_alternative<_Np, volatile _Variant>
119 { using type = volatile variant_alternative_t<_Np, _Variant>; };
120
121 template<size_t _Np, typename _Variant>
122 struct variant_alternative<_Np, const volatile _Variant>
123 { using type = const volatile variant_alternative_t<_Np, _Variant>; };
124
125 inline constexpr size_t variant_npos = -1;
126
127 template<size_t _Np, typename... _Types>
128 constexpr variant_alternative_t<_Np, variant<_Types...>>&
129 get(variant<_Types...>&);
130
131 template<size_t _Np, typename... _Types>
132 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
133 get(variant<_Types...>&&);
134
135 template<size_t _Np, typename... _Types>
136 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
137 get(const variant<_Types...>&);
138
139 template<size_t _Np, typename... _Types>
140 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
141 get(const variant<_Types...>&&);
142
143 template<typename _Result_type, typename _Visitor, typename... _Variants>
144 constexpr decltype(auto)
145 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
146
147 template <typename... _Types, typename _Tp>
148 _GLIBCXX20_CONSTEXPR
149 decltype(auto)
150 __variant_cast(_Tp&& __rhs)
151 {
152 if constexpr (is_lvalue_reference_v<_Tp>)
153 {
154 if constexpr (is_const_v<remove_reference_t<_Tp>>)
155 return static_cast<const variant<_Types...>&>(__rhs);
156 else
157 return static_cast<variant<_Types...>&>(__rhs);
158 }
159 else
160 return static_cast<variant<_Types...>&&>(__rhs);
161 }
162
163namespace __detail
164{
165namespace __variant
166{
167 // used for raw visitation
168 struct __variant_cookie {};
169 // used for raw visitation with indices passed in
170 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
171 // Used to enable deduction (and same-type checking) for std::visit:
172 template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
173
174 // Visit variants that might be valueless.
175 template<typename _Visitor, typename... _Variants>
176 constexpr void
177 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
178 {
179 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
180 std::forward<_Variants>(__variants)...);
181 }
182
183 // Visit variants that might be valueless, passing indices to the visitor.
184 template<typename _Visitor, typename... _Variants>
185 constexpr void
186 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
187 {
188 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
189 std::forward<_Variants>(__variants)...);
190 }
191
192 // The __as function templates implement the exposition-only "as-variant"
193
194 template<typename... _Types>
195 constexpr std::variant<_Types...>&
196 __as(std::variant<_Types...>& __v) noexcept
197 { return __v; }
198
199 template<typename... _Types>
200 constexpr const std::variant<_Types...>&
201 __as(const std::variant<_Types...>& __v) noexcept
202 { return __v; }
203
204 template<typename... _Types>
205 constexpr std::variant<_Types...>&&
206 __as(std::variant<_Types...>&& __v) noexcept
207 { return std::move(__v); }
208
209 template<typename... _Types>
210 constexpr const std::variant<_Types...>&&
211 __as(const std::variant<_Types...>&& __v) noexcept
212 { return std::move(__v); }
213
214#if (__cpp_lib_variant < 202106L) && defined(_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI)
215 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
216 struct _Uninitialized;
217#else
218 template<typename _Type, bool /* unused */ = true>
219 struct _Uninitialized;
220#endif
221
222 // The primary template is used for trivially destructible types in C++17,
223 // and for all types in C++20.
224 template<typename _Type, bool>
225 struct _Uninitialized
226 {
227 template<typename... _Args>
228 constexpr
229 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
230 : _M_storage(std::forward<_Args>(__args)...)
231 { }
232
233#if (__cpp_lib_variant < 202106L) && defined(_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI)
234 constexpr const _Type& _M_get() const & noexcept
235 { return _M_storage; }
236
237 constexpr _Type& _M_get() & noexcept
238 { return _M_storage; }
239
240 constexpr const _Type&& _M_get() const && noexcept
241 { return std::move(_M_storage); }
242
243 constexpr _Type&& _M_get() && noexcept
244 { return std::move(_M_storage); }
245#endif
246
247 _Type _M_storage;
248 };
249
250#if (__cpp_lib_variant < 202106L) && defined(_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI)
251 // This partial specialization is used for non-trivially destructible types
252 // in C++17.
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 // !_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI
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 // !_GLIBCXX_USE_VARIANT_CXX17_OLD_ABI
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#if __cpp_lib_constexpr_exceptions >= 202502L
1414#define _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS constexpr
1415#else
1416#define _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS
1417#endif
1418 [[noreturn]] _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS void
1419 __throw_bad_variant_access(unsigned);
1420
1421 class bad_variant_access : public exception
1422 {
1423 public:
1424 _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS bad_variant_access() noexcept { }
1425
1426 _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS const char* what()
1427 const noexcept override
1428 { return _M_reason; }
1429
1430 private:
1431 // Must only be called with a string literal
1432 _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS
1433 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1434
1435 // Must point to a string with static storage duration:
1436 const char* _M_reason = "bad variant access";
1437
1438 friend _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS void
1439 __throw_bad_variant_access([[maybe_unused]] unsigned __n)
1440 {
1441 [[maybe_unused]] static constexpr const char* __reasons[] = {
1442 "std::get: wrong index for variant",
1443 "std::get: variant is valueless",
1444 "std::visit: variant is valueless",
1445 "std::visit<R>: variant is valueless",
1446 };
1447 _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__reasons[__n % 4u]));
1448 }
1449#undef _GLIBCXX_CONSTEXPR_BAD_VARIANT_ACCESS
1450 };
1451
1452 template<typename... _Types>
1453 class variant
1454 : private __detail::__variant::_Variant_base<_Types...>,
1455 private _Enable_copy_move<
1456 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1457 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1458 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1459 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1460 variant<_Types...>>
1461 {
1462 private:
1463 template <typename... _UTypes, typename _Tp>
1464 friend _GLIBCXX20_CONSTEXPR decltype(auto)
1465 __variant_cast(_Tp&&);
1466
1467 static_assert(sizeof...(_Types) > 0,
1468 "variant must have at least one alternative");
1469 static_assert(((std::is_object_v<_Types> && !is_array_v<_Types>) && ...),
1470 "variant alternatives must be non-array object types");
1471
1472 using _Base = __detail::__variant::_Variant_base<_Types...>;
1473
1474 template<typename _Tp>
1475 static constexpr bool __not_self
1476 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1477
1478 template<typename _Tp>
1479 static constexpr bool
1480 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1481
1482 template<typename _Tp>
1483 static constexpr size_t __accepted_index
1484 = __detail::__variant::__accepted_index<_Tp, variant>;
1485
1486 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1487 using __to_type = typename _Nth_type<_Np, _Types...>::type;
1488
1489 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1490 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1491
1492 template<typename _Tp>
1493 static constexpr size_t __index_of
1494 = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1495
1496 using _Traits = __detail::__variant::_Traits<_Types...>;
1497
1498 template<typename _Tp>
1499 static constexpr bool __not_in_place_tag
1500 = !__is_in_place_type_v<__remove_cvref_t<_Tp>>
1501 && !__is_in_place_index_v<__remove_cvref_t<_Tp>>;
1502
1503 public:
1504#if __cpp_concepts
1505 variant() requires is_default_constructible_v<__to_type<0>> = default;
1506#else
1507 template<typename _Tp0 = __to_type<0>,
1508 typename = enable_if_t<is_default_constructible_v<_Tp0>>>
1509 constexpr
1510 variant() noexcept(is_nothrow_default_constructible_v<__to_type<0>>)
1511 { }
1512#endif
1513
1514 variant(const variant& __rhs) = default;
1515 variant(variant&&) = default;
1516 variant& operator=(const variant&) = default;
1517 variant& operator=(variant&&) = default;
1518 _GLIBCXX20_CONSTEXPR ~variant() = default;
1519
1520 template<typename _Tp,
1521 typename = enable_if_t<sizeof...(_Types) != 0>,
1522 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1523 typename _Tj = __accepted_type<_Tp&&>,
1524 typename = enable_if_t<__exactly_once<_Tj>
1525 && is_constructible_v<_Tj, _Tp>>>
1526 constexpr
1527 variant(_Tp&& __t)
1528 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1529 : variant(in_place_index<__accepted_index<_Tp>>,
1530 std::forward<_Tp>(__t))
1531 { }
1532
1533 template<typename _Tp, typename... _Args,
1534 typename = enable_if_t<__exactly_once<_Tp>
1535 && is_constructible_v<_Tp, _Args...>>>
1536 constexpr explicit
1537 variant(in_place_type_t<_Tp>, _Args&&... __args)
1538 : variant(in_place_index<__index_of<_Tp>>,
1539 std::forward<_Args>(__args)...)
1540 { }
1541
1542 template<typename _Tp, typename _Up, typename... _Args,
1543 typename = enable_if_t<__exactly_once<_Tp>
1544 && is_constructible_v<_Tp,
1545 initializer_list<_Up>&, _Args...>>>
1546 constexpr explicit
1547 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1548 _Args&&... __args)
1549 : variant(in_place_index<__index_of<_Tp>>, __il,
1550 std::forward<_Args>(__args)...)
1551 { }
1552
1553 template<size_t _Np, typename... _Args,
1554 typename _Tp = __to_type<_Np>,
1555 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1556 constexpr explicit
1557 variant(in_place_index_t<_Np>, _Args&&... __args)
1558 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...)
1559 { }
1560
1561 template<size_t _Np, typename _Up, typename... _Args,
1562 typename _Tp = __to_type<_Np>,
1563 typename = enable_if_t<is_constructible_v<_Tp,
1564 initializer_list<_Up>&,
1565 _Args...>>>
1566 constexpr explicit
1567 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1568 _Args&&... __args)
1569 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...)
1570 { }
1571
1572 template<typename _Tp>
1573 _GLIBCXX20_CONSTEXPR
1574 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1575 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1576 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1577 variant&>
1578 operator=(_Tp&& __rhs)
1579 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1580 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1581 {
1582 constexpr auto __index = __accepted_index<_Tp>;
1583 if (index() == __index)
1584 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1585 else
1586 {
1587 using _Tj = __accepted_type<_Tp&&>;
1588 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1589 || !is_nothrow_move_constructible_v<_Tj>)
1590 this->emplace<__index>(std::forward<_Tp>(__rhs));
1591 else
1592 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1593 // 3585. converting assignment with immovable alternative
1594 this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
1595 }
1596 return *this;
1597 }
1598
1599 template<typename _Tp, typename... _Args>
1600 _GLIBCXX20_CONSTEXPR
1601 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1602 _Tp&>
1603 emplace(_Args&&... __args)
1604 {
1605 constexpr size_t __index = __index_of<_Tp>;
1606 return this->emplace<__index>(std::forward<_Args>(__args)...);
1607 }
1608
1609 template<typename _Tp, typename _Up, typename... _Args>
1610 _GLIBCXX20_CONSTEXPR
1611 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1612 && __exactly_once<_Tp>,
1613 _Tp&>
1614 emplace(initializer_list<_Up> __il, _Args&&... __args)
1615 {
1616 constexpr size_t __index = __index_of<_Tp>;
1617 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1618 }
1619
1620 template<size_t _Np, typename... _Args>
1621 _GLIBCXX20_CONSTEXPR
1622 enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1623 __to_type<_Np>&>
1624 emplace(_Args&&... __args)
1625 {
1626 namespace __variant = std::__detail::__variant;
1627 using type = typename _Nth_type<_Np, _Types...>::type;
1628 // Provide the strong exception-safety guarantee when possible,
1629 // to avoid becoming valueless.
1630 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1631 {
1632 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1633 }
1634 else if constexpr (is_scalar_v<type>)
1635 {
1636 // This might invoke a potentially-throwing conversion operator:
1637 const type __tmp(std::forward<_Args>(__args)...);
1638 // But this won't throw:
1639 __variant::__emplace<_Np>(*this, __tmp);
1640 }
1641 else if constexpr (__variant::_Never_valueless_alt<type>()
1642 && _Traits::_S_move_assign)
1643 {
1644 // This construction might throw:
1645 variant __tmp(in_place_index<_Np>,
1646 std::forward<_Args>(__args)...);
1647 // But _Never_valueless_alt<type> means this won't:
1648 *this = std::move(__tmp);
1649 }
1650 else
1651 {
1652 // This case only provides the basic exception-safety guarantee,
1653 // i.e. the variant can become valueless.
1654 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1655 }
1656 return std::get<_Np>(*this);
1657 }
1658
1659 template<size_t _Np, typename _Up, typename... _Args>
1660 _GLIBCXX20_CONSTEXPR
1661 enable_if_t<is_constructible_v<__to_type<_Np>,
1662 initializer_list<_Up>&, _Args...>,
1663 __to_type<_Np>&>
1664 emplace(initializer_list<_Up> __il, _Args&&... __args)
1665 {
1666 namespace __variant = std::__detail::__variant;
1667 using type = typename _Nth_type<_Np, _Types...>::type;
1668 // Provide the strong exception-safety guarantee when possible,
1669 // to avoid becoming valueless.
1670 if constexpr (is_nothrow_constructible_v<type,
1671 initializer_list<_Up>&,
1672 _Args...>)
1673 {
1674 __variant::__emplace<_Np>(*this, __il,
1675 std::forward<_Args>(__args)...);
1676 }
1677 else if constexpr (__variant::_Never_valueless_alt<type>()
1678 && _Traits::_S_move_assign)
1679 {
1680 // This construction might throw:
1681 variant __tmp(in_place_index<_Np>, __il,
1682 std::forward<_Args>(__args)...);
1683 // But _Never_valueless_alt<type> means this won't:
1684 *this = std::move(__tmp);
1685 }
1686 else
1687 {
1688 // This case only provides the basic exception-safety guarantee,
1689 // i.e. the variant can become valueless.
1690 __variant::__emplace<_Np>(*this, __il,
1691 std::forward<_Args>(__args)...);
1692 }
1693 return std::get<_Np>(*this);
1694 }
1695
1696 template<size_t _Np, typename... _Args>
1697 enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1698
1699 template<typename _Tp, typename... _Args>
1700 enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1701
1702 constexpr bool valueless_by_exception() const noexcept
1703 { return !this->_M_valid(); }
1704
1705 constexpr size_t index() const noexcept
1706 {
1707 using __index_type = typename _Base::__index_type;
1708 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1709 return this->_M_index;
1710 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1711 return make_signed_t<__index_type>(this->_M_index);
1712 else
1713 return size_t(__index_type(this->_M_index + 1)) - 1;
1714 }
1715
1716 _GLIBCXX20_CONSTEXPR
1717 void
1718 swap(variant& __rhs)
1719 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1720 && is_nothrow_move_constructible_v<variant>)
1721 {
1722 static_assert((is_move_constructible_v<_Types> && ...));
1723
1724 // Handle this here to simplify the visitation.
1725 if (__rhs.valueless_by_exception()) [[__unlikely__]]
1726 {
1727 if (!this->valueless_by_exception()) [[__likely__]]
1728 __rhs.swap(*this);
1729 return;
1730 }
1731
1732 namespace __variant = __detail::__variant;
1733
1734 __variant::__raw_idx_visit(
1735 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1736 {
1737 constexpr size_t __j = __rhs_index;
1738 if constexpr (__j != variant_npos)
1739 {
1740 if (this->index() == __j)
1741 {
1742 using std::swap;
1743 swap(std::get<__j>(*this), __rhs_mem);
1744 }
1745 else
1746 {
1747 auto __tmp(std::move(__rhs_mem));
1748
1749 if constexpr (_Traits::_S_trivial_move_assign)
1750 __rhs = std::move(*this);
1751 else
1752 __variant::__raw_idx_visit(
1753 [&__rhs](auto&& __this_mem, auto __this_index) mutable
1754 {
1755 constexpr size_t __k = __this_index;
1756 if constexpr (__k != variant_npos)
1757 __variant::__emplace<__k>(__rhs,
1758 std::move(__this_mem));
1759 }, *this);
1760
1761 __variant::__emplace<__j>(*this, std::move(__tmp));
1762 }
1763 }
1764 }, __rhs);
1765 }
1766
1767#if __cpp_lib_variant >= 202306L // >= C++26
1768 // [variant.visit], visitation
1769
1770 /** Simple visitation for a single variant
1771 *
1772 * To visit a single variant you can use `var.visit(visitor)`
1773 * instead of `std::visit(visitor, var)`.
1774 *
1775 * @since C++26
1776 */
1777 template<int = 0, typename _Self, typename _Visitor>
1778 constexpr decltype(auto)
1779 visit(this _Self&& __self, _Visitor&& __vis)
1780 {
1781 using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
1782 const variant, variant>;
1783 using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
1784 _CVar&&, _CVar&>;
1785 return std::visit(std::forward<_Visitor>(__vis), (_Var)__self);
1786 }
1787
1788 /** Simple visitation for a single variant, with explicit return type
1789 *
1790 * To visit a single variant you can use `var.visit<R>(visitor)`
1791 * instead of `std::visit<R>(visitor, var)`.
1792 *
1793 * @since C++26
1794 */
1795 template<typename _Res, typename _Self, typename _Visitor>
1796 constexpr _Res
1797 visit(this _Self&& __self, _Visitor&& __vis)
1798 {
1799 using _CVar = __conditional_t<is_const_v<remove_reference_t<_Self>>,
1800 const variant, variant>;
1801 using _Var = __conditional_t<is_rvalue_reference_v<_Self&&>,
1802 _CVar&&, _CVar&>;
1803 return std::visit<_Res>(std::forward<_Visitor>(__vis), (_Var)__self);
1804 }
1805#endif
1806
1807 private:
1808 template<size_t _Np, typename _Vp>
1809 friend constexpr decltype(auto)
1810 __detail::__variant::__get(_Vp&& __v) noexcept;
1811 };
1812
1813 template<size_t _Np, typename... _Types>
1814 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1815 get(variant<_Types...>& __v)
1816 {
1817 static_assert(_Np < sizeof...(_Types),
1818 "The index must be in [0, number of alternatives)");
1819 if (__v.index() != _Np)
1820 __throw_bad_variant_access(__v.valueless_by_exception());
1821 return __detail::__variant::__get<_Np>(__v);
1822 }
1823
1824 template<size_t _Np, typename... _Types>
1825 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1826 get(variant<_Types...>&& __v)
1827 {
1828 static_assert(_Np < sizeof...(_Types),
1829 "The index must be in [0, number of alternatives)");
1830 if (__v.index() != _Np)
1831 __throw_bad_variant_access(__v.valueless_by_exception());
1832 return __detail::__variant::__get<_Np>(std::move(__v));
1833 }
1834
1835 template<size_t _Np, typename... _Types>
1836 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1837 get(const variant<_Types...>& __v)
1838 {
1839 static_assert(_Np < sizeof...(_Types),
1840 "The index must be in [0, number of alternatives)");
1841 if (__v.index() != _Np)
1842 __throw_bad_variant_access(__v.valueless_by_exception());
1843 return __detail::__variant::__get<_Np>(__v);
1844 }
1845
1846 template<size_t _Np, typename... _Types>
1847 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1848 get(const variant<_Types...>&& __v)
1849 {
1850 static_assert(_Np < sizeof...(_Types),
1851 "The index must be in [0, number of alternatives)");
1852 if (__v.index() != _Np)
1853 __throw_bad_variant_access(__v.valueless_by_exception());
1854 return __detail::__variant::__get<_Np>(std::move(__v));
1855 }
1856
1857 /// @cond undocumented
1858 template<typename _Result_type, typename _Visitor, typename... _Variants>
1859 constexpr decltype(auto)
1860 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1861 {
1862 // Get the silly case of visiting no variants out of the way first.
1863 if constexpr (sizeof...(_Variants) == 0)
1864 {
1865 if constexpr (is_void_v<_Result_type>)
1866 return (void) std::forward<_Visitor>(__visitor)();
1867 else
1868 return std::forward<_Visitor>(__visitor)();
1869 }
1870 else
1871 {
1872 constexpr size_t __max = 11; // "These go to eleven."
1873
1874 // The type of the first variant in the pack.
1875 using _V0 = typename _Nth_type<0, _Variants...>::type;
1876 // The number of alternatives in that first variant.
1877 constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1878
1879 if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1880 {
1881 // Use a jump table for the general case.
1882 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1883 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1884
1885 auto __func_ptr = __vtable._M_access(__variants.index()...);
1886 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1887 std::forward<_Variants>(__variants)...);
1888 }
1889 else // We have a single variant with a small number of alternatives.
1890 {
1891 // A name for the first variant in the pack.
1892 _V0& __v0
1893 = [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1894
1895 using __detail::__variant::_Multi_array;
1896 using __detail::__variant::__gen_vtable_impl;
1897 using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1898
1899#ifdef _GLIBCXX_DEBUG
1900# define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1901#else
1902# define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1903#endif
1904
1905#define _GLIBCXX_VISIT_CASE(N) \
1906 case N: \
1907 { \
1908 if constexpr (N < __n) \
1909 { \
1910 return __gen_vtable_impl<_Ma, index_sequence<N>>:: \
1911 __visit_invoke(std::forward<_Visitor>(__visitor), \
1912 std::forward<_V0>(__v0)); \
1913 } \
1914 else _GLIBCXX_VISIT_UNREACHABLE(); \
1915 }
1916
1917 switch (__v0.index())
1918 {
1919 _GLIBCXX_VISIT_CASE(0)
1920 _GLIBCXX_VISIT_CASE(1)
1921 _GLIBCXX_VISIT_CASE(2)
1922 _GLIBCXX_VISIT_CASE(3)
1923 _GLIBCXX_VISIT_CASE(4)
1924 _GLIBCXX_VISIT_CASE(5)
1925 _GLIBCXX_VISIT_CASE(6)
1926 _GLIBCXX_VISIT_CASE(7)
1927 _GLIBCXX_VISIT_CASE(8)
1928 _GLIBCXX_VISIT_CASE(9)
1929 _GLIBCXX_VISIT_CASE(10)
1930 case variant_npos:
1931 using __detail::__variant::__variant_idx_cookie;
1932 using __detail::__variant::__variant_cookie;
1933 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1934 || is_same_v<_Result_type, __variant_cookie>)
1935 {
1936 using _Npos = index_sequence<variant_npos>;
1937 return __gen_vtable_impl<_Ma, _Npos>::
1938 __visit_invoke(std::forward<_Visitor>(__visitor),
1939 std::forward<_V0>(__v0));
1940 }
1941 else
1942 _GLIBCXX_VISIT_UNREACHABLE();
1943 default:
1944 _GLIBCXX_VISIT_UNREACHABLE();
1945 }
1946#undef _GLIBCXX_VISIT_CASE
1947#undef _GLIBCXX_VISIT_UNREACHABLE
1948 }
1949 }
1950 }
1951 /// @endcond
1952
1953 template<typename _Visitor, typename... _Variants>
1954 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1955 visit(_Visitor&& __visitor, _Variants&&... __variants)
1956 {
1957 namespace __variant = std::__detail::__variant;
1958
1959 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1960 __throw_bad_variant_access(2);
1961
1962 using _Result_type
1963 = __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1964
1965 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1966
1967 if constexpr (sizeof...(_Variants) == 1)
1968 {
1969 using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1970
1971 constexpr bool __visit_rettypes_match = __detail::__variant::
1972 __check_visitor_results<_Visitor, _Vp>(
1973 make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1974 if constexpr (!__visit_rettypes_match)
1975 {
1976 static_assert(__visit_rettypes_match,
1977 "std::visit requires the visitor to have the same "
1978 "return type for all alternatives of a variant");
1979 return;
1980 }
1981 else
1982 return std::__do_visit<_Tag>(
1983 std::forward<_Visitor>(__visitor),
1984 static_cast<_Vp>(__variants)...);
1985 }
1986 else
1987 return std::__do_visit<_Tag>(
1988 std::forward<_Visitor>(__visitor),
1989 __variant::__as(std::forward<_Variants>(__variants))...);
1990 }
1991
1992#if __cplusplus > 201703L
1993 template<typename _Res, typename _Visitor, typename... _Variants>
1994 constexpr _Res
1995 visit(_Visitor&& __visitor, _Variants&&... __variants)
1996 {
1997 namespace __variant = std::__detail::__variant;
1998
1999 if ((__variant::__as(__variants).valueless_by_exception() || ...))
2000 __throw_bad_variant_access(3);
2001
2002 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
2003 __variant::__as(std::forward<_Variants>(__variants))...);
2004 }
2005#endif
2006
2007 /// @cond undocumented
2008 template<typename... _Types>
2009 struct __variant_hash
2010 {
2011#if __cplusplus < 202002L
2012 using result_type [[__deprecated__]] = size_t;
2013 using argument_type [[__deprecated__]] = variant<_Types...>;
2014#endif
2015
2016 size_t
2017 operator()(const variant<_Types...>& __t) const
2018 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
2019 {
2020 size_t __ret;
2021 __detail::__variant::__raw_visit(
2022 [&__t, &__ret](auto&& __t_mem) mutable
2023 {
2024 using _Type = __remove_cvref_t<decltype(__t_mem)>;
2025 if constexpr (!is_same_v<_Type,
2026 __detail::__variant::__variant_cookie>)
2027 __ret = std::hash<size_t>{}(__t.index())
2028 + std::hash<_Type>{}(__t_mem);
2029 else
2030 __ret = std::hash<size_t>{}(__t.index());
2031 }, __t);
2032 return __ret;
2033 }
2034 };
2035 /// @endcond
2036
2037 template<typename... _Types>
2038 struct hash<variant<_Types...>>
2039 : __conditional_t<(__is_hash_enabled_for<remove_const_t<_Types>> && ...),
2040 __variant_hash<_Types...>,
2041 __hash_not_enabled<variant<_Types...>>>
2042#if ! _GLIBCXX_INLINE_VERSION
2043 , __detail::__variant::_Variant_hash_base<variant<_Types...>,
2044 index_sequence_for<_Types...>>
2045#endif
2046 { };
2047
2048 template<typename... _Types>
2049 struct __is_fast_hash<hash<variant<_Types...>>>
2050 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
2051 { };
2052
2053_GLIBCXX_END_NAMESPACE_VERSION
2054} // namespace std
2055
2056#endif // __cpp_lib_variant
2057#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:1853
typename add_pointer< _Tp >::type add_pointer_t
Alias template for add_pointer.
Definition type_traits:2323
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2907
__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:2681
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
[concept.convertible], concept convertible_to
Definition concepts:81