29 #ifndef _GLIBCXX_VARIANT
30 #define _GLIBCXX_VARIANT 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201703L
48 #if __cplusplus > 201703L
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 template<
size_t _Np,
typename... _Types>
63 template<
size_t _Np,
typename _First,
typename... _Rest>
64 struct _Nth_type<_Np, _First, _Rest...>
65 : _Nth_type<_Np-1, _Rest...> { };
67 template<
typename _First,
typename... _Rest>
68 struct _Nth_type<0, _First, _Rest...>
69 {
using type = _First; };
74 #define __cpp_lib_variant 201606L
76 template<
typename... _Types>
class tuple;
77 template<
typename... _Types>
class variant;
78 template <
typename>
struct hash;
80 template<
typename _Variant>
83 template<
typename _Variant>
84 struct variant_size<const _Variant> : variant_size<_Variant> {};
86 template<
typename _Variant>
87 struct variant_size<volatile _Variant> : variant_size<_Variant> {};
89 template<
typename _Variant>
90 struct variant_size<const volatile _Variant> : variant_size<_Variant> {};
92 template<
typename... _Types>
93 struct variant_size<variant<_Types...>>
96 template<
typename _Variant>
97 inline constexpr
size_t variant_size_v = variant_size<_Variant>::value;
99 template<
size_t _Np,
typename _Variant>
100 struct variant_alternative;
102 template<
size_t _Np,
typename _First,
typename... _Rest>
103 struct variant_alternative<_Np, variant<_First, _Rest...>>
104 : variant_alternative<_Np-1, variant<_Rest...>> {};
106 template<
typename _First,
typename... _Rest>
107 struct variant_alternative<0, variant<_First, _Rest...>>
108 {
using type = _First; };
110 template<
size_t _Np,
typename _Variant>
111 using variant_alternative_t =
112 typename variant_alternative<_Np, _Variant>::type;
114 template<
size_t _Np,
typename _Variant>
115 struct variant_alternative<_Np, const _Variant>
116 {
using type = add_const_t<variant_alternative_t<_Np, _Variant>>; };
118 template<
size_t _Np,
typename _Variant>
119 struct variant_alternative<_Np, volatile _Variant>
120 {
using type = add_volatile_t<variant_alternative_t<_Np, _Variant>>; };
122 template<
size_t _Np,
typename _Variant>
123 struct variant_alternative<_Np, const volatile _Variant>
124 {
using type = add_cv_t<variant_alternative_t<_Np, _Variant>>; };
126 inline constexpr
size_t variant_npos = -1;
128 template<
size_t _Np,
typename... _Types>
129 constexpr variant_alternative_t<_Np, variant<_Types...>>&
130 get(variant<_Types...>&);
132 template<
size_t _Np,
typename... _Types>
133 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
134 get(variant<_Types...>&&);
136 template<
size_t _Np,
typename... _Types>
137 constexpr variant_alternative_t<_Np, variant<_Types...>>
const&
138 get(
const variant<_Types...>&);
140 template<
size_t _Np,
typename... _Types>
141 constexpr variant_alternative_t<_Np, variant<_Types...>>
const&&
142 get(
const variant<_Types...>&&);
144 template<
typename _Result_type,
typename _Visitor,
typename... _Variants>
145 constexpr decltype(
auto)
146 __do_visit(_Visitor&& __visitor, _Variants&&... __variants);
148 template <typename... _Types, typename _Tp>
150 __variant_cast(_Tp&& __rhs)
152 if constexpr (is_lvalue_reference_v<_Tp>)
154 if constexpr (is_const_v<remove_reference_t<_Tp>>)
155 return static_cast<const variant<_Types...>&>(__rhs);
157 return static_cast<variant<_Types...>&>(__rhs);
160 return static_cast<variant<_Types...>&&>(__rhs);
169 template<
typename _Tp,
typename... _Types>
172 template<
typename _Tp,
typename... _Types>
173 inline constexpr
size_t __index_of_v = __index_of<_Tp, _Types...>::value;
175 template<
typename _Tp,
typename _First,
typename... _Rest>
176 struct __index_of<_Tp, _First, _Rest...> :
178 ? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
181 struct __variant_cookie {};
183 struct __variant_idx_cookie {
using type = __variant_idx_cookie; };
185 template<
typename _Tp>
struct __deduce_visit_result {
using type = _Tp; };
188 template<
typename _Visitor,
typename... _Variants>
190 __raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
192 std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
193 std::forward<_Variants>(__variants)...);
197 template<
typename _Visitor,
typename... _Variants>
199 __raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
201 std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
202 std::forward<_Variants>(__variants)...);
207 template<
typename _Type,
bool = std::is_trivially_destructible_v<_Type>>
208 struct _Uninitialized;
210 template<
typename _Type>
211 struct _Uninitialized<_Type, true>
213 template<
typename... _Args>
215 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
219 constexpr
const _Type& _M_get() const & noexcept
220 {
return _M_storage; }
222 constexpr _Type& _M_get() & noexcept
223 {
return _M_storage; }
225 constexpr
const _Type&& _M_get() const && noexcept
228 constexpr _Type&& _M_get() && noexcept
234 template<
typename _Type>
235 struct _Uninitialized<_Type, false>
237 template<
typename... _Args>
239 _Uninitialized(in_place_index_t<0>, _Args&&... __args)
242 _Type(std::forward<_Args>(__args)...);
245 const _Type& _M_get() const & noexcept
246 {
return *_M_storage._M_ptr(); }
248 _Type& _M_get() & noexcept
249 {
return *_M_storage._M_ptr(); }
251 const _Type&& _M_get() const && noexcept
252 {
return std::move(*_M_storage._M_ptr()); }
254 _Type&& _M_get() && noexcept
255 {
return std::move(*_M_storage._M_ptr()); }
257 __gnu_cxx::__aligned_membuf<_Type> _M_storage;
260 template<
typename _Union>
261 constexpr decltype(
auto)
262 __get(in_place_index_t<0>, _Union&& __u) noexcept
263 {
return std::forward<_Union>(__u)._M_first._M_get(); }
265 template<
size_t _Np,
typename _Union>
266 constexpr decltype(
auto)
267 __get(in_place_index_t<_Np>, _Union&& __u) noexcept
269 return __variant::__get(in_place_index<_Np-1>,
270 std::forward<_Union>(__u)._M_rest);
274 template<
size_t _Np,
typename _Variant>
275 constexpr decltype(
auto)
276 __get(_Variant&& __v) noexcept
278 return __variant::__get(std::in_place_index<_Np>,
279 std::forward<_Variant>(__v)._M_u);
282 template<
typename... _Types>
285 static constexpr
bool _S_default_ctor =
286 is_default_constructible_v<
typename _Nth_type<0, _Types...>::type>;
287 static constexpr
bool _S_copy_ctor =
288 (is_copy_constructible_v<_Types> && ...);
289 static constexpr
bool _S_move_ctor =
290 (is_move_constructible_v<_Types> && ...);
291 static constexpr
bool _S_copy_assign =
293 && (is_copy_assignable_v<_Types> && ...);
294 static constexpr
bool _S_move_assign =
296 && (is_move_assignable_v<_Types> && ...);
298 static constexpr
bool _S_trivial_dtor =
299 (is_trivially_destructible_v<_Types> && ...);
300 static constexpr
bool _S_trivial_copy_ctor =
301 (is_trivially_copy_constructible_v<_Types> && ...);
302 static constexpr
bool _S_trivial_move_ctor =
303 (is_trivially_move_constructible_v<_Types> && ...);
304 static constexpr
bool _S_trivial_copy_assign =
305 _S_trivial_dtor && _S_trivial_copy_ctor
306 && (is_trivially_copy_assignable_v<_Types> && ...);
307 static constexpr
bool _S_trivial_move_assign =
308 _S_trivial_dtor && _S_trivial_move_ctor
309 && (is_trivially_move_assignable_v<_Types> && ...);
313 static constexpr
bool _S_nothrow_default_ctor =
314 is_nothrow_default_constructible_v<
315 typename _Nth_type<0, _Types...>::type>;
316 static constexpr
bool _S_nothrow_copy_ctor =
false;
317 static constexpr
bool _S_nothrow_move_ctor =
318 (is_nothrow_move_constructible_v<_Types> && ...);
319 static constexpr
bool _S_nothrow_copy_assign =
false;
320 static constexpr
bool _S_nothrow_move_assign =
322 && (is_nothrow_move_assignable_v<_Types> && ...);
326 template<
typename... _Types>
327 union _Variadic_union { };
329 template<
typename _First,
typename... _Rest>
330 union _Variadic_union<_First, _Rest...>
332 constexpr _Variadic_union() : _M_rest() { }
334 template<
typename... _Args>
335 constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
336 : _M_first(in_place_index<0>,
std::
forward<_Args>(__args)...)
339 template<
size_t _Np,
typename... _Args>
340 constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
341 : _M_rest(in_place_index<_Np-1>,
std::
forward<_Args>(__args)...)
344 _Uninitialized<_First> _M_first;
345 _Variadic_union<_Rest...> _M_rest;
353 template<
typename _Tp>
354 struct _Never_valueless_alt
355 : __and_<bool_constant<sizeof(_Tp) <= 256>, is_trivially_copyable<_Tp>>
368 template <typename... _Types>
369 constexpr bool __never_valueless()
371 return _Traits<_Types...>::_S_move_assign
372 && (_Never_valueless_alt<_Types>::value && ...);
376 template<bool __trivially_destructible, typename... _Types>
377 struct _Variant_storage;
379 template <typename... _Types>
380 using __select_index =
381 typename __select_int::_Select_int_base<sizeof...(_Types),
383 unsigned short>::type::value_type;
385 template<typename... _Types>
386 struct _Variant_storage<false, _Types...>
390 : _M_index(static_cast<__index_type>(variant_npos))
393 template<size_t _Np, typename... _Args>
395 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
396 : _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
402 if (!_M_valid()) [[unlikely]]
405 std::__do_visit<void>([](auto&& __this_mem) mutable
407 std::_Destroy(std::__addressof(__this_mem));
408 }, __variant_cast<_Types...>(*this));
410 _M_index = static_cast<__index_type>(variant_npos);
417 _M_storage() const noexcept
419 return const_cast<void*>(static_cast<const void*>(
420 std::addressof(_M_u)));
424 _M_valid() const noexcept
426 if constexpr (__variant::__never_valueless<_Types...>())
428 return this->_M_index != __index_type(variant_npos);
431 _Variadic_union<_Types...> _M_u;
432 using __index_type = __select_index<_Types...>;
433 __index_type _M_index;
436 template<typename... _Types>
437 struct _Variant_storage<true, _Types...>
441 : _M_index(static_cast<__index_type>(variant_npos))
444 template<
size_t _Np,
typename... _Args>
446 _Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
447 : _M_u(in_place_index<_Np>,
std::
forward<_Args>(__args)...),
451 void _M_reset() noexcept
452 { _M_index =
static_cast<__index_type
>(variant_npos); }
455 _M_storage() const noexcept
457 return const_cast<void*
>(
static_cast<const void*
>(
462 _M_valid() const noexcept
464 if constexpr (__variant::__never_valueless<_Types...>())
466 return this->_M_index != static_cast<__index_type>(variant_npos);
469 _Variadic_union<_Types...> _M_u;
470 using __index_type = __select_index<_Types...>;
471 __index_type _M_index;
474 template<typename... _Types>
475 using _Variant_storage_alias =
476 _Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
478 template<typename _Tp, typename _Up>
479 void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
483 if constexpr (!is_same_v<_Type, __variant_cookie>)
485 _Type(
std::
forward<decltype(__rhs_mem)>(__rhs_mem));
488 template<
typename... _Types,
typename _Tp,
typename _Up>
489 void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
491 __lhs._M_index = __rhs._M_index;
492 __variant::__raw_visit([&__lhs](
auto&& __rhs_mem)
mutable
494 __variant_construct_single(std::forward<_Tp>(__lhs),
496 }, __variant_cast<_Types...>(std::forward<_Up>(__rhs)));
502 template<bool,
typename... _Types>
503 struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
505 using _Base = _Variant_storage_alias<_Types...>;
508 _Copy_ctor_base(
const _Copy_ctor_base& __rhs)
509 noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
511 __variant_construct<_Types...>(*
this, __rhs);
514 _Copy_ctor_base(_Copy_ctor_base&&) =
default;
515 _Copy_ctor_base&
operator=(
const _Copy_ctor_base&) =
default;
516 _Copy_ctor_base&
operator=(_Copy_ctor_base&&) =
default;
519 template<
typename... _Types>
520 struct _Copy_ctor_base<true, _Types...> : _Variant_storage_alias<_Types...>
522 using _Base = _Variant_storage_alias<_Types...>;
526 template<
typename... _Types>
527 using _Copy_ctor_alias =
528 _Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
530 template<bool,
typename... _Types>
531 struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
533 using _Base = _Copy_ctor_alias<_Types...>;
536 _Move_ctor_base(_Move_ctor_base&& __rhs)
537 noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
539 __variant_construct<_Types...>(*
this,
std::move(__rhs));
542 template<
typename _Up>
543 void _M_destructive_move(
unsigned short __rhs_index, _Up&& __rhs)
546 __variant_construct_single(*
this, std::forward<_Up>(__rhs));
547 this->_M_index = __rhs_index;
550 template<
typename _Up>
551 void _M_destructive_copy(
unsigned short __rhs_index,
const _Up& __rhs)
554 __variant_construct_single(*
this, __rhs);
555 this->_M_index = __rhs_index;
558 _Move_ctor_base(
const _Move_ctor_base&) =
default;
559 _Move_ctor_base&
operator=(
const _Move_ctor_base&) =
default;
560 _Move_ctor_base&
operator=(_Move_ctor_base&&) =
default;
563 template<
typename... _Types>
564 struct _Move_ctor_base<true, _Types...> : _Copy_ctor_alias<_Types...>
566 using _Base = _Copy_ctor_alias<_Types...>;
569 template<
typename _Up>
570 void _M_destructive_move(
unsigned short __rhs_index, _Up&& __rhs)
573 __variant_construct_single(*
this, std::forward<_Up>(__rhs));
574 this->_M_index = __rhs_index;
577 template<
typename _Up>
578 void _M_destructive_copy(
unsigned short __rhs_index,
const _Up& __rhs)
581 __variant_construct_single(*
this, __rhs);
582 this->_M_index = __rhs_index;
586 template<
typename... _Types>
587 using _Move_ctor_alias =
588 _Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
590 template<bool,
typename... _Types>
591 struct _Copy_assign_base : _Move_ctor_alias<_Types...>
593 using _Base = _Move_ctor_alias<_Types...>;
597 operator=(
const _Copy_assign_base& __rhs)
598 noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
600 __variant::__raw_idx_visit(
601 [
this](
auto&& __rhs_mem,
auto __rhs_index)
mutable
603 if constexpr (__rhs_index != variant_npos)
605 if (this->_M_index == __rhs_index)
606 __variant::__get<__rhs_index>(*
this) = __rhs_mem;
609 using __rhs_type = __remove_cvref_t<decltype(__rhs_mem)>;
610 if constexpr (is_nothrow_copy_constructible_v<__rhs_type>
611 || !is_nothrow_move_constructible_v<__rhs_type>)
619 this->_M_destructive_copy(__rhs_index, __rhs_mem);
621 __variant_cast<_Types...>(*this)
622 = variant<_Types...>(std::in_place_index<__rhs_index>,
628 }, __variant_cast<_Types...>(__rhs));
632 _Copy_assign_base(
const _Copy_assign_base&) =
default;
633 _Copy_assign_base(_Copy_assign_base&&) =
default;
634 _Copy_assign_base&
operator=(_Copy_assign_base&&) =
default;
637 template<
typename... _Types>
638 struct _Copy_assign_base<true, _Types...> : _Move_ctor_alias<_Types...>
640 using _Base = _Move_ctor_alias<_Types...>;
644 template<
typename... _Types>
645 using _Copy_assign_alias =
646 _Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
648 template<bool,
typename... _Types>
649 struct _Move_assign_base : _Copy_assign_alias<_Types...>
651 using _Base = _Copy_assign_alias<_Types...>;
656 noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
658 __variant::__raw_idx_visit(
659 [
this](
auto&& __rhs_mem,
auto __rhs_index)
mutable
661 if constexpr (__rhs_index != variant_npos)
663 if (this->_M_index == __rhs_index)
664 __variant::__get<__rhs_index>(*
this) =
std::move(__rhs_mem);
666 __variant_cast<_Types...>(*this)
667 .template emplace<__rhs_index>(
std::move(__rhs_mem));
671 }, __variant_cast<_Types...>(__rhs));
675 _Move_assign_base(
const _Move_assign_base&) =
default;
676 _Move_assign_base(_Move_assign_base&&) =
default;
677 _Move_assign_base&
operator=(
const _Move_assign_base&) =
default;
680 template<
typename... _Types>
681 struct _Move_assign_base<true, _Types...> : _Copy_assign_alias<_Types...>
683 using _Base = _Copy_assign_alias<_Types...>;
687 template<
typename... _Types>
688 using _Move_assign_alias =
689 _Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
691 template<
typename... _Types>
692 struct _Variant_base : _Move_assign_alias<_Types...>
694 using _Base = _Move_assign_alias<_Types...>;
698 noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
699 : _Variant_base(in_place_index<0>) { }
701 template<
size_t _Np,
typename... _Args>
703 _Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
707 _Variant_base(
const _Variant_base&) =
default;
708 _Variant_base(_Variant_base&&) =
default;
709 _Variant_base&
operator=(
const _Variant_base&) =
default;
710 _Variant_base&
operator=(_Variant_base&&) =
default;
714 template<
typename _Tp,
typename _Tuple>
715 struct __tuple_count;
717 template<
typename _Tp,
typename _Tuple>
718 inline constexpr
size_t __tuple_count_v =
719 __tuple_count<_Tp, _Tuple>::value;
721 template<
typename _Tp,
typename... _Types>
722 struct __tuple_count<_Tp, tuple<_Types...>>
723 : integral_constant<size_t, 0> { };
725 template<
typename _Tp,
typename _First,
typename... _Rest>
726 struct __tuple_count<_Tp, tuple<_First, _Rest...>>
729 __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
732 template<
typename _Tp,
typename... _Types>
733 inline constexpr
bool __exactly_once =
734 __tuple_count_v<_Tp, tuple<_Types...>> == 1;
737 template<
typename _Ti>
struct _Arr { _Ti _M_x[1]; };
740 template<
size_t _Ind,
typename _Tp,
typename _Ti,
typename =
void>
749 template<
size_t _Ind,
typename _Tp,
typename _Ti>
750 struct _Build_FUN<_Ind, _Tp, _Ti,
751 void_t<decltype(_Arr<_Ti>{{std::declval<_Tp>()}})>>
754 static integral_constant<size_t, _Ind> _S_fun(_Ti);
757 template<
typename _Tp,
typename _Variant,
758 typename = make_index_sequence<variant_size_v<_Variant>>>
761 template<
typename _Tp,
typename... _Ti,
size_t... _Ind>
763 : _Build_FUN<_Ind, _Tp, _Ti>...
765 using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
770 template<
typename _Tp,
typename _Variant>
772 = decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
775 template<
typename _Tp,
typename _Variant,
typename =
void>
776 struct __accepted_index
777 : integral_constant<size_t, variant_npos>
780 template<
typename _Tp,
typename _Variant>
781 struct __accepted_index<_Tp, _Variant,
void_t<_FUN_type<_Tp, _Variant>>>
782 : _FUN_type<_Tp, _Variant>
786 template<
typename _Variant>
787 void* __get_storage(_Variant&& __v) noexcept
788 {
return __v._M_storage(); }
790 template <
typename _Maybe_variant_cookie,
typename _Variant>
791 struct _Extra_visit_slot_needed
793 template <
typename>
struct _Variant_never_valueless;
795 template <
typename... _Types>
796 struct _Variant_never_valueless<variant<_Types...>>
797 : bool_constant<__variant::__never_valueless<_Types...>()> {};
799 static constexpr
bool value =
800 (is_same_v<_Maybe_variant_cookie, __variant_cookie>
801 || is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
802 && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
806 template<
typename _Tp,
size_t... _Dimensions>
810 template<
typename _Tp>
811 struct _Multi_array<_Tp>
814 struct __untag_result
818 template <
typename... _Args>
819 struct __untag_result<const void(*)(_Args...)>
823 template <
typename... _Args>
824 struct __untag_result<__variant_cookie(*)(_Args...)>
828 template <
typename... _Args>
829 struct __untag_result<__variant_idx_cookie(*)(_Args...)>
833 template <
typename _Res,
typename... _Args>
834 struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
838 using __result_is_deduced = __untag_result<_Tp>;
848 template<
typename _Ret,
850 typename... _Variants,
851 size_t __first,
size_t... __rest>
852 struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
854 static constexpr
size_t __index =
855 sizeof...(_Variants) -
sizeof...(__rest) - 1;
857 using _Variant =
typename _Nth_type<__index, _Variants...>::type;
859 static constexpr
int __do_cookie =
860 _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
862 using _Tp = _Ret(*)(_Visitor, _Variants...);
864 template<
typename... _Args>
865 constexpr decltype(
auto)
866 _M_access(
size_t __first_index, _Args... __rest_indices)
const
868 return _M_arr[__first_index + __do_cookie]
869 ._M_access(__rest_indices...);
872 _Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
902 template<
typename _Array_type,
typename _Index_seq>
903 struct __gen_vtable_impl;
912 template<
typename _Result_type,
typename _Visitor,
size_t... __dimensions,
913 typename... _Variants,
size_t... __indices>
914 struct __gen_vtable_impl<
915 _Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
920 _Variants...>::type>;
922 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
925 static constexpr _Array_type
928 _Array_type __vtable{};
934 template<
size_t... __var_indices>
935 static constexpr
void
936 _S_apply_all_alts(_Array_type& __vtable,
939 if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
940 (_S_apply_single_alt<true, __var_indices>(
941 __vtable._M_arr[__var_indices + 1],
942 &(__vtable._M_arr[0])), ...);
944 (_S_apply_single_alt<false, __var_indices>(
945 __vtable._M_arr[__var_indices]), ...);
948 template<
bool __do_cookie,
size_t __index,
typename _Tp>
949 static constexpr
void
950 _S_apply_single_alt(_Tp& __element, _Tp* __cookie_element =
nullptr)
952 if constexpr (__do_cookie)
954 __element = __gen_vtable_impl<
957 *__cookie_element = __gen_vtable_impl<
963 auto __tmp_element = __gen_vtable_impl<
966 static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
967 "std::visit requires the visitor to have the same "
968 "return type for all alternatives of a variant");
969 __element = __tmp_element;
977 template<
typename _Result_type,
typename _Visitor,
typename... _Variants,
979 struct __gen_vtable_impl<
980 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
984 _Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
986 template<
size_t __index,
typename _Variant>
987 static constexpr decltype(
auto)
988 __element_by_index_or_cookie(_Variant&& __var) noexcept
990 if constexpr (__index != variant_npos)
991 return __variant::__get<__index>(std::forward<_Variant>(__var));
993 return __variant_cookie{};
996 static constexpr decltype(
auto)
997 __visit_invoke(_Visitor&& __visitor, _Variants... __vars)
999 if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
1003 __element_by_index_or_cookie<__indices>(
1004 std::forward<_Variants>(__vars))...,
1005 integral_constant<size_t, __indices>()...);
1006 else if constexpr (is_same_v<_Result_type, __variant_cookie>)
1009 __element_by_index_or_cookie<__indices>(
1010 std::forward<_Variants>(__vars))...);
1011 else if constexpr (_Array_type::__result_is_deduced::value)
1014 __element_by_index_or_cookie<__indices>(
1015 std::forward<_Variants>(__vars))...);
1017 return std::__invoke_r<_Result_type>(
1018 std::forward<_Visitor>(__visitor),
1019 __variant::__get<__indices>(std::forward<_Variants>(__vars))...);
1022 static constexpr
auto
1025 if constexpr (_Array_type::__result_is_deduced::value)
1027 constexpr
bool __visit_ret_type_mismatch =
1028 !is_same_v<
typename _Result_type::type,
1029 decltype(__visit_invoke(std::declval<_Visitor>(),
1030 std::declval<_Variants>()...))>;
1031 if constexpr (__visit_ret_type_mismatch)
1033 struct __cannot_match {};
1034 return __cannot_match{};
1037 return _Array_type{&__visit_invoke};
1040 return _Array_type{&__visit_invoke};
1044 template<
typename _Result_type,
typename _Visitor,
typename... _Variants>
1048 _Multi_array<_Result_type (*)(_Visitor, _Variants...),
1049 variant_size_v<remove_reference_t<_Variants>>...>;
1051 static constexpr _Array_type _S_vtable
1052 = __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
1055 template<
size_t _Np,
typename _Tp>
1056 struct _Base_dedup :
public _Tp { };
1058 template<
typename _Variant,
typename __indices>
1059 struct _Variant_hash_base;
1061 template<
typename... _Types,
size_t... __indices>
1062 struct _Variant_hash_base<variant<_Types...>,
1064 : _Base_dedup<__indices, __poison_hash<remove_const_t<_Types>>>... { };
1069 template<
size_t _Np,
typename _Variant,
typename... _Args>
1070 void __variant_construct_by_index(_Variant& __v, _Args&&... __args)
1073 auto&& __storage = __detail::__variant::__get<_Np>(__v);
1075 remove_reference_t<decltype(__storage)>
1076 (std::forward<_Args>(__args)...);
1079 template<
typename _Tp,
typename... _Types>
1081 holds_alternative(
const variant<_Types...>& __v) noexcept
1083 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1084 "T must occur exactly once in alternatives");
1085 return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
1088 template<
typename _Tp,
typename... _Types>
1089 constexpr _Tp& get(variant<_Types...>& __v)
1091 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1092 "T must occur exactly once in alternatives");
1093 static_assert(!is_void_v<_Tp>,
"_Tp must not be void");
1094 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1097 template<
typename _Tp,
typename... _Types>
1098 constexpr _Tp&& get(variant<_Types...>&& __v)
1100 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1101 "T must occur exactly once in alternatives");
1102 static_assert(!is_void_v<_Tp>,
"_Tp must not be void");
1103 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1107 template<
typename _Tp,
typename... _Types>
1108 constexpr
const _Tp& get(
const variant<_Types...>& __v)
1110 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1111 "T must occur exactly once in alternatives");
1112 static_assert(!is_void_v<_Tp>,
"_Tp must not be void");
1113 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
1116 template<
typename _Tp,
typename... _Types>
1117 constexpr
const _Tp&& get(
const variant<_Types...>&& __v)
1119 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1120 "T must occur exactly once in alternatives");
1121 static_assert(!is_void_v<_Tp>,
"_Tp must not be void");
1122 return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1126 template<
size_t _Np,
typename... _Types>
1127 constexpr
add_pointer_t<variant_alternative_t<_Np, variant<_Types...>>>
1128 get_if(variant<_Types...>* __ptr) noexcept
1130 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1131 static_assert(_Np <
sizeof...(_Types),
1132 "The index must be in [0, number of alternatives)");
1133 static_assert(!is_void_v<_Alternative_type>,
"_Tp must not be void");
1134 if (__ptr && __ptr->index() == _Np)
1139 template<
size_t _Np,
typename... _Types>
1141 add_pointer_t<
const variant_alternative_t<_Np, variant<_Types...>>>
1142 get_if(
const variant<_Types...>* __ptr) noexcept
1144 using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
1145 static_assert(_Np <
sizeof...(_Types),
1146 "The index must be in [0, number of alternatives)");
1147 static_assert(!is_void_v<_Alternative_type>,
"_Tp must not be void");
1148 if (__ptr && __ptr->index() == _Np)
1153 template<
typename _Tp,
typename... _Types>
1154 constexpr add_pointer_t<_Tp>
1155 get_if(variant<_Types...>* __ptr) noexcept
1157 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1158 "T must occur exactly once in alternatives");
1159 static_assert(!is_void_v<_Tp>,
"_Tp must not be void");
1160 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1164 template<
typename _Tp,
typename... _Types>
1165 constexpr add_pointer_t<const _Tp>
1166 get_if(
const variant<_Types...>* __ptr) noexcept
1168 static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
1169 "T must occur exactly once in alternatives");
1170 static_assert(!is_void_v<_Tp>,
"_Tp must not be void");
1171 return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
1175 struct monostate { };
1177 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
1178 template<typename... _Types> \
1179 constexpr bool operator __OP(const variant<_Types...>& __lhs, \
1180 const variant<_Types...>& __rhs) \
1182 bool __ret = true; \
1183 __detail::__variant::__raw_idx_visit( \
1184 [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
1186 if constexpr (__rhs_index != variant_npos) \
1188 if (__lhs.index() == __rhs_index) \
1190 auto& __this_mem = std::get<__rhs_index>(__lhs); \
1191 __ret = __this_mem __OP __rhs_mem; \
1194 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1197 __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
1202 _VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
1203 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
1204 _VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
1205 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
1206 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
1207 _VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
1209 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1211 constexpr
bool operator==(monostate, monostate) noexcept {
return true; }
1213 #ifdef __cpp_lib_three_way_comparison
1214 template<
typename... _Types>
1215 requires (three_way_comparable<_Types> && ...)
1217 common_comparison_category_t<compare_three_way_result_t<_Types>...>
1218 operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
1220 common_comparison_category_t<compare_three_way_result_t<_Types>...> __ret
1221 = strong_ordering::equal;
1223 __detail::__variant::__raw_idx_visit(
1224 [&__ret, &__v] (
auto&& __w_mem,
auto __w_index)
mutable
1226 if constexpr (__w_index != variant_npos)
1228 if (__v.index() == __w_index)
1230 auto& __this_mem = std::get<__w_index>(__v);
1231 __ret = __this_mem <=> __w_mem;
1235 __ret = (__v.index() + 1) <=> (__w_index + 1);
1240 constexpr strong_ordering
1241 operator<=>(monostate, monostate) noexcept {
return strong_ordering::equal; }
1243 constexpr
bool operator!=(monostate, monostate) noexcept {
return false; }
1244 constexpr
bool operator<(monostate, monostate) noexcept {
return false; }
1245 constexpr
bool operator>(monostate, monostate) noexcept {
return false; }
1246 constexpr
bool operator<=(monostate, monostate) noexcept {
return true; }
1247 constexpr
bool operator>=(monostate, monostate) noexcept {
return true; }
1250 template<
typename _Visitor,
typename... _Variants>
1251 constexpr decltype(
auto) visit(_Visitor&&, _Variants&&...);
1253 template<typename... _Types>
1254 inline
enable_if_t<(is_move_constructible_v<_Types> && ...)
1256 swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
1257 noexcept(noexcept(__lhs.
swap(__rhs)))
1258 { __lhs.swap(__rhs); }
1260 template<
typename... _Types>
1261 enable_if_t<!((is_move_constructible_v<_Types> && ...)
1262 && (is_swappable_v<_Types> && ...))>
1263 swap(variant<_Types...>&, variant<_Types...>&) =
delete;
1265 class bad_variant_access :
public exception
1268 bad_variant_access() noexcept { }
1270 const char* what() const noexcept
override
1271 {
return _M_reason; }
1274 bad_variant_access(
const char* __reason) noexcept : _M_reason(__reason) { }
1277 const char* _M_reason =
"bad variant access";
1279 friend void __throw_bad_variant_access(
const char* __what);
1284 __throw_bad_variant_access(
const char* __what)
1285 { _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
1288 __throw_bad_variant_access(
bool __valueless)
1290 if (__valueless) [[__unlikely__]]
1291 __throw_bad_variant_access(
"std::get: variant is valueless");
1293 __throw_bad_variant_access(
"std::get: wrong index for variant");
1296 template<
typename... _Types>
1298 :
private __detail::__variant::_Variant_base<_Types...>,
1299 private _Enable_default_constructor<
1300 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1301 variant<_Types...>>,
1302 private _Enable_copy_move<
1303 __detail::__variant::_Traits<_Types...>::_S_copy_ctor,
1304 __detail::__variant::_Traits<_Types...>::_S_copy_assign,
1305 __detail::__variant::_Traits<_Types...>::_S_move_ctor,
1306 __detail::__variant::_Traits<_Types...>::_S_move_assign,
1310 template <
typename... _UTypes,
typename _Tp>
1311 friend decltype(
auto) __variant_cast(_Tp&&);
1312 template<
size_t _Np, typename _Variant, typename... _Args>
1313 friend
void __variant_construct_by_index(_Variant& __v,
1316 static_assert(sizeof...(_Types) > 0,
1317 "variant must have at least one alternative");
1318 static_assert(!(
std::is_reference_v<_Types> || ...),
1319 "variant must have no reference alternative");
1320 static_assert(!(
std::is_void_v<_Types> || ...),
1321 "variant must have no
void alternative");
1323 using _Base = __detail::__variant::_Variant_base<_Types...>;
1324 using _Default_ctor_enabler =
1325 _Enable_default_constructor<
1326 __detail::__variant::_Traits<_Types...>::_S_default_ctor,
1327 variant<_Types...>>;
1329 template<typename _Tp>
1330 static constexpr
bool __not_self
1331 = !is_same_v<__remove_cvref_t<_Tp>, variant>;
1333 template<typename _Tp>
1334 static constexpr
bool
1335 __exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
1337 template<typename _Tp>
1338 static constexpr
size_t __accepted_index
1339 = __detail::__variant::__accepted_index<_Tp, variant>::value;
1341 template<
size_t _Np, typename =
enable_if_t<(_Np < sizeof...(_Types))>>
1342 using __to_type = variant_alternative_t<_Np, variant>;
1344 template<typename _Tp, typename =
enable_if_t<__not_self<_Tp>>>
1345 using __accepted_type = __to_type<__accepted_index<_Tp>>;
1347 template<typename _Tp>
1348 static constexpr
size_t __index_of =
1349 __detail::__variant::__index_of_v<_Tp, _Types...>;
1351 using _Traits = __detail::__variant::_Traits<_Types...>;
1353 template<typename _Tp>
1355 template<
typename _Tp>
1356 struct __is_in_place_tag<in_place_type_t<_Tp>> :
true_type { };
1357 template<
size_t _Np>
1358 struct __is_in_place_tag<in_place_index_t<_Np>> :
true_type { };
1360 template<
typename _Tp>
1361 static constexpr
bool __not_in_place_tag
1362 = !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
1365 variant() =
default;
1366 variant(
const variant& __rhs) =
default;
1367 variant(variant&&) =
default;
1368 variant&
operator=(
const variant&) =
default;
1369 variant&
operator=(variant&&) =
default;
1370 ~variant() =
default;
1372 template<
typename _Tp,
1375 typename _Tj = __accepted_type<_Tp&&>,
1377 && is_constructible_v<_Tj, _Tp>>>
1380 noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
1381 : variant(in_place_index<__accepted_index<_Tp>>,
1385 template<
typename _Tp,
typename... _Args,
1386 typename = enable_if_t<__exactly_once<_Tp>
1387 && is_constructible_v<_Tp, _Args...>>>
1389 variant(in_place_type_t<_Tp>, _Args&&... __args)
1390 : variant(in_place_index<__index_of<_Tp>>,
1394 template<
typename _Tp,
typename _Up,
typename... _Args,
1395 typename = enable_if_t<__exactly_once<_Tp>
1396 && is_constructible_v<_Tp,
1397 initializer_list<_Up>&, _Args...>>>
1399 variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
1401 : variant(in_place_index<__index_of<_Tp>>, __il,
1405 template<
size_t _Np,
typename... _Args,
1406 typename _Tp = __to_type<_Np>,
1407 typename =
enable_if_t<is_constructible_v<_Tp, _Args...>>>
1409 variant(in_place_index_t<_Np>, _Args&&... __args)
1410 : _Base(in_place_index<_Np>,
std::
forward<_Args>(__args)...),
1411 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1414 template<
size_t _Np,
typename _Up,
typename... _Args,
1415 typename _Tp = __to_type<_Np>,
1417 initializer_list<_Up>&,
1420 variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
1422 : _Base(in_place_index<_Np>, __il,
std::
forward<_Args>(__args)...),
1423 _Default_ctor_enabler(_Enable_default_constructor_tag{})
1426 template<
typename _Tp>
1427 enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
1428 && is_constructible_v<__accepted_type<_Tp&&>, _Tp>
1429 && is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
1432 noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
1433 && is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
1435 constexpr
auto __index = __accepted_index<_Tp>;
1436 if (index() == __index)
1437 std::get<__index>(*
this) = std::forward<_Tp>(__rhs);
1440 using _Tj = __accepted_type<_Tp&&>;
1441 if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
1442 || !is_nothrow_move_constructible_v<_Tj>)
1443 this->emplace<__index>(std::forward<_Tp>(__rhs));
1445 operator=(variant(std::forward<_Tp>(__rhs)));
1450 template<
typename _Tp,
typename... _Args>
1451 enable_if_t<is_constructible_v<_Tp, _Args...> && __exactly_once<_Tp>,
1453 emplace(_Args&&... __args)
1455 constexpr
size_t __index = __index_of<_Tp>;
1456 return this->emplace<__index>(std::forward<_Args>(__args)...);
1459 template<
typename _Tp,
typename _Up,
typename... _Args>
1460 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>
1461 && __exactly_once<_Tp>,
1463 emplace(initializer_list<_Up> __il, _Args&&... __args)
1465 constexpr
size_t __index = __index_of<_Tp>;
1466 return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
1469 template<
size_t _Np,
typename... _Args>
1470 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1472 variant_alternative_t<_Np, variant>&>
1473 emplace(_Args&&... __args)
1475 static_assert(_Np <
sizeof...(_Types),
1476 "The index must be in [0, number of alternatives)");
1477 using type = variant_alternative_t<_Np, variant>;
1480 if constexpr (is_nothrow_constructible_v<type, _Args...>)
1483 __variant_construct_by_index<_Np>(*
this,
1484 std::forward<_Args>(__args)...);
1486 else if constexpr (is_scalar_v<type>)
1489 const type __tmp(std::forward<_Args>(__args)...);
1492 __variant_construct_by_index<_Np>(*
this, __tmp);
1494 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1495 && _Traits::_S_move_assign)
1498 variant __tmp(in_place_index<_Np>,
1499 std::forward<_Args>(__args)...);
1510 __variant_construct_by_index<_Np>(*
this,
1511 std::forward<_Args>(__args)...);
1515 using __index_type = decltype(this->_M_index);
1516 this->_M_index =
static_cast<__index_type
>(variant_npos);
1517 __throw_exception_again;
1520 return std::get<_Np>(*
this);
1523 template<
size_t _Np,
typename _Up,
typename... _Args>
1524 enable_if_t<is_constructible_v<variant_alternative_t<_Np, variant>,
1525 initializer_list<_Up>&, _Args...>,
1526 variant_alternative_t<_Np, variant>&>
1527 emplace(initializer_list<_Up> __il, _Args&&... __args)
1529 static_assert(_Np <
sizeof...(_Types),
1530 "The index must be in [0, number of alternatives)");
1531 using type = variant_alternative_t<_Np, variant>;
1534 if constexpr (is_nothrow_constructible_v<type,
1535 initializer_list<_Up>&,
1539 __variant_construct_by_index<_Np>(*
this, __il,
1540 std::forward<_Args>(__args)...);
1542 else if constexpr (__detail::__variant::_Never_valueless_alt<type>()
1543 && _Traits::_S_move_assign)
1546 variant __tmp(in_place_index<_Np>, __il,
1547 std::forward<_Args>(__args)...);
1558 __variant_construct_by_index<_Np>(*
this, __il,
1559 std::forward<_Args>(__args)...);
1563 using __index_type = decltype(this->_M_index);
1564 this->_M_index =
static_cast<__index_type
>(variant_npos);
1565 __throw_exception_again;
1568 return std::get<_Np>(*
this);
1571 constexpr
bool valueless_by_exception() const noexcept
1572 {
return !this->_M_valid(); }
1574 constexpr
size_t index() const noexcept
1576 using __index_type =
typename _Base::__index_type;
1577 if constexpr (__detail::__variant::__never_valueless<_Types...>())
1578 return this->_M_index;
1579 else if constexpr (sizeof...(_Types) <= __index_type(-1) / 2)
1582 return
size_t(__index_type(this->_M_index + 1)) - 1;
1586 swap(variant& __rhs)
1587 noexcept((__is_nothrow_swappable<_Types>::value && ...)
1588 && is_nothrow_move_constructible_v<variant>)
1590 __detail::__variant::__raw_idx_visit(
1591 [
this, &__rhs](
auto&& __rhs_mem,
auto __rhs_index)
mutable
1593 if constexpr (__rhs_index != variant_npos)
1595 if (this->index() == __rhs_index)
1598 std::get<__rhs_index>(*
this);
1600 swap(__this_mem, __rhs_mem);
1604 if (!this->valueless_by_exception()) [[__likely__]]
1608 this->_M_destructive_move(__rhs_index,
1613 this->_M_destructive_move(__rhs_index,
1621 if (!this->valueless_by_exception()) [[__likely__]]
1632 #if defined(__clang__) && __clang_major__ <= 7
1638 template<
size_t _Np,
typename _Vp>
1639 friend constexpr decltype(
auto)
1640 __detail::__variant::__get(_Vp&& __v) noexcept;
1642 template<typename _Vp>
1644 __detail::__variant::__get_storage(_Vp&& __v) noexcept;
1646 #define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \
1647 template<typename... _Tp> \
1648 friend constexpr bool \
1649 operator __OP(const variant<_Tp...>& __lhs, \
1650 const variant<_Tp...>& __rhs);
1652 _VARIANT_RELATION_FUNCTION_TEMPLATE(<)
1653 _VARIANT_RELATION_FUNCTION_TEMPLATE(<=)
1654 _VARIANT_RELATION_FUNCTION_TEMPLATE(==)
1655 _VARIANT_RELATION_FUNCTION_TEMPLATE(!=)
1656 _VARIANT_RELATION_FUNCTION_TEMPLATE(>=)
1657 _VARIANT_RELATION_FUNCTION_TEMPLATE(>)
1659 #undef _VARIANT_RELATION_FUNCTION_TEMPLATE
1662 template<
size_t _Np,
typename... _Types>
1663 constexpr variant_alternative_t<_Np, variant<_Types...>>&
1664 get(variant<_Types...>& __v)
1666 static_assert(_Np <
sizeof...(_Types),
1667 "The index must be in [0, number of alternatives)");
1668 if (__v.index() != _Np)
1669 __throw_bad_variant_access(__v.valueless_by_exception());
1670 return __detail::__variant::__get<_Np>(__v);
1673 template<
size_t _Np,
typename... _Types>
1674 constexpr variant_alternative_t<_Np, variant<_Types...>>&&
1675 get(variant<_Types...>&& __v)
1677 static_assert(_Np <
sizeof...(_Types),
1678 "The index must be in [0, number of alternatives)");
1679 if (__v.index() != _Np)
1680 __throw_bad_variant_access(__v.valueless_by_exception());
1681 return __detail::__variant::__get<_Np>(
std::move(__v));
1684 template<
size_t _Np,
typename... _Types>
1685 constexpr
const variant_alternative_t<_Np, variant<_Types...>>&
1686 get(
const variant<_Types...>& __v)
1688 static_assert(_Np <
sizeof...(_Types),
1689 "The index must be in [0, number of alternatives)");
1690 if (__v.index() != _Np)
1691 __throw_bad_variant_access(__v.valueless_by_exception());
1692 return __detail::__variant::__get<_Np>(__v);
1695 template<
size_t _Np,
typename... _Types>
1696 constexpr
const variant_alternative_t<_Np, variant<_Types...>>&&
1697 get(
const variant<_Types...>&& __v)
1699 static_assert(_Np <
sizeof...(_Types),
1700 "The index must be in [0, number of alternatives)");
1701 if (__v.index() != _Np)
1702 __throw_bad_variant_access(__v.valueless_by_exception());
1703 return __detail::__variant::__get<_Np>(
std::move(__v));
1706 template<
typename _Result_type,
typename _Visitor,
typename... _Variants>
1707 constexpr decltype(
auto)
1708 __do_visit(_Visitor&& __visitor, _Variants&&... __variants)
1710 constexpr
auto& __vtable = __detail::__variant::__gen_vtable<
1711 _Result_type, _Visitor&&, _Variants&&...>::_S_vtable;
1713 auto __func_ptr = __vtable._M_access(__variants.index()...);
1714 return (*__func_ptr)(std::forward<_Visitor>(__visitor),
1715 std::forward<_Variants>(__variants)...);
1718 template<
typename _Tp,
typename... _Types>
1719 constexpr
inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
1721 template <
size_t _Idx,
typename _Visitor,
typename _Variant>
1723 __check_visitor_result(_Visitor&& __vis, _Variant&& __variant)
1726 std::get<_Idx>(std::forward<_Variant>(__variant)));
1729 template <
typename _Visitor,
typename _Variant,
size_t... _Idxs>
1732 return __same_types<decltype(__check_visitor_result<_Idxs>(
1733 std::declval<_Visitor>(),
1734 std::declval<_Variant>()))...>;
1738 template<
typename _Visitor,
typename... _Variants>
1739 constexpr decltype(
auto)
1740 visit(_Visitor&& __visitor, _Variants&&... __variants)
1742 if ((__variants.valueless_by_exception() || ...))
1743 __throw_bad_variant_access(
"std::visit: variant is valueless");
1746 decltype(std::get<0>(std::declval<_Variants>()))...>;
1748 using _Tag = __detail::__variant::__deduce_visit_result<_Result_type>;
1750 if constexpr (
sizeof...(_Variants) == 1)
1752 constexpr
bool __visit_rettypes_match =
1753 __check_visitor_results<_Visitor, _Variants...>(
1755 std::variant_size<remove_reference_t<_Variants>...>::value>());
1756 if constexpr (!__visit_rettypes_match)
1758 static_assert(__visit_rettypes_match,
1759 "std::visit requires the visitor to have the same "
1760 "return type for all alternatives of a variant");
1764 return std::__do_visit<_Tag>(
1765 std::forward<_Visitor>(__visitor),
1766 std::forward<_Variants>(__variants)...);
1769 return std::__do_visit<_Tag>(
1770 std::forward<_Visitor>(__visitor),
1771 std::forward<_Variants>(__variants)...);
1774 #if __cplusplus > 201703L
1775 template<
typename _Res,
typename _Visitor,
typename... _Variants>
1777 visit(_Visitor&& __visitor, _Variants&&... __variants)
1779 if ((__variants.valueless_by_exception() || ...))
1780 __throw_bad_variant_access(
"std::visit<R>: variant is valueless");
1782 return std::__do_visit<_Res>(std::forward<_Visitor>(__visitor),
1783 std::forward<_Variants>(__variants)...);
1787 template<bool,
typename... _Types>
1788 struct __variant_hash_call_base_impl
1791 operator()(
const variant<_Types...>& __t)
const
1795 __detail::__variant::__raw_visit(
1796 [&__t, &__ret](
auto&& __t_mem)
mutable
1798 using _Type = __remove_cvref_t<decltype(__t_mem)>;
1799 if constexpr (!is_same_v<_Type,
1800 __detail::__variant::__variant_cookie>)
1801 __ret =
std::hash<
size_t>{}(__t.index())
1810 template<
typename... _Types>
1811 struct __variant_hash_call_base_impl<false, _Types...> {};
1813 template<
typename... _Types>
1814 using __variant_hash_call_base =
1815 __variant_hash_call_base_impl<(__poison_hash<remove_const_t<_Types>>::
1816 __enable_hash_call &&...), _Types...>;
1818 template<
typename... _Types>
1819 struct hash<variant<_Types...>>
1820 :
private __detail::__variant::_Variant_hash_base<
1821 variant<_Types...>, std::index_sequence_for<_Types...>>,
1822 public __variant_hash_call_base<_Types...>
1824 using result_type [[__deprecated__]] = size_t;
1825 using argument_type [[__deprecated__]] = variant<_Types...>;
1829 struct hash<monostate>
1831 using result_type [[__deprecated__]] = size_t;
1832 using argument_type [[__deprecated__]] = monostate;
1835 operator()(
const monostate&)
const noexcept
1837 constexpr
size_t __magic_monostate_hash = -7777;
1838 return __magic_monostate_hash;
1842 template<
typename... _Types>
1843 struct __is_fast_hash<hash<variant<_Types...>>>
1844 : bool_constant<(__is_fast_hash<_Types>::value && ...)>
1847 _GLIBCXX_END_NAMESPACE_VERSION
auto_ptr & operator=(auto_ptr &__a)
auto_ptr assignment operator.
_Tp element_type
The pointed-to type.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
void void_t
A metafunction that always yields void, used for detecting valid types.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
typename add_pointer< _Tp >::type add_pointer_t
Alias template for add_pointer.
typename make_signed< _Tp >::type make_signed_t
Alias template for make_signed.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
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...
ISO C++ entities toplevel namespace is std.
constexpr bool is_swappable_v
is_swappable_v
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
typename invoke_result< _Fn, _Args... >::type invoke_result_t
std::invoke_result_t
constexpr bool is_nothrow_invocable_v
std::is_nothrow_invocable_v
Primary class template hash.
Class template integer_sequence.