1// <variant> -*- C++ -*-
3// Copyright (C) 2016-2022 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 * This is the `<variant>` C++ Library header.
29#ifndef _GLIBCXX_VARIANT
30#define _GLIBCXX_VARIANT 1
32#pragma GCC system_header
34#if __cplusplus >= 201703L
36#include <initializer_list>
38#include <bits/enable_special_members.h>
39#include <bits/exception_defines.h>
40#include <bits/functional_hash.h>
41#include <bits/invoke.h>
42#include <bits/parse_numbers.h>
43#include <bits/stl_iterator_base_types.h>
44#include <bits/stl_iterator_base_funcs.h>
45#include <bits/stl_construct.h>
46#include <bits/utility.h> // in_place_index_t
47#if __cplusplus >= 202002L
51#if __cpp_concepts >= 202002L && __cpp_constexpr >= 201811L
52// P2231R1 constexpr needs constexpr unions and constrained destructors.
53# define __cpp_lib_variant 202106L
55# include <ext/aligned_buffer.h> // Use __aligned_membuf instead of union.
56# define __cpp_lib_variant 202102L
59namespace std _GLIBCXX_VISIBILITY(default)
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
63 template<typename... _Types> class tuple;
64 template<typename... _Types> class variant;
65 template <typename> struct hash;
67 template<typename _Variant>
70 template<typename _Variant>
71 struct variant_size<const _Variant> : variant_size<_Variant> {};
73 template<typename _Variant>
74 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
76 template<typename _Variant>
77 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
79 template<typename... _Types>
80 struct variant_size<variant<_Types...>>
81 : std::integral_constant<size_t, sizeof...(_Types)> {};
83 template<typename _Variant>
84 inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
86 template<typename... _Types>
87 inline constexpr size_t
88 variant_size_v<variant<_Types...>> = sizeof...(_Types);
90 template<typename... _Types>
91 inline constexpr size_t
92 variant_size_v<const variant<_Types...>> = sizeof...(_Types);
94 template<size_t _Np, typename _Variant>
95 struct variant_alternative;
97 template<size_t _Np, typename... _Types>
98 struct variant_alternative<_Np, variant<_Types...>>
100 static_assert(_Np < sizeof...(_Types));
102 using type = typename _Nth_type<_Np, _Types...>::type;
105 template<size_t _Np, typename _Variant>
106 using variant_alternative_t =
107 typename variant_alternative<_Np, _Variant>::type;
109 template<size_t _Np, typename _Variant>
110 struct variant_alternative<_Np, const _Variant>
111 { using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
113 template<size_t _Np, typename _Variant>
114 struct variant_alternative<_Np, volatile _Variant>
115 { using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
117 template<size_t _Np, typename _Variant>
118 struct variant_alternative<_Np, const volatile _Variant>
119 { using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
121 inline constexpr size_t variant_npos = -1;
123 template<size_t _Np, typename... _Types>
124 constexpr variant_alternative_t<_Np, variant<_Types...>>&
125 get(variant<_Types...>&);
127 template<size_t _Np, typename... _Types>
128 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
129 get(variant<_Types...>&&);
131 template<size_t _Np, typename... _Types>
132 constexpr variant_alternative_t<_Np, variant<_Types...>> const&
133 get(const variant<_Types...>&);
135 template<size_t _Np, typename... _Types>
136 constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
137 get(const variant<_Types...>&&);
139 template<typename _Result_type, typename _Visitor, typename... _Variants>
140 constexpr decltype(auto)
141 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
143 template <typename... _Types, typename _Tp>
146 __variant_cast(_Tp&& __rhs)
148 if constexpr (is_lvalue_reference_v<_Tp>)
150 if constexpr (is_const_v<remove_reference_t<_Tp>>)
151 return static_cast<const variant<_Types...>&>(__rhs);
153 return static_cast<variant<_Types...>&>(__rhs);
156 return static_cast<variant<_Types...>&&>(__rhs);
163 // used for raw visitation
164 struct __variant_cookie {};
165 // used for raw visitation with indices passed in
166 struct __variant_idx_cookie { using type = __variant_idx_cookie; };
167 // Used to enable deduction (and same-type checking) for std::visit:
168 template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
170 // Visit variants that might be valueless.
171 template<typename _Visitor, typename... _Variants>
173 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
175 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
176 std::forward<_Variants>(__variants)...);
179 // Visit variants that might be valueless, passing indices to the visitor.
180 template<typename _Visitor, typename... _Variants>
182 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
184 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
185 std::forward<_Variants>(__variants)...);
188 // The __as function templates implement the exposition-only "as-variant"
190 template<typename... _Types>
191 constexpr std::variant<_Types...>&
192 __as(std::variant<_Types...>& __v) noexcept
195 template<typename... _Types>
196 constexpr const std::variant<_Types...>&
197 __as(const std::variant<_Types...>& __v) noexcept
200 template<typename... _Types>
201 constexpr std::variant<_Types...>&&
202 __as(std::variant<_Types...>&& __v) noexcept
203 { return std::move(__v); }
205 template<typename... _Types>
206 constexpr const std::variant<_Types...>&&
207 __as(const std::variant<_Types...>&& __v) noexcept
208 { return std::move(__v); }
211 // _Uninitialized<T> is guaranteed to be a trivially destructible type,
214 // _Uninitialized<T> is trivially destructible iff T is, so _Variant_union
215 // needs a constrained non-trivial destructor.
216 template<typename _Type, bool = std::is_trivially_destructible_v<_Type>>
217 struct _Uninitialized;
219 template<typename _Type>
220 struct _Uninitialized<_Type, true>
222 template<typename... _Args>
224 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
225 : _M_storage(std::forward<_Args>(__args)...)
228 constexpr const _Type& _M_get() const & noexcept
229 { return _M_storage; }
231 constexpr _Type& _M_get() & noexcept
232 { return _M_storage; }
234 constexpr const _Type&& _M_get() const && noexcept
235 { return std::move(_M_storage); }
237 constexpr _Type&& _M_get() && noexcept
238 { return std::move(_M_storage); }
243 template<typename _Type>
244 struct _Uninitialized<_Type, false>
246#if __cpp_lib_variant >= 202106L
247 template<typename... _Args>
249 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
250 : _M_storage(std::forward<_Args>(__args)...)
253 constexpr ~_Uninitialized() { }
255 _Uninitialized(const _Uninitialized&) = default;
256 _Uninitialized(_Uninitialized&&) = default;
257 _Uninitialized& operator=(const _Uninitialized&) = default;
258 _Uninitialized& operator=(_Uninitialized&&) = default;
260 constexpr const _Type& _M_get() const & noexcept
261 { return _M_storage; }
263 constexpr _Type& _M_get() & noexcept
264 { return _M_storage; }
266 constexpr const _Type&& _M_get() const && noexcept
267 { return std::move(_M_storage); }
269 constexpr _Type&& _M_get() && noexcept
270 { return std::move(_M_storage); }
272 struct _Empty_byte { };
275 _Empty_byte _M_empty;
279 template<typename... _Args>
281 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
283 ::new ((void*)std::addressof(_M_storage))
284 _Type(std::forward<_Args>(__args)...);
287 const _Type& _M_get() const & noexcept
288 { return *_M_storage._M_ptr(); }
290 _Type& _M_get() & noexcept
291 { return *_M_storage._M_ptr(); }
293 const _Type&& _M_get() const && noexcept
294 { return std::move(*_M_storage._M_ptr()); }
296 _Type&& _M_get() && noexcept
297 { return std::move(*_M_storage._M_ptr()); }
299 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
303 template<size_t _Np, typename _Union>
304 constexpr decltype(auto)
305 __get_n(_Union&& __u) noexcept
307 if constexpr (_Np == 0)
308 return std::forward<_Union>(__u)._M_first._M_get();
309 else if constexpr (_Np == 1)
310 return std::forward<_Union>(__u)._M_rest._M_first._M_get();
311 else if constexpr (_Np == 2)
312 return std::forward<_Union>(__u)._M_rest._M_rest._M_first._M_get();
314 return __variant::__get_n<_Np - 3>(
315 std::forward<_Union>(__u)._M_rest._M_rest._M_rest);
318 // Returns the typed storage for __v.
319 template<size_t _Np, typename _Variant>
320 constexpr decltype(auto)
321 __get(_Variant&& __v) noexcept
322 { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); }
324 // Gets the _Uninitialized to construct into for __u.
325 template<size_t _Np, typename _Union>
326 constexpr decltype(auto)
327 __construct_n(_Union& __u) noexcept
329 if constexpr (_Np == 0)
330 return &__u._M_first;
331 else if constexpr (_Np == 1)
333 std::_Construct(&__u._M_rest);
334 return &__u._M_rest._M_first;
336 else if constexpr (_Np == 2)
338 std::_Construct(&__u._M_rest);
339 std::_Construct(&__u._M_rest._M_rest);
340 return &__u._M_rest._M_rest._M_first;
344 std::_Construct(&__u._M_rest);
345 std::_Construct(&__u._M_rest._M_rest);
346 std::_Construct(&__u._M_rest._M_rest._M_rest);
347 return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest);
351 template<typename... _Types>
354 static constexpr bool _S_default_ctor =
355 is_default_constructible_v<typename _Nth_type<0, _Types...>::type>;
356 static constexpr bool _S_copy_ctor =
357 (is_copy_constructible_v<_Types> && ...);
358 static constexpr bool _S_move_ctor =
359 (is_move_constructible_v<_Types> && ...);
360 static constexpr bool _S_copy_assign =
362 && (is_copy_assignable_v<_Types> && ...);
363 static constexpr bool _S_move_assign =
365 && (is_move_assignable_v<_Types> && ...);
367 static constexpr bool _S_trivial_dtor =
368 (is_trivially_destructible_v<_Types> && ...);
369 static constexpr bool _S_trivial_copy_ctor =
370 (is_trivially_copy_constructible_v<_Types> && ...);
371 static constexpr bool _S_trivial_move_ctor =
372 (is_trivially_move_constructible_v<_Types> && ...);
373 static constexpr bool _S_trivial_copy_assign =
374 _S_trivial_dtor && _S_trivial_copy_ctor
375 && (is_trivially_copy_assignable_v<_Types> && ...);
376 static constexpr bool _S_trivial_move_assign =
377 _S_trivial_dtor && _S_trivial_move_ctor
378 && (is_trivially_move_assignable_v<_Types> && ...);
380 // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
381 // are always nothrow.
382 static constexpr bool _S_nothrow_default_ctor =
383 is_nothrow_default_constructible_v<
384 typename _Nth_type<0, _Types...>::type>;
385 static constexpr bool _S_nothrow_copy_ctor = false;
386 static constexpr bool _S_nothrow_move_ctor =
387 (is_nothrow_move_constructible_v<_Types> && ...);
388 static constexpr bool _S_nothrow_copy_assign = false;
389 static constexpr bool _S_nothrow_move_assign =
391 && (is_nothrow_move_assignable_v<_Types> && ...);
394 // Defines members and ctors.
395 template<typename... _Types>
396 union _Variadic_union
398 _Variadic_union() = default;
400 template<size_t _Np, typename... _Args>
401 _Variadic_union(in_place_index_t<_Np>, _Args&&...) = delete;
404 template<typename _First, typename... _Rest>
405 union _Variadic_union<_First, _Rest...>
407 constexpr _Variadic_union() : _M_rest() { }
409 template<typename... _Args>
411 _Variadic_union(in_place_index_t<0>, _Args&&... __args)
412 : _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
415 template<size_t _Np, typename... _Args>
417 _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
418 : _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
421#if __cpp_lib_variant >= 202106L
422 _Variadic_union(const _Variadic_union&) = default;
423 _Variadic_union(_Variadic_union&&) = default;
424 _Variadic_union& operator=(const _Variadic_union&) = default;
425 _Variadic_union& operator=(_Variadic_union&&) = default;
427 ~_Variadic_union() = default;
429 constexpr ~_Variadic_union()
430 requires (!__has_trivial_destructor(_First))
431 || (!__has_trivial_destructor(_Variadic_union<_Rest...>))
435 _Uninitialized<_First> _M_first;
436 _Variadic_union<_Rest...> _M_rest;
439 // _Never_valueless_alt is true for variant alternatives that can
440 // always be placed in a variant without it becoming valueless.
442 // For suitably-small, trivially copyable types we can create temporaries
443 // on the stack and then memcpy them into place.
444 template<typename _Tp>
445 struct _Never_valueless_alt
446 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
449 // Specialize _Never_valueless_alt for other types which have a
450 // non-throwing and cheap move construction and move assignment operator,
451 // so that emplacing the type will provide the strong exception-safety
452 // guarantee, by creating and moving a temporary.
453 // Whether _Never_valueless_alt<T> is true or not affects the ABI of a
454 // variant using that alternative, so we can't change the value later!
456 // True if every alternative in _Types... can be emplaced in a variant
457 // without it becoming valueless. If this is true, variant<_Types...>
458 // can never be valueless, which enables some minor optimizations.
459 template <typename... _Types>
460 constexpr bool __never_valueless()
462 return _Traits<_Types...>::_S_move_assign
463 && (_Never_valueless_alt<_Types>::value && ...);
466 // Defines index and the dtor, possibly trivial.
467 template<bool __trivially_destructible, typename... _Types>
468 struct _Variant_storage;
470 template <typename... _Types>
471 using __select_index =
472 typename __select_int::_Select_int_base<sizeof...(_Types),
474 unsigned short>::type::value_type;
476 template<typename... _Types>
477 struct _Variant_storage<false, _Types...>
481 : _M_index(static_cast<__index_type>(variant_npos))
484 template<size_t _Np, typename... _Args>
486 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
487 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
494 if (!_M_valid()) [[unlikely]]
497 std::__do_visit<void>([](auto&& __this_mem) mutable
499 std::_Destroy(std::__addressof(__this_mem));
500 }, __variant_cast<_Types...>(*this));
502 _M_index = static_cast<__index_type>(variant_npos);
510 _M_valid() const noexcept
512 if constexpr (__variant::__never_valueless<_Types...>())
514 return this->_M_index != __index_type(variant_npos);
517 _Variadic_union<_Types...> _M_u;
518 using __index_type = __select_index<_Types...>;
519 __index_type _M_index;
522 template<typename... _Types>
523 struct _Variant_storage<true, _Types...>
527 : _M_index(static_cast<__index_type>(variant_npos))
530 template<size_t _Np, typename... _Args>
532 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
533 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
539 { _M_index = static_cast<__index_type>(variant_npos); }
542 _M_valid() const noexcept
544 if constexpr (__variant::__never_valueless<_Types...>())
546 // It would be nice if we could just return true for -fno-exceptions.
547 // It's possible (but inadvisable) that a std::variant could become
548 // valueless in a translation unit compiled with -fexceptions and then
549 // be passed to functions compiled with -fno-exceptions. We would need
550 // some #ifdef _GLIBCXX_NO_EXCEPTIONS_GLOBALLY property to elide all
551 // checks for valueless_by_exception().
552 return this->_M_index != static_cast<__index_type>(variant_npos);
555 _Variadic_union<_Types...> _M_u;
556 using __index_type = __select_index<_Types...>;
557 __index_type _M_index;
560 // Implementation of v.emplace<N>(args...).
561 template<size_t _Np, bool _Triv, typename... _Types, typename... _Args>
564 __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args)
567 auto* __addr = __variant::__construct_n<_Np>(__v._M_u);
568 std::_Construct(__addr, in_place_index<0>,
569 std::forward<_Args>(__args)...);
570 // Construction didn't throw, so can set the new index now:
574 template<typename... _Types>
575 using _Variant_storage_alias =
576 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
578 // The following are (Copy|Move) (ctor|assign) layers for forwarding
579 // triviality and handling non-trivial SMF behaviors.
581 template<bool, typename... _Types>
582 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
584 using _Base = _Variant_storage_alias<_Types...>;
588 _Copy_ctor_base(const _Copy_ctor_base& __rhs)
589 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
591 __variant::__raw_idx_visit(
592 [this](auto&& __rhs_mem, auto __rhs_index) mutable
594 constexpr size_t __j = __rhs_index;
595 if constexpr (__j != variant_npos)
596 std::_Construct(std::__addressof(this->_M_u),
597 in_place_index<__j>, __rhs_mem);
598 }, __variant_cast<_Types...>(__rhs));
599 this->_M_index = __rhs._M_index;
602 _Copy_ctor_base(_Copy_ctor_base&&) = default;
603 _Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
604 _Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
607 template<typename... _Types>
608 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
610 using _Base = _Variant_storage_alias<_Types...>;
614 template<typename... _Types>
615 using _Copy_ctor_alias =
616 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
618 template<bool, typename... _Types>
619 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
621 using _Base = _Copy_ctor_alias<_Types...>;
625 _Move_ctor_base(_Move_ctor_base&& __rhs)
626 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
628 __variant::__raw_idx_visit(
629 [this](auto&& __rhs_mem, auto __rhs_index) mutable
631 constexpr size_t __j = __rhs_index;
632 if constexpr (__j != variant_npos)
633 std::_Construct(std::__addressof(this->_M_u),
635 std::forward<decltype(__rhs_mem)>(__rhs_mem));
636 }, __variant_cast<_Types...>(std::move(__rhs)));
637 this->_M_index = __rhs._M_index;
640 _Move_ctor_base(const _Move_ctor_base&) = default;
641 _Move_ctor_base& operator=(const _Move_ctor_base&) = default;
642 _Move_ctor_base& operator=(_Move_ctor_base&&) = default;
645 template<typename... _Types>
646 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
648 using _Base = _Copy_ctor_alias<_Types...>;
652 template<typename... _Types>
653 using _Move_ctor_alias =
654 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
656 template<bool, typename... _Types>
657 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
659 using _Base = _Move_ctor_alias<_Types...>;
664 operator=(const _Copy_assign_base& __rhs)
665 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
667 __variant::__raw_idx_visit(
668 [this](auto&& __rhs_mem, auto __rhs_index) mutable
670 constexpr size_t __j = __rhs_index;
671 if constexpr (__j == variant_npos)
672 this->_M_reset(); // Make *this valueless.
673 else if (this->_M_index == __j)
674 __variant::__get<__j>(*this) = __rhs_mem;
677 using _Tj = typename _Nth_type<__j, _Types...>::type;
678 if constexpr (is_nothrow_copy_constructible_v<_Tj>
679 || !is_nothrow_move_constructible_v<_Tj>)
680 __variant::__emplace<__j>(*this, __rhs_mem);
683 using _Variant = variant<_Types...>;
684 _Variant& __self = __variant_cast<_Types...>(*this);
685 __self = _Variant(in_place_index<__j>, __rhs_mem);
688 }, __variant_cast<_Types...>(__rhs));
692 _Copy_assign_base(const _Copy_assign_base&) = default;
693 _Copy_assign_base(_Copy_assign_base&&) = default;
694 _Copy_assign_base& operator=(_Copy_assign_base&&) = default;
697 template<typename... _Types>
698 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
700 using _Base = _Move_ctor_alias<_Types...>;
704 template<typename... _Types>
705 using _Copy_assign_alias =
706 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
708 template<bool, typename... _Types>
709 struct _Move_assign_base : _Copy_assign_alias<_Types...>
711 using _Base = _Copy_assign_alias<_Types...>;
716 operator=(_Move_assign_base&& __rhs)
717 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
719 __variant::__raw_idx_visit(
720 [this](auto&& __rhs_mem, auto __rhs_index) mutable
722 constexpr size_t __j = __rhs_index;
723 if constexpr (__j != variant_npos)
725 if (this->_M_index == __j)
726 __variant::__get<__j>(*this) = std::move(__rhs_mem);
729 using _Tj = typename _Nth_type<__j, _Types...>::type;
730 if constexpr (is_nothrow_move_constructible_v<_Tj>)
731 __variant::__emplace<__j>(*this, std::move(__rhs_mem));
734 using _Variant = variant<_Types...>;
735 _Variant& __self = __variant_cast<_Types...>(*this);
736 __self.template emplace<__j>(std::move(__rhs_mem));
742 }, __variant_cast<_Types...>(__rhs));
746 _Move_assign_base(const _Move_assign_base&) = default;
747 _Move_assign_base(_Move_assign_base&&) = default;
748 _Move_assign_base& operator=(const _Move_assign_base&) = default;
751 template<typename... _Types>
752 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
754 using _Base = _Copy_assign_alias<_Types...>;
758 template<typename... _Types>
759 using _Move_assign_alias =
760 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
762 template<typename... _Types>
763 struct _Variant_base : _Move_assign_alias<_Types...>
765 using _Base = _Move_assign_alias<_Types...>;
768 _Variant_base() noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
769 : _Variant_base(in_place_index<0>) { }
771 template<size_t _Np, typename... _Args>
773 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
774 : _Base(__i, std::forward<_Args>(__args)...)
777 _Variant_base(const _Variant_base&) = default;
778 _Variant_base(_Variant_base&&) = default;
779 _Variant_base& operator=(const _Variant_base&) = default;
780 _Variant_base& operator=(_Variant_base&&) = default;
783 template<typename _Tp, typename... _Types>
784 inline constexpr bool __exactly_once
785 = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types);
787 // Helper used to check for valid conversions that don't involve narrowing.
788 template<typename _Ti> struct _Arr { _Ti _M_x[1]; };
790 // "Build an imaginary function FUN(Ti) for each alternative type Ti"
791 template<size_t _Ind, typename _Tp, typename _Ti, typename = void>
794 // This function means 'using _Build_FUN<I, T, Ti>::_S_fun;' is valid,
795 // but only static functions will be considered in the call below.
799 // "... for which Ti x[] = {std::forward<T>(t)}; is well-formed."
800 template<size_t _Ind, typename _Tp, typename _Ti>
801 struct _Build_FUN<_Ind, _Tp, _Ti,
802 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
804 // This is the FUN function for type _Ti, with index _Ind
805 static integral_constant<size_t, _Ind> _S_fun(_Ti);
808 template<typename _Tp, typename _Variant,
809 typename = make_index_sequence<variant_size_v<_Variant>>>
812 template<typename _Tp, typename... _Ti, size_t... _Ind>
813 struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
814 : _Build_FUN<_Ind, _Tp, _Ti>...
816 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
819 // The index j of the overload FUN(Tj) selected by overload resolution
820 // for FUN(std::forward<_Tp>(t))
821 template<typename _Tp, typename _Variant>
823 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
825 // The index selected for FUN(std::forward<T>(t)), or variant_npos if none.
826 template<typename _Tp, typename _Variant, typename = void>
827 struct __accepted_index
828 : integral_constant<size_t, variant_npos>
831 template<typename _Tp, typename _Variant>
832 struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
833 : _FUN_type<_Tp, _Variant>
836 template <typename _Maybe_variant_cookie, typename _Variant>
837 struct _Extra_visit_slot_needed
839 template <typename> struct _Variant_never_valueless;
841 template <typename... _Types>
842 struct _Variant_never_valueless<variant<_Types...>>
843 : bool_constant<__variant::__never_valueless<_Types...>()> {};
845 static constexpr bool value =
846 (is_same_v<_Maybe_variant_cookie, __variant_cookie>
847 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
848 && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
851 // Used for storing a multi-dimensional vtable.
852 template<typename _Tp, size_t... _Dimensions>
855 // Partial specialization with rank zero, stores a single _Tp element.
856 template<typename _Tp>
857 struct _Multi_array<_Tp>
860 struct __untag_result
862 { using element_type = _Tp; };
864 template <typename... _Args>
865 struct __untag_result<const void(*)(_Args...)>
867 { using element_type = void(*)(_Args...); };
869 template <typename... _Args>
870 struct __untag_result<__variant_cookie(*)(_Args...)>
872 { using element_type = void(*)(_Args...); };
874 template <typename... _Args>
875 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
877 { using element_type = void(*)(_Args...); };
879 template <typename _Res, typename... _Args>
880 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
882 { using element_type = _Res(*)(_Args...); };
884 using __result_is_deduced = __untag_result<_Tp>;
886 constexpr const typename __untag_result<_Tp>::element_type&
890 typename __untag_result<_Tp>::element_type _M_data;
893 // Partial specialization with rank >= 1.
894 template<typename _Ret,
896 typename... _Variants,
897 size_t __first, size_t... __rest>
898 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
900 static constexpr size_t __index =
901 sizeof...(_Variants) - sizeof...(__rest) - 1;
903 using _Variant = typename _Nth_type<__index, _Variants...>::type;
905 static constexpr int __do_cookie =
906 _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
908 using _Tp = _Ret(*)(_Visitor, _Variants...);
910 template<typename... _Args>
911 constexpr decltype(auto)
912 _M_access(size_t __first_index, _Args... __rest_indices) const
914 return _M_arr[__first_index + __do_cookie]
915 ._M_access(__rest_indices...);
918 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
921 // Creates a multi-dimensional vtable recursively.
924 // visit([](auto, auto){},
925 // variant<int, char>(), // typedef'ed as V1
926 // variant<float, double, long double>()) // typedef'ed as V2
927 // will trigger instantiations of:
928 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 2, 3>,
929 // tuple<V1&&, V2&&>, std::index_sequence<>>
930 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
931 // tuple<V1&&, V2&&>, std::index_sequence<0>>
932 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
933 // tuple<V1&&, V2&&>, std::index_sequence<0, 0>>
934 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
935 // tuple<V1&&, V2&&>, std::index_sequence<0, 1>>
936 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
937 // tuple<V1&&, V2&&>, std::index_sequence<0, 2>>
938 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&), 3>,
939 // tuple<V1&&, V2&&>, std::index_sequence<1>>
940 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
941 // tuple<V1&&, V2&&>, std::index_sequence<1, 0>>
942 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
943 // tuple<V1&&, V2&&>, std::index_sequence<1, 1>>
944 // __gen_vtable_impl<_Multi_array<void(*)(V1&&, V2&&)>,
945 // tuple<V1&&, V2&&>, std::index_sequence<1, 2>>
946 // The returned multi-dimensional vtable can be fast accessed by the visitor
947 // using index calculation.
948 template<typename _Array_type, typename _Index_seq>
949 struct __gen_vtable_impl;
951 // Defines the _S_apply() member that returns a _Multi_array populated
952 // with function pointers that perform the visitation expressions e(m)
953 // for each valid pack of indexes into the variant types _Variants.
955 // This partial specialization builds up the index sequences by recursively
956 // calling _S_apply() on the next specialization of __gen_vtable_impl.
957 // The base case of the recursion defines the actual function pointers.
958 template<typename _Result_type, typename _Visitor, size_t... __dimensions,
959 typename... _Variants, size_t... __indices>
960 struct __gen_vtable_impl<
961 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
962 std::index_sequence<__indices...>>
965 remove_reference_t<typename _Nth_type<sizeof...(__indices),
966 _Variants...>::type>;
968 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
971 static constexpr _Array_type
974 _Array_type __vtable{};
976 __vtable, make_index_sequence<variant_size_v<_Next>>());
980 template<size_t... __var_indices>
981 static constexpr void
982 _S_apply_all_alts(_Array_type& __vtable,
983 std::index_sequence<__var_indices...>)
985 if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
986 (_S_apply_single_alt<true, __var_indices>(
987 __vtable._M_arr[__var_indices + 1],
988 &(__vtable._M_arr[0])), ...);
990 (_S_apply_single_alt<false, __var_indices>(
991 __vtable._M_arr[__var_indices]), ...);
994 template<bool __do_cookie, size_t __index, typename _Tp>
995 static constexpr void
996 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
998 if constexpr (__do_cookie)
1000 __element = __gen_vtable_impl<
1002 std::index_sequence<__indices..., __index>>::_S_apply();
1003 *__cookie_element = __gen_vtable_impl<
1005 std::index_sequence<__indices..., variant_npos>>::_S_apply();
1009 auto __tmp_element = __gen_vtable_impl<
1010 remove_reference_t<decltype(__element)>,
1011 std::index_sequence<__indices..., __index>>::_S_apply();
1012 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
1013 "std::visit requires the visitor to have the same "
1014 "return type for all alternatives of a variant");
1015 __element = __tmp_element;
1020 // This partial specialization is the base case for the recursion.
1021 // It populates a _Multi_array element with the address of a function
1022 // that invokes the visitor with the alternatives specified by __indices.
1023 template<typename _Result_type, typename _Visitor, typename... _Variants,
1024 size_t... __indices>
1025 struct __gen_vtable_impl<
1026 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
1027 std::index_sequence<__indices...>>
1030 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
1032 template<size_t __index, typename _Variant>
1033 static constexpr decltype(auto)
1034 __element_by_index_or_cookie(_Variant&& __var) noexcept
1036 if constexpr (__index != variant_npos)
1037 return __variant::__get<__index>(std::forward<_Variant>(__var));
1039 return __variant_cookie{};
1042 static constexpr decltype(auto)
1043 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
1045 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1046 // For raw visitation using indices, pass the indices to the visitor
1047 // and discard the return value:
1048 std::__invoke(std::forward<_Visitor>(__visitor),
1049 __element_by_index_or_cookie<__indices>(
1050 std::forward<_Variants>(__vars))...,
1051 integral_constant<size_t, __indices>()...);
1052 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1053 // For raw visitation without indices, and discard the return value:
1054 std::__invoke(std::forward<_Visitor>(__visitor),
1055 __element_by_index_or_cookie<__indices>(
1056 std::forward<_Variants>(__vars))...);
1057 else if constexpr (_Array_type::__result_is_deduced::value)
1058 // For the usual std::visit case deduce the return value:
1059 return std::__invoke(std::forward<_Visitor>(__visitor),
1060 __element_by_index_or_cookie<__indices>(
1061 std::forward<_Variants>(__vars))...);
1062 else // for std::visit<R> use INVOKE<R>
1063 return std::__invoke_r<_Result_type>(
1064 std::forward<_Visitor>(__visitor),
1065 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1068 static constexpr auto
1071 if constexpr (_Array_type::__result_is_deduced::value)
1073 constexpr bool __visit_ret_type_mismatch =
1074 !is_same_v<typename _Result_type::type,
1075 decltype(__visit_invoke(std::declval<_Visitor>(),
1076 std::declval<_Variants>()...))>;
1077 if constexpr (__visit_ret_type_mismatch)
1079 struct __cannot_match {};
1080 return __cannot_match{};
1083 return _Array_type{&__visit_invoke};
1086 return _Array_type{&__visit_invoke};
1090 template<typename _Result_type, typename _Visitor, typename... _Variants>
1094 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1095 variant_size_v<remove_reference_t<_Variants>>...>;
1097 static constexpr _Array_type _S_vtable
1098 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1101 template<size_t _Np, typename _Tp>
1102 struct _Base_dedup : public _Tp { };
1104 template<typename _Variant, typename __indices>
1105 struct _Variant_hash_base;
1107 template<typename... _Types, size_t... __indices>
1108 struct _Variant_hash_base<variant<_Types...>,
1109 std::index_sequence<__indices...>>
1110 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1112 // Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
1113 template<size_t _Np, typename _Variant,
1114 typename _AsV = decltype(__variant::__as(std::declval<_Variant>())),
1115 typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
1117 = __conditional_t<is_lvalue_reference_v<_Variant>, _Tp&, _Tp&&>;
1119 // Return type of std::visit.
1120 template<typename _Visitor, typename... _Variants>
1121 using __visit_result_t
1122 = invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
1124 template<typename _Tp, typename... _Types>
1125 constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1127 template <typename _Visitor, typename _Variant, size_t... _Idxs>
1128 constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
1130 return __same_types<
1131 invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
1135} // namespace __variant
1136} // namespace __detail
1138 template<typename _Tp, typename... _Types>
1140 holds_alternative(const variant<_Types...>& __v) noexcept
1142 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1143 "T must occur exactly once in alternatives");
1144 return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>();
1147 template<typename _Tp, typename... _Types>
1149 get(variant<_Types...>& __v)
1151 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1152 "T must occur exactly once in alternatives");
1153 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1154 return std::get<__n>(__v);
1157 template<typename _Tp, typename... _Types>
1159 get(variant<_Types...>&& __v)
1161 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1162 "T must occur exactly once in alternatives");
1163 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1164 return std::get<__n>(std::move(__v));
1167 template<typename _Tp, typename... _Types>
1168 constexpr const _Tp&
1169 get(const variant<_Types...>& __v)
1171 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1172 "T must occur exactly once in alternatives");
1173 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1174 return std::get<__n>(__v);
1177 template<typename _Tp, typename... _Types>
1178 constexpr const _Tp&&
1179 get(const variant<_Types...>&& __v)
1181 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1182 "T must occur exactly once in alternatives");
1183 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1184 return std::get<__n>(std::move(__v));
1187 template<size_t _Np, typename... _Types>
1188 constexpr add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1189 get_if(variant<_Types...>* __ptr) noexcept
1191 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1192 static_assert(_Np < sizeof...(_Types),
1193 "The index must be in [0, number of alternatives)");
1194 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1195 if (__ptr && __ptr->index() == _Np)
1196 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1200 template<size_t _Np, typename... _Types>
1202 add_pointer_t<const variant_alternative_t<_Np, variant<_Types...>>>
1203 get_if(const variant<_Types...>* __ptr) noexcept
1205 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1206 static_assert(_Np < sizeof...(_Types),
1207 "The index must be in [0, number of alternatives)");
1208 static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
1209 if (__ptr && __ptr->index() == _Np)
1210 return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
1214 template<typename _Tp, typename... _Types>
1215 constexpr add_pointer_t<_Tp>
1216 get_if(variant<_Types...>* __ptr) noexcept
1218 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1219 "T must occur exactly once in alternatives");
1220 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1221 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1222 return std::get_if<__n>(__ptr);
1225 template<typename _Tp, typename... _Types>
1226 constexpr add_pointer_t<const _Tp>
1227 get_if(const variant<_Types...>* __ptr) noexcept
1229 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1230 "T must occur exactly once in alternatives");
1231 static_assert(!is_void_v<_Tp>, "_Tp must not be void");
1232 constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1233 return std::get_if<__n>(__ptr);
1236 struct monostate { };
1238#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1239 template<typename... _Types> \
1240 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1241 const variant<_Types...>& __rhs) \
1243 bool __ret = true; \
1244 __detail::__variant::__raw_idx_visit( \
1245 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1247 if constexpr (__rhs_index != variant_npos) \
1249 if (__lhs.index() == __rhs_index) \
1251 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1252 __ret = __this_mem __OP __rhs_mem; \
1255 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1258 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1263 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1264 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1265 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1266 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1267 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1268 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1270#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1272 constexpr bool operator==(monostate, monostate) noexcept { return true; }
1274#ifdef __cpp_lib_three_way_comparison
1275 template<typename... _Types>
1276 requires (three_way_comparable<_Types> && ...)
1278 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1279 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1281 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1282 = strong_ordering::equal;
1284 __detail::__variant::__raw_idx_visit(
1285 [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
1287 if constexpr (__w_index != variant_npos)
1289 if (__v.index() == __w_index)
1291 auto& __this_mem = std::get<__w_index>(__v);
1292 __ret = __this_mem <=> __w_mem;
1296 __ret = (__v.index() + 1) <=> (__w_index + 1);
1301 constexpr strong_ordering
1302 operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
1304 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
1305 constexpr bool operator<(monostate, monostate) noexcept { return false; }
1306 constexpr bool operator>(monostate, monostate) noexcept { return false; }
1307 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
1308 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
1311 template<typename _Visitor, typename... _Variants>
1312 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1313 visit(_Visitor&&, _Variants&&...);
1315 template<typename... _Types>
1316 _GLIBCXX20_CONSTEXPR
1317 inline enable_if_t<(is_move_constructible_v<_Types> && ...)
1318 && (is_swappable_v<_Types> && ...)>
1319 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1320 noexcept(noexcept(__lhs.swap(__rhs)))
1321 { __lhs.swap(__rhs); }
1323 template<typename... _Types>
1324 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1325 && (is_swappable_v<_Types> && ...))>
1326 swap(variant<_Types...>&, variant<_Types...>&) = delete;
1328 class bad_variant_access : public exception
1331 bad_variant_access() noexcept { }
1333 const char* what() const noexcept override
1334 { return _M_reason; }
1337 bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
1339 // Must point to a string with static storage duration:
1340 const char* _M_reason = "bad variant access";
1342 friend void __throw_bad_variant_access(const char* __what);
1345 // Must only be called with a string literal
1347 __throw_bad_variant_access(const char* __what)
1348 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1351 __throw_bad_variant_access(bool __valueless)
1353 if (__valueless) [[__unlikely__]]
1354 __throw_bad_variant_access("std::get: variant is valueless");
1356 __throw_bad_variant_access("std::get: wrong index for variant");
1359 template<typename... _Types>
1361 : private __detail::__variant::_Variant_base<_Types...>,
1362 private _Enable_default_constructor<
1363 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1364 variant<_Types...>>,
1365 private _Enable_copy_move<
1366 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1367 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1368 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1369 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1373 template <typename... _UTypes, typename _Tp>
1374 friend _GLIBCXX20_CONSTEXPR decltype(auto)
1375 __variant_cast(_Tp&&);
1377 static_assert(sizeof...(_Types) > 0,
1378 "variant must have at least one alternative");
1379 static_assert(!(std::is_reference_v<_Types> || ...),
1380 "variant must have no reference alternative");
1381 static_assert(!(std::is_void_v<_Types> || ...),
1382 "variant must have no void alternative");
1384 using _Base = __detail::__variant::_Variant_base<_Types...>;
1385 using _Default_ctor_enabler =
1386 _Enable_default_constructor<
1387 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1388 variant<_Types...>>;
1390 template<typename _Tp>
1391 static constexpr bool __not_self
1392 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1394 template<typename _Tp>
1395 static constexpr bool
1396 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1398 template<typename _Tp>
1399 static constexpr size_t __accepted_index
1400 = __detail::__variant::__accepted_index<_Tp, variant>::value;
1402 template<size_t _Np, typename = enable_if_t<(_Np < sizeof...(_Types))>>
1403 using __to_type = typename _Nth_type<_Np, _Types...>::type;
1405 template<typename _Tp, typename = enable_if_t<__not_self<_Tp>>>
1406 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1408 template<typename _Tp>
1409 static constexpr size_t __index_of
1410 = std::__find_uniq_type_in_pack<_Tp, _Types...>();
1412 using _Traits = __detail::__variant::_Traits<_Types...>;
1414 template<typename _Tp>
1415 struct __is_in_place_tag : false_type { };
1416 template<typename _Tp>
1417 struct __is_in_place_tag<in_place_type_t<_Tp>> : true_type { };
1418 template<size_t _Np>
1419 struct __is_in_place_tag<in_place_index_t<_Np>> : true_type { };
1421 template<typename _Tp>
1422 static constexpr bool __not_in_place_tag
1423 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1426 variant() = default;
1427 variant(const variant& __rhs) = default;
1428 variant(variant&&) = default;
1429 variant& operator=(const variant&) = default;
1430 variant& operator=(variant&&) = default;
1431 _GLIBCXX20_CONSTEXPR ~variant() = default;
1433 template<typename _Tp,
1434 typename = enable_if_t<sizeof...(_Types) != 0>,
1435 typename = enable_if_t<__not_in_place_tag<_Tp>>,
1436 typename _Tj = __accepted_type<_Tp&&>,
1437 typename = enable_if_t<__exactly_once<_Tj>
1438 && is_constructible_v<_Tj, _Tp>>>
1441 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1442 : variant(in_place_index<__accepted_index<_Tp>>,
1443 std::forward<_Tp>(__t))
1446 template<typename _Tp, typename... _Args,
1447 typename = enable_if_t<__exactly_once<_Tp>
1448 && is_constructible_v<_Tp, _Args...>>>
1450 variant(in_place_type_t<_Tp>, _Args&&... __args)
1451 : variant(in_place_index<__index_of<_Tp>>,
1452 std::forward<_Args>(__args)...)
1455 template<typename _Tp, typename _Up, typename... _Args,
1456 typename = enable_if_t<__exactly_once<_Tp>
1457 && is_constructible_v<_Tp,
1458 initializer_list<_Up>&, _Args...>>>
1460 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1462 : variant(in_place_index<__index_of<_Tp>>, __il,
1463 std::forward<_Args>(__args)...)
1466 template<size_t _Np, typename... _Args,
1467 typename _Tp = __to_type<_Np>,
1468 typename = enable_if_t<is_constructible_v<_Tp, _Args...>>>
1470 variant(in_place_index_t<_Np>, _Args&&... __args)
1471 : _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
1472 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1475 template<size_t _Np, typename _Up, typename... _Args,
1476 typename _Tp = __to_type<_Np>,
1477 typename = enable_if_t<is_constructible_v<_Tp,
1478 initializer_list<_Up>&,
1481 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1483 : _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
1484 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1487 template<typename _Tp>
1488 _GLIBCXX20_CONSTEXPR
1489 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1490 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1491 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1493 operator=(_Tp&& __rhs)
1494 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1495 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1497 constexpr auto __index = __accepted_index<_Tp>;
1498 if (index() == __index)
1499 std::get<__index>(*this) = std::forward<_Tp>(__rhs);
1502 using _Tj = __accepted_type<_Tp&&>;
1503 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1504 || !is_nothrow_move_constructible_v<_Tj>)
1505 this->emplace<__index>(std::forward<_Tp>(__rhs));
1507 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1508 // 3585. converting assignment with immovable alternative
1509 this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
1514 template<typename _Tp, typename... _Args>
1515 _GLIBCXX20_CONSTEXPR
1516 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1518 emplace(_Args&&... __args)
1520 constexpr size_t __index = __index_of<_Tp>;
1521 return this->emplace<__index>(std::forward<_Args>(__args)...);
1524 template<typename _Tp, typename _Up, typename... _Args>
1525 _GLIBCXX20_CONSTEXPR
1526 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1527 && __exactly_once<_Tp>,
1529 emplace(initializer_list<_Up> __il, _Args&&... __args)
1531 constexpr size_t __index = __index_of<_Tp>;
1532 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1535 template<size_t _Np, typename... _Args>
1536 _GLIBCXX20_CONSTEXPR
1537 enable_if_t<is_constructible_v<__to_type<_Np>, _Args...>,
1539 emplace(_Args&&... __args)
1541 namespace __variant = std::__detail::__variant;
1542 using type = typename _Nth_type<_Np, _Types...>::type;
1543 // Provide the strong exception-safety guarantee when possible,
1544 // to avoid becoming valueless.
1545 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1547 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1549 else if constexpr (is_scalar_v<type>)
1551 // This might invoke a potentially-throwing conversion operator:
1552 const type __tmp(std::forward<_Args>(__args)...);
1553 // But this won't throw:
1554 __variant::__emplace<_Np>(*this, __tmp);
1556 else if constexpr (__variant::_Never_valueless_alt<type>()
1557 && _Traits::_S_move_assign)
1559 // This construction might throw:
1560 variant __tmp(in_place_index<_Np>,
1561 std::forward<_Args>(__args)...);
1562 // But _Never_valueless_alt<type> means this won't:
1563 *this = std::move(__tmp);
1567 // This case only provides the basic exception-safety guarantee,
1568 // i.e. the variant can become valueless.
1569 __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
1571 return std::get<_Np>(*this);
1574 template<size_t _Np, typename _Up, typename... _Args>
1575 _GLIBCXX20_CONSTEXPR
1576 enable_if_t<is_constructible_v<__to_type<_Np>,
1577 initializer_list<_Up>&, _Args...>,
1579 emplace(initializer_list<_Up> __il, _Args&&... __args)
1581 namespace __variant = std::__detail::__variant;
1582 using type = typename _Nth_type<_Np, _Types...>::type;
1583 // Provide the strong exception-safety guarantee when possible,
1584 // to avoid becoming valueless.
1585 if constexpr (is_nothrow_constructible_v<type,
1586 initializer_list<_Up>&,
1589 __variant::__emplace<_Np>(*this, __il,
1590 std::forward<_Args>(__args)...);
1592 else if constexpr (__variant::_Never_valueless_alt<type>()
1593 && _Traits::_S_move_assign)
1595 // This construction might throw:
1596 variant __tmp(in_place_index<_Np>, __il,
1597 std::forward<_Args>(__args)...);
1598 // But _Never_valueless_alt<type> means this won't:
1599 *this = std::move(__tmp);
1603 // This case only provides the basic exception-safety guarantee,
1604 // i.e. the variant can become valueless.
1605 __variant::__emplace<_Np>(*this, __il,
1606 std::forward<_Args>(__args)...);
1608 return std::get<_Np>(*this);
1611 template<size_t _Np, typename... _Args>
1612 enable_if_t<!(_Np < sizeof...(_Types))> emplace(_Args&&...) = delete;
1614 template<typename _Tp, typename... _Args>
1615 enable_if_t<!__exactly_once<_Tp>> emplace(_Args&&...) = delete;
1617 constexpr bool valueless_by_exception() const noexcept
1618 { return !this->_M_valid(); }
1620 constexpr size_t index() const noexcept
1622 using __index_type = typename _Base::__index_type;
1623 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1624 return this->_M_index;
1625 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1626 return make_signed_t<__index_type>(this->_M_index);
1628 return size_t(__index_type(this->_M_index + 1)) - 1;
1631 _GLIBCXX20_CONSTEXPR
1633 swap(variant& __rhs)
1634 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1635 && is_nothrow_move_constructible_v<variant>)
1637 static_assert((is_move_constructible_v<_Types> && ...));
1639 // Handle this here to simplify the visitation.
1640 if (__rhs.valueless_by_exception()) [[__unlikely__]]
1642 if (!this->valueless_by_exception()) [[__likely__]]
1647 namespace __variant = __detail::__variant;
1649 __variant::__raw_idx_visit(
1650 [this, &__rhs](auto&& __rhs_mem, auto __rhs_index) mutable
1652 constexpr size_t __j = __rhs_index;
1653 if constexpr (__j != variant_npos)
1655 if (this->index() == __j)
1658 swap(std::get<__j>(*this), __rhs_mem);
1662 auto __tmp(std::move(__rhs_mem));
1664 if constexpr (_Traits::_S_trivial_move_assign)
1665 __rhs = std::move(*this);
1667 __variant::__raw_idx_visit(
1668 [&__rhs](auto&& __this_mem, auto __this_index) mutable
1670 constexpr size_t __k = __this_index;
1671 if constexpr (__k != variant_npos)
1672 __variant::__emplace<__k>(__rhs,
1673 std::move(__this_mem));
1676 __variant::__emplace<__j>(*this, std::move(__tmp));
1682#if defined(__clang__) && __clang_major__ <= 7
1684 using _Base::_M_u; // See https://bugs.llvm.org/show_bug.cgi?id=31852
1688 template<size_t _Np, typename _Vp>
1689 friend constexpr decltype(auto)
1690 __detail::__variant::__get(_Vp&& __v) noexcept;
1692#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1693 template<typename... _Tp> \
1694 friend constexpr bool \
1695 operator __OP(const variant<_Tp...>& __lhs, \
1696 const variant<_Tp...>& __rhs);
1698 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1699 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1700 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1701 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1702 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1703 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1705#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1708 template<size_t _Np, typename... _Types>
1709 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1710 get(variant<_Types...>& __v)
1712 static_assert(_Np < sizeof...(_Types),
1713 "The index must be in [0, number of alternatives)");
1714 if (__v.index() != _Np)
1715 __throw_bad_variant_access(__v.valueless_by_exception());
1716 return __detail::__variant::__get<_Np>(__v);
1719 template<size_t _Np, typename... _Types>
1720 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1721 get(variant<_Types...>&& __v)
1723 static_assert(_Np < sizeof...(_Types),
1724 "The index must be in [0, number of alternatives)");
1725 if (__v.index() != _Np)
1726 __throw_bad_variant_access(__v.valueless_by_exception());
1727 return __detail::__variant::__get<_Np>(std::move(__v));
1730 template<size_t _Np, typename... _Types>
1731 constexpr const variant_alternative_t<_Np, variant<_Types...>>&
1732 get(const variant<_Types...>& __v)
1734 static_assert(_Np < sizeof...(_Types),
1735 "The index must be in [0, number of alternatives)");
1736 if (__v.index() != _Np)
1737 __throw_bad_variant_access(__v.valueless_by_exception());
1738 return __detail::__variant::__get<_Np>(__v);
1741 template<size_t _Np, typename... _Types>
1742 constexpr const variant_alternative_t<_Np, variant<_Types...>>&&
1743 get(const variant<_Types...>&& __v)
1745 static_assert(_Np < sizeof...(_Types),
1746 "The index must be in [0, number of alternatives)");
1747 if (__v.index() != _Np)
1748 __throw_bad_variant_access(__v.valueless_by_exception());
1749 return __detail::__variant::__get<_Np>(std::move(__v));
1752 /// @cond undocumented
1753 template<typename _Result_type, typename _Visitor, typename... _Variants>
1754 constexpr decltype(auto)
1755 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1757 // Get the silly case of visiting no variants out of the way first.
1758 if constexpr (sizeof...(_Variants) == 0)
1760 if constexpr (is_void_v<_Result_type>)
1761 return (void) std::forward<_Visitor>(__visitor)();
1763 return std::forward<_Visitor>(__visitor)();
1767 constexpr size_t __max = 11; // "These go to eleven."
1769 // The type of the first variant in the pack.
1770 using _V0 = typename _Nth_type<0, _Variants...>::type;
1771 // The number of alternatives in that first variant.
1772 constexpr auto __n = variant_size_v<remove_reference_t<_V0>>;
1774 if constexpr (sizeof...(_Variants) > 1 || __n > __max)
1776 // Use a jump table for the general case.
1777 constexpr auto& __vtable = __detail::__variant::__gen_vtable<
1778 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1780 auto __func_ptr = __vtable._M_access(__variants.index()...);
1781 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1782 std::forward<_Variants>(__variants)...);
1784 else // We have a single variant with a small number of alternatives.
1786 // A name for the first variant in the pack.
1788 = [](_V0& __v, ...) -> _V0& { return __v; }(__variants...);
1790 using __detail::__variant::_Multi_array;
1791 using __detail::__variant::__gen_vtable_impl;
1792 using _Ma = _Multi_array<_Result_type (*)(_Visitor&&, _V0&&)>;
1794#ifdef _GLIBCXX_DEBUG
1795# define _GLIBCXX_VISIT_UNREACHABLE __builtin_trap
1797# define _GLIBCXX_VISIT_UNREACHABLE __builtin_unreachable
1800#define _GLIBCXX_VISIT_CASE(N) \
1803 if constexpr (N < __n) \
1805 return __gen_vtable_impl<_Ma, index_sequence<N>>:: \
1806 __visit_invoke(std::forward<_Visitor>(__visitor), \
1807 std::forward<_V0>(__v0)); \
1809 else _GLIBCXX_VISIT_UNREACHABLE(); \
1812 switch (__v0.index())
1814 _GLIBCXX_VISIT_CASE(0)
1815 _GLIBCXX_VISIT_CASE(1)
1816 _GLIBCXX_VISIT_CASE(2)
1817 _GLIBCXX_VISIT_CASE(3)
1818 _GLIBCXX_VISIT_CASE(4)
1819 _GLIBCXX_VISIT_CASE(5)
1820 _GLIBCXX_VISIT_CASE(6)
1821 _GLIBCXX_VISIT_CASE(7)
1822 _GLIBCXX_VISIT_CASE(8)
1823 _GLIBCXX_VISIT_CASE(9)
1824 _GLIBCXX_VISIT_CASE(10)
1826 using __detail::__variant::__variant_idx_cookie;
1827 using __detail::__variant::__variant_cookie;
1828 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>
1829 || is_same_v<_Result_type, __variant_cookie>)
1831 using _Npos = index_sequence<variant_npos>;
1832 return __gen_vtable_impl<_Ma, _Npos>::
1833 __visit_invoke(std::forward<_Visitor>(__visitor),
1834 std::forward<_V0>(__v0));
1837 _GLIBCXX_VISIT_UNREACHABLE();
1839 _GLIBCXX_VISIT_UNREACHABLE();
1841#undef _GLIBCXX_VISIT_CASE
1842#undef _GLIBCXX_VISIT_UNREACHABLE
1848 template<typename _Visitor, typename... _Variants>
1849 constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
1850 visit(_Visitor&& __visitor, _Variants&&... __variants)
1852 namespace __variant = std::__detail::__variant;
1854 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1855 __throw_bad_variant_access("std::visit: variant is valueless");
1858 = __detail::__variant::__visit_result_t<_Visitor, _Variants...>;
1860 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1862 if constexpr (sizeof...(_Variants) == 1)
1864 using _Vp = decltype(__variant::__as(std::declval<_Variants>()...));
1866 constexpr bool __visit_rettypes_match = __detail::__variant::
1867 __check_visitor_results<_Visitor, _Vp>(
1868 make_index_sequence<variant_size_v<remove_reference_t<_Vp>>>());
1869 if constexpr (!__visit_rettypes_match)
1871 static_assert(__visit_rettypes_match,
1872 "std::visit requires the visitor to have the same "
1873 "return type for all alternatives of a variant");
1877 return std::__do_visit<_Tag>(
1878 std::forward<_Visitor>(__visitor),
1879 static_cast<_Vp>(__variants)...);
1882 return std::__do_visit<_Tag>(
1883 std::forward<_Visitor>(__visitor),
1884 __variant::__as(std::forward<_Variants>(__variants))...);
1887#if __cplusplus > 201703L
1888 template<typename _Res, typename _Visitor, typename... _Variants>
1890 visit(_Visitor&& __visitor, _Variants&&... __variants)
1892 namespace __variant = std::__detail::__variant;
1894 if ((__variant::__as(__variants).valueless_by_exception() || ...))
1895 __throw_bad_variant_access("std::visit<R>: variant is valueless");
1897 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1898 __variant::__as(std::forward<_Variants>(__variants))...);
1902 /// @cond undocumented
1903 template<bool, typename... _Types>
1904 struct __variant_hash_call_base_impl
1907 operator()(const variant<_Types...>& __t) const
1908 noexcept((is_nothrow_invocable_v<hash<decay_t<_Types>>, _Types> && ...))
1911 __detail::__variant::__raw_visit(
1912 [&__t, &__ret](auto&& __t_mem) mutable
1914 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1915 if constexpr (!is_same_v<_Type,
1916 __detail::__variant::__variant_cookie>)
1917 __ret = std::hash<size_t>{}(__t.index())
1918 + std::hash<_Type>{}(__t_mem);
1920 __ret = std::hash<size_t>{}(__t.index());
1926 template<typename... _Types>
1927 struct __variant_hash_call_base_impl<false, _Types...> {};
1929 template<typename... _Types>
1930 using __variant_hash_call_base =
1931 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1932 __enable_hash_call &&...), _Types...>;
1935 template<typename... _Types>
1936 struct hash<variant<_Types...>>
1937 : private __detail::__variant::_Variant_hash_base<
1938 variant<_Types...>, std::index_sequence_for<_Types...>>,
1939 public __variant_hash_call_base<_Types...>
1941 using result_type [[__deprecated__]] = size_t;
1942 using argument_type [[__deprecated__]] = variant<_Types...>;
1946 struct hash<monostate>
1948 using result_type [[__deprecated__]] = size_t;
1949 using argument_type [[__deprecated__]] = monostate;
1952 operator()(const monostate&) const noexcept
1954 constexpr size_t __magic_monostate_hash = -7777;
1955 return __magic_monostate_hash;
1959 template<typename... _Types>
1960 struct __is_fast_hash<hash<variant<_Types...>>>
1961 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1964_GLIBCXX_END_NAMESPACE_VERSION
1969#endif // _GLIBCXX_VARIANT