// -*- C++ -*-
// Copyright (C) 2016-2021 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// .
/** @file variant
* This is the `` C++ Library header.
*/
#ifndef _GLIBCXX_VARIANT
#define _GLIBCXX_VARIANT 1
#pragma GCC system_header
#if __cplusplus >= 201703L
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if __cplusplus > 201703L
# include
#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace __detail
{
namespace __variant
{
template
struct _Nth_type;
template
struct _Nth_type<_Np, _First, _Rest...>
: _Nth_type<_Np-1, _Rest...> { };
template
struct _Nth_type<0, _First, _Rest...>
{ using type = _First; };
} // namespace __variant
} // namespace __detail
#define __cpp_lib_variant 202102L
template class tuple;
template class variant;
template struct hash;
template
struct variant_size;
template
struct variant_size : variant_size<_Variant> {};
template
struct variant_size : variant_size<_Variant> {};
template
struct variant_size : variant_size<_Variant> {};
template
struct variant_size>
: std::integral_constant {};
template
inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
template
struct variant_alternative;
template
struct variant_alternative<_Np, variant<_First, _Rest...>>
: variant_alternative<_Np-1, variant<_Rest...>> {};
template
struct variant_alternative<0, variant<_First, _Rest...>>
{ using type = _First; };
template
using variant_alternative_t =
typename variant_alternative<_Np, _Variant>::type;
template
struct variant_alternative<_Np, const _Variant>
{ using type = add_const_t>; };
template
struct variant_alternative<_Np, volatile _Variant>
{ using type = add_volatile_t>; };
template
struct variant_alternative<_Np, const volatile _Variant>
{ using type = add_cv_t>; };
inline constexpr size_t variant_npos = -1;
template
constexpr variant_alternative_t<_Np, variant<_Types...>>&
get(variant<_Types...>&);
template
constexpr variant_alternative_t<_Np, variant<_Types...>>&&
get(variant<_Types...>&&);
template
constexpr variant_alternative_t<_Np, variant<_Types...>> const&
get(const variant<_Types...>&);
template
constexpr variant_alternative_t<_Np, variant<_Types...>> const&&
get(const variant<_Types...>&&);
template
constexpr decltype(auto)
__do_visit(_Visitor&& __visitor, _Variants&&... __variants);
template
decltype(auto)
__variant_cast(_Tp&& __rhs)
{
if constexpr (is_lvalue_reference_v<_Tp>)
{
if constexpr (is_const_v>)
return static_cast&>(__rhs);
else
return static_cast&>(__rhs);
}
else
return static_cast&&>(__rhs);
}
namespace __detail
{
namespace __variant
{
// Returns the first appearance of _Tp in _Types.
// Returns sizeof...(_Types) if _Tp is not in _Types.
template
struct __index_of : std::integral_constant {};
template
inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value;
template
struct __index_of<_Tp, _First, _Rest...> :
std::integral_constant
? 0 : __index_of_v<_Tp, _Rest...> + 1> {};
// used for raw visitation
struct __variant_cookie {};
// used for raw visitation with indices passed in
struct __variant_idx_cookie { using type = __variant_idx_cookie; };
// Used to enable deduction (and same-type checking) for std::visit:
template struct __deduce_visit_result { using type = _Tp; };
// Visit variants that might be valueless.
template
constexpr void
__raw_visit(_Visitor&& __visitor, _Variants&&... __variants)
{
std::__do_visit<__variant_cookie>(std::forward<_Visitor>(__visitor),
std::forward<_Variants>(__variants)...);
}
// Visit variants that might be valueless, passing indices to the visitor.
template
constexpr void
__raw_idx_visit(_Visitor&& __visitor, _Variants&&... __variants)
{
std::__do_visit<__variant_idx_cookie>(std::forward<_Visitor>(__visitor),
std::forward<_Variants>(__variants)...);
}
// The __as function templates implement the exposition-only "as-variant"
template
constexpr std::variant<_Types...>&
__as(std::variant<_Types...>& __v) noexcept
{ return __v; }
template
constexpr const std::variant<_Types...>&
__as(const std::variant<_Types...>& __v) noexcept
{ return __v; }
template
constexpr std::variant<_Types...>&&
__as(std::variant<_Types...>&& __v) noexcept
{ return std::move(__v); }
template
constexpr const std::variant<_Types...>&&
__as(const std::variant<_Types...>&& __v) noexcept
{ return std::move(__v); }
// _Uninitialized is guaranteed to be a trivially destructible type,
// even if T is not.
template>
struct _Uninitialized;
template
struct _Uninitialized<_Type, true>
{
template
constexpr
_Uninitialized(in_place_index_t<0>, _Args&&... __args)
: _M_storage(std::forward<_Args>(__args)...)
{ }
constexpr const _Type& _M_get() const & noexcept
{ return _M_storage; }
constexpr _Type& _M_get() & noexcept
{ return _M_storage; }
constexpr const _Type&& _M_get() const && noexcept
{ return std::move(_M_storage); }
constexpr _Type&& _M_get() && noexcept
{ return std::move(_M_storage); }
_Type _M_storage;
};
template
struct _Uninitialized<_Type, false>
{
template
constexpr
_Uninitialized(in_place_index_t<0>, _Args&&... __args)
{
::new ((void*)std::addressof(_M_storage))
_Type(std::forward<_Args>(__args)...);
}
const _Type& _M_get() const & noexcept
{ return *_M_storage._M_ptr(); }
_Type& _M_get() & noexcept
{ return *_M_storage._M_ptr(); }
const _Type&& _M_get() const && noexcept
{ return std::move(*_M_storage._M_ptr()); }
_Type&& _M_get() && noexcept
{ return std::move(*_M_storage._M_ptr()); }
__gnu_cxx::__aligned_membuf<_Type> _M_storage;
};
template
constexpr decltype(auto)
__get(in_place_index_t<0>, _Union&& __u) noexcept
{ return std::forward<_Union>(__u)._M_first._M_get(); }
template
constexpr decltype(auto)
__get(in_place_index_t<_Np>, _Union&& __u) noexcept
{
return __variant::__get(in_place_index<_Np-1>,
std::forward<_Union>(__u)._M_rest);
}
// Returns the typed storage for __v.
template
constexpr decltype(auto)
__get(_Variant&& __v) noexcept
{
return __variant::__get(std::in_place_index<_Np>,
std::forward<_Variant>(__v)._M_u);
}
template
struct _Traits
{
static constexpr bool _S_default_ctor =
is_default_constructible_v::type>;
static constexpr bool _S_copy_ctor =
(is_copy_constructible_v<_Types> && ...);
static constexpr bool _S_move_ctor =
(is_move_constructible_v<_Types> && ...);
static constexpr bool _S_copy_assign =
_S_copy_ctor
&& (is_copy_assignable_v<_Types> && ...);
static constexpr bool _S_move_assign =
_S_move_ctor
&& (is_move_assignable_v<_Types> && ...);
static constexpr bool _S_trivial_dtor =
(is_trivially_destructible_v<_Types> && ...);
static constexpr bool _S_trivial_copy_ctor =
(is_trivially_copy_constructible_v<_Types> && ...);
static constexpr bool _S_trivial_move_ctor =
(is_trivially_move_constructible_v<_Types> && ...);
static constexpr bool _S_trivial_copy_assign =
_S_trivial_dtor && _S_trivial_copy_ctor
&& (is_trivially_copy_assignable_v<_Types> && ...);
static constexpr bool _S_trivial_move_assign =
_S_trivial_dtor && _S_trivial_move_ctor
&& (is_trivially_move_assignable_v<_Types> && ...);
// The following nothrow traits are for non-trivial SMFs. Trivial SMFs
// are always nothrow.
static constexpr bool _S_nothrow_default_ctor =
is_nothrow_default_constructible_v<
typename _Nth_type<0, _Types...>::type>;
static constexpr bool _S_nothrow_copy_ctor = false;
static constexpr bool _S_nothrow_move_ctor =
(is_nothrow_move_constructible_v<_Types> && ...);
static constexpr bool _S_nothrow_copy_assign = false;
static constexpr bool _S_nothrow_move_assign =
_S_nothrow_move_ctor
&& (is_nothrow_move_assignable_v<_Types> && ...);
};
// Defines members and ctors.
template
union _Variadic_union { };
template
union _Variadic_union<_First, _Rest...>
{
constexpr _Variadic_union() : _M_rest() { }
template
constexpr _Variadic_union(in_place_index_t<0>, _Args&&... __args)
: _M_first(in_place_index<0>, std::forward<_Args>(__args)...)
{ }
template
constexpr _Variadic_union(in_place_index_t<_Np>, _Args&&... __args)
: _M_rest(in_place_index<_Np-1>, std::forward<_Args>(__args)...)
{ }
_Uninitialized<_First> _M_first;
_Variadic_union<_Rest...> _M_rest;
};
// _Never_valueless_alt is true for variant alternatives that can
// always be placed in a variant without it becoming valueless.
// For suitably-small, trivially copyable types we can create temporaries
// on the stack and then memcpy them into place.
template
struct _Never_valueless_alt
: __and_, is_trivially_copyable<_Tp>>
{ };
// Specialize _Never_valueless_alt for other types which have a
// non-throwing and cheap move construction and move assignment operator,
// so that emplacing the type will provide the strong exception-safety
// guarantee, by creating and moving a temporary.
// Whether _Never_valueless_alt is true or not affects the ABI of a
// variant using that alternative, so we can't change the value later!
// True if every alternative in _Types... can be emplaced in a variant
// without it becoming valueless. If this is true, variant<_Types...>
// can never be valueless, which enables some minor optimizations.
template
constexpr bool __never_valueless()
{
return _Traits<_Types...>::_S_move_assign
&& (_Never_valueless_alt<_Types>::value && ...);
}
// Defines index and the dtor, possibly trivial.
template
struct _Variant_storage;
template
using __select_index =
typename __select_int::_Select_int_base::type::value_type;
template
struct _Variant_storage
{
constexpr
_Variant_storage()
: _M_index(static_cast<__index_type>(variant_npos))
{ }
template
constexpr
_Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
_M_index{_Np}
{ }
void _M_reset()
{
if (!_M_valid()) [[unlikely]]
return;
std::__do_visit([](auto&& __this_mem) mutable
{
std::_Destroy(std::__addressof(__this_mem));
}, __variant_cast<_Types...>(*this));
_M_index = static_cast<__index_type>(variant_npos);
}
~_Variant_storage()
{ _M_reset(); }
constexpr bool
_M_valid() const noexcept
{
if constexpr (__variant::__never_valueless<_Types...>())
return true;
return this->_M_index != __index_type(variant_npos);
}
_Variadic_union<_Types...> _M_u;
using __index_type = __select_index<_Types...>;
__index_type _M_index;
};
template
struct _Variant_storage
{
constexpr
_Variant_storage()
: _M_index(static_cast<__index_type>(variant_npos))
{ }
template
constexpr
_Variant_storage(in_place_index_t<_Np>, _Args&&... __args)
: _M_u(in_place_index<_Np>, std::forward<_Args>(__args)...),
_M_index{_Np}
{ }
void _M_reset() noexcept
{ _M_index = static_cast<__index_type>(variant_npos); }
constexpr bool
_M_valid() const noexcept
{
if constexpr (__variant::__never_valueless<_Types...>())
return true;
return this->_M_index != static_cast<__index_type>(variant_npos);
}
_Variadic_union<_Types...> _M_u;
using __index_type = __select_index<_Types...>;
__index_type _M_index;
};
template
using _Variant_storage_alias =
_Variant_storage<_Traits<_Types...>::_S_trivial_dtor, _Types...>;
template
void __variant_construct_single(_Tp&& __lhs, _Up&& __rhs_mem)
{
void* __storage = std::addressof(__lhs._M_u);
using _Type = remove_reference_t;
if constexpr (!is_same_v<_Type, __variant_cookie>)
::new (__storage)
_Type(std::forward(__rhs_mem));
}
template
void __variant_construct(_Tp&& __lhs, _Up&& __rhs)
{
__lhs._M_index = __rhs._M_index;
__variant::__raw_visit([&__lhs](auto&& __rhs_mem) mutable
{
__variant_construct_single(std::forward<_Tp>(__lhs),
std::forward(__rhs_mem));
}, __variant_cast<_Types...>(std::forward<_Up>(__rhs)));
}
// The following are (Copy|Move) (ctor|assign) layers for forwarding
// triviality and handling non-trivial SMF behaviors.
template
struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
{
using _Base = _Variant_storage_alias<_Types...>;
using _Base::_Base;
_Copy_ctor_base(const _Copy_ctor_base& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_copy_ctor)
{
__variant_construct<_Types...>(*this, __rhs);
}
_Copy_ctor_base(_Copy_ctor_base&&) = default;
_Copy_ctor_base& operator=(const _Copy_ctor_base&) = default;
_Copy_ctor_base& operator=(_Copy_ctor_base&&) = default;
};
template
struct _Copy_ctor_base : _Variant_storage_alias<_Types...>
{
using _Base = _Variant_storage_alias<_Types...>;
using _Base::_Base;
};
template
using _Copy_ctor_alias =
_Copy_ctor_base<_Traits<_Types...>::_S_trivial_copy_ctor, _Types...>;
template
struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
{
using _Base = _Copy_ctor_alias<_Types...>;
using _Base::_Base;
_Move_ctor_base(_Move_ctor_base&& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_move_ctor)
{
__variant_construct<_Types...>(*this, std::move(__rhs));
}
template
void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, std::forward<_Up>(__rhs));
this->_M_index = __rhs_index;
}
template
void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, __rhs);
this->_M_index = __rhs_index;
}
_Move_ctor_base(const _Move_ctor_base&) = default;
_Move_ctor_base& operator=(const _Move_ctor_base&) = default;
_Move_ctor_base& operator=(_Move_ctor_base&&) = default;
};
template
struct _Move_ctor_base : _Copy_ctor_alias<_Types...>
{
using _Base = _Copy_ctor_alias<_Types...>;
using _Base::_Base;
template
void _M_destructive_move(unsigned short __rhs_index, _Up&& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, std::forward<_Up>(__rhs));
this->_M_index = __rhs_index;
}
template
void _M_destructive_copy(unsigned short __rhs_index, const _Up& __rhs)
{
this->_M_reset();
__variant_construct_single(*this, __rhs);
this->_M_index = __rhs_index;
}
};
template
using _Move_ctor_alias =
_Move_ctor_base<_Traits<_Types...>::_S_trivial_move_ctor, _Types...>;
template
struct _Copy_assign_base : _Move_ctor_alias<_Types...>
{
using _Base = _Move_ctor_alias<_Types...>;
using _Base::_Base;
_Copy_assign_base&
operator=(const _Copy_assign_base& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_copy_assign)
{
__variant::__raw_idx_visit(
[this](auto&& __rhs_mem, auto __rhs_index) mutable
{
if constexpr (__rhs_index != variant_npos)
{
if (this->_M_index == __rhs_index)
__variant::__get<__rhs_index>(*this) = __rhs_mem;
else
{
using __rhs_type = __remove_cvref_t;
if constexpr (is_nothrow_copy_constructible_v<__rhs_type>
|| !is_nothrow_move_constructible_v<__rhs_type>)
// The standard says this->emplace<__rhs_type>(__rhs_mem)
// should be used here, but _M_destructive_copy is
// equivalent in this case. Either copy construction
// doesn't throw, so _M_destructive_copy gives strong
// exception safety guarantee, or both copy construction
// and move construction can throw, so emplace only gives
// basic exception safety anyway.
this->_M_destructive_copy(__rhs_index, __rhs_mem);
else
__variant_cast<_Types...>(*this)
= variant<_Types...>(std::in_place_index<__rhs_index>,
__rhs_mem);
}
}
else
this->_M_reset();
}, __variant_cast<_Types...>(__rhs));
return *this;
}
_Copy_assign_base(const _Copy_assign_base&) = default;
_Copy_assign_base(_Copy_assign_base&&) = default;
_Copy_assign_base& operator=(_Copy_assign_base&&) = default;
};
template
struct _Copy_assign_base : _Move_ctor_alias<_Types...>
{
using _Base = _Move_ctor_alias<_Types...>;
using _Base::_Base;
};
template
using _Copy_assign_alias =
_Copy_assign_base<_Traits<_Types...>::_S_trivial_copy_assign, _Types...>;
template
struct _Move_assign_base : _Copy_assign_alias<_Types...>
{
using _Base = _Copy_assign_alias<_Types...>;
using _Base::_Base;
_Move_assign_base&
operator=(_Move_assign_base&& __rhs)
noexcept(_Traits<_Types...>::_S_nothrow_move_assign)
{
__variant::__raw_idx_visit(
[this](auto&& __rhs_mem, auto __rhs_index) mutable
{
if constexpr (__rhs_index != variant_npos)
{
if (this->_M_index == __rhs_index)
__variant::__get<__rhs_index>(*this) = std::move(__rhs_mem);
else
__variant_cast<_Types...>(*this)
.template emplace<__rhs_index>(std::move(__rhs_mem));
}
else
this->_M_reset();
}, __variant_cast<_Types...>(__rhs));
return *this;
}
_Move_assign_base(const _Move_assign_base&) = default;
_Move_assign_base(_Move_assign_base&&) = default;
_Move_assign_base& operator=(const _Move_assign_base&) = default;
};
template
struct _Move_assign_base : _Copy_assign_alias<_Types...>
{
using _Base = _Copy_assign_alias<_Types...>;
using _Base::_Base;
};
template
using _Move_assign_alias =
_Move_assign_base<_Traits<_Types...>::_S_trivial_move_assign, _Types...>;
template
struct _Variant_base : _Move_assign_alias<_Types...>
{
using _Base = _Move_assign_alias<_Types...>;
constexpr
_Variant_base()
noexcept(_Traits<_Types...>::_S_nothrow_default_ctor)
: _Variant_base(in_place_index<0>) { }
template
constexpr explicit
_Variant_base(in_place_index_t<_Np> __i, _Args&&... __args)
: _Base(__i, std::forward<_Args>(__args)...)
{ }
_Variant_base(const _Variant_base&) = default;
_Variant_base(_Variant_base&&) = default;
_Variant_base& operator=(const _Variant_base&) = default;
_Variant_base& operator=(_Variant_base&&) = default;
};
// For how many times does _Tp appear in _Tuple?
template
struct __tuple_count;
template
inline constexpr size_t __tuple_count_v =
__tuple_count<_Tp, _Tuple>::value;
template
struct __tuple_count<_Tp, tuple<_Types...>>
: integral_constant { };
template
struct __tuple_count<_Tp, tuple<_First, _Rest...>>
: integral_constant<
size_t,
__tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
// TODO: Reuse this in ?
template
inline constexpr bool __exactly_once =
__tuple_count_v<_Tp, tuple<_Types...>> == 1;
// Helper used to check for valid conversions that don't involve narrowing.
template struct _Arr { _Ti _M_x[1]; };
// "Build an imaginary function FUN(Ti) for each alternative type Ti"
template
struct _Build_FUN
{
// This function means 'using _Build_FUN::_S_fun;' is valid,
// but only static functions will be considered in the call below.
void _S_fun();
};
// "... for which Ti x[] = {std::forward(t)}; is well-formed."
template
struct _Build_FUN<_Ind, _Tp, _Ti,
void_t{{std::declval<_Tp>()}})>>
{
// This is the FUN function for type _Ti, with index _Ind
static integral_constant _S_fun(_Ti);
};
template>>
struct _Build_FUNs;
template
struct _Build_FUNs<_Tp, variant<_Ti...>, index_sequence<_Ind...>>
: _Build_FUN<_Ind, _Tp, _Ti>...
{
using _Build_FUN<_Ind, _Tp, _Ti>::_S_fun...;
};
// The index j of the overload FUN(Tj) selected by overload resolution
// for FUN(std::forward<_Tp>(t))
template
using _FUN_type
= decltype(_Build_FUNs<_Tp, _Variant>::_S_fun(std::declval<_Tp>()));
// The index selected for FUN(std::forward(t)), or variant_npos if none.
template
struct __accepted_index
: integral_constant
{ };
template
struct __accepted_index<_Tp, _Variant, void_t<_FUN_type<_Tp, _Variant>>>
: _FUN_type<_Tp, _Variant>
{ };
template
struct _Extra_visit_slot_needed
{
template struct _Variant_never_valueless;
template
struct _Variant_never_valueless>
: bool_constant<__variant::__never_valueless<_Types...>()> {};
static constexpr bool value =
(is_same_v<_Maybe_variant_cookie, __variant_cookie>
|| is_same_v<_Maybe_variant_cookie, __variant_idx_cookie>)
&& !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value;
};
// Used for storing a multi-dimensional vtable.
template
struct _Multi_array;
// Partial specialization with rank zero, stores a single _Tp element.
template
struct _Multi_array<_Tp>
{
template
struct __untag_result
: false_type
{ using element_type = _Tp; };
template
struct __untag_result
: false_type
{ using element_type = void(*)(_Args...); };
template
struct __untag_result<__variant_cookie(*)(_Args...)>
: false_type
{ using element_type = void(*)(_Args...); };
template
struct __untag_result<__variant_idx_cookie(*)(_Args...)>
: false_type
{ using element_type = void(*)(_Args...); };
template
struct __untag_result<__deduce_visit_result<_Res>(*)(_Args...)>
: true_type
{ using element_type = _Res(*)(_Args...); };
using __result_is_deduced = __untag_result<_Tp>;
constexpr const typename __untag_result<_Tp>::element_type&
_M_access() const
{ return _M_data; }
typename __untag_result<_Tp>::element_type _M_data;
};
// Partial specialization with rank >= 1.
template
struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...>
{
static constexpr size_t __index =
sizeof...(_Variants) - sizeof...(__rest) - 1;
using _Variant = typename _Nth_type<__index, _Variants...>::type;
static constexpr int __do_cookie =
_Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0;
using _Tp = _Ret(*)(_Visitor, _Variants...);
template
constexpr decltype(auto)
_M_access(size_t __first_index, _Args... __rest_indices) const
{
return _M_arr[__first_index + __do_cookie]
._M_access(__rest_indices...);
}
_Multi_array<_Tp, __rest...> _M_arr[__first + __do_cookie];
};
// Creates a multi-dimensional vtable recursively.
//
// For example,
// visit([](auto, auto){},
// variant(), // typedef'ed as V1
// variant()) // typedef'ed as V2
// will trigger instantiations of:
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<0>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<0, 0>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<0, 1>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<0, 2>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<1>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<1, 0>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<1, 1>>
// __gen_vtable_impl<_Multi_array,
// tuple, std::index_sequence<1, 2>>
// The returned multi-dimensional vtable can be fast accessed by the visitor
// using index calculation.
template
struct __gen_vtable_impl;
// Defines the _S_apply() member that returns a _Multi_array populated
// with function pointers that perform the visitation expressions e(m)
// for each valid pack of indexes into the variant types _Variants.
//
// This partial specialization builds up the index sequences by recursively
// calling _S_apply() on the next specialization of __gen_vtable_impl.
// The base case of the recursion defines the actual function pointers.
template
struct __gen_vtable_impl<
_Multi_array<_Result_type (*)(_Visitor, _Variants...), __dimensions...>,
std::index_sequence<__indices...>>
{
using _Next =
remove_reference_t::type>;
using _Array_type =
_Multi_array<_Result_type (*)(_Visitor, _Variants...),
__dimensions...>;
static constexpr _Array_type
_S_apply()
{
_Array_type __vtable{};
_S_apply_all_alts(
__vtable, make_index_sequence>());
return __vtable;
}
template
static constexpr void
_S_apply_all_alts(_Array_type& __vtable,
std::index_sequence<__var_indices...>)
{
if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value)
(_S_apply_single_alt(
__vtable._M_arr[__var_indices + 1],
&(__vtable._M_arr[0])), ...);
else
(_S_apply_single_alt(
__vtable._M_arr[__var_indices]), ...);
}
template
static constexpr void
_S_apply_single_alt(_Tp& __element, _Tp* __cookie_element = nullptr)
{
if constexpr (__do_cookie)
{
__element = __gen_vtable_impl<
_Tp,
std::index_sequence<__indices..., __index>>::_S_apply();
*__cookie_element = __gen_vtable_impl<
_Tp,
std::index_sequence<__indices..., variant_npos>>::_S_apply();
}
else
{
auto __tmp_element = __gen_vtable_impl<
remove_reference_t,
std::index_sequence<__indices..., __index>>::_S_apply();
static_assert(is_same_v<_Tp, decltype(__tmp_element)>,
"std::visit requires the visitor to have the same "
"return type for all alternatives of a variant");
__element = __tmp_element;
}
}
};
// This partial specialization is the base case for the recursion.
// It populates a _Multi_array element with the address of a function
// that invokes the visitor with the alternatives specified by __indices.
template
struct __gen_vtable_impl<
_Multi_array<_Result_type (*)(_Visitor, _Variants...)>,
std::index_sequence<__indices...>>
{
using _Array_type =
_Multi_array<_Result_type (*)(_Visitor, _Variants...)>;
template
static constexpr decltype(auto)
__element_by_index_or_cookie(_Variant&& __var) noexcept
{
if constexpr (__index != variant_npos)
return __variant::__get<__index>(std::forward<_Variant>(__var));
else
return __variant_cookie{};
}
static constexpr decltype(auto)
__visit_invoke(_Visitor&& __visitor, _Variants... __vars)
{
if constexpr (is_same_v<_Result_type, __variant_idx_cookie>)
// For raw visitation using indices, pass the indices to the visitor
// and discard the return value:
std::__invoke(std::forward<_Visitor>(__visitor),
__element_by_index_or_cookie<__indices>(
std::forward<_Variants>(__vars))...,
integral_constant()...);
else if constexpr (is_same_v<_Result_type, __variant_cookie>)
// For raw visitation without indices, and discard the return value:
std::__invoke(std::forward<_Visitor>(__visitor),
__element_by_index_or_cookie<__indices>(
std::forward<_Variants>(__vars))...);
else if constexpr (_Array_type::__result_is_deduced::value)
// For the usual std::visit case deduce the return value:
return std::__invoke(std::forward<_Visitor>(__visitor),
__element_by_index_or_cookie<__indices>(
std::forward<_Variants>(__vars))...);
else // for std::visit use INVOKE
return std::__invoke_r<_Result_type>(
std::forward<_Visitor>(__visitor),
__variant::__get<__indices>(std::forward<_Variants>(__vars))...);
}
static constexpr auto
_S_apply()
{
if constexpr (_Array_type::__result_is_deduced::value)
{
constexpr bool __visit_ret_type_mismatch =
!is_same_v(),
std::declval<_Variants>()...))>;
if constexpr (__visit_ret_type_mismatch)
{
struct __cannot_match {};
return __cannot_match{};
}
else
return _Array_type{&__visit_invoke};
}
else
return _Array_type{&__visit_invoke};
}
};
template
struct __gen_vtable
{
using _Array_type =
_Multi_array<_Result_type (*)(_Visitor, _Variants...),
variant_size_v>...>;
static constexpr _Array_type _S_vtable
= __gen_vtable_impl<_Array_type, std::index_sequence<>>::_S_apply();
};
template
struct _Base_dedup : public _Tp { };
template
struct _Variant_hash_base;
template
struct _Variant_hash_base,
std::index_sequence<__indices...>>
: _Base_dedup<__indices, __poison_hash>>... { };
// Equivalent to decltype(get<_Np>(as-variant(declval<_Variant>())))
template())),
typename _Tp = variant_alternative_t<_Np, remove_reference_t<_AsV>>>
using __get_t
= conditional_t, _Tp&, _Tp&&>;
// Return type of std::visit.
template
using __visit_result_t
= invoke_result_t<_Visitor, __get_t<0, _Variants>...>;
template
constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
template
constexpr bool __check_visitor_results(std::index_sequence<_Idxs...>)
{
return __same_types<
invoke_result_t<_Visitor, __get_t<_Idxs, _Variant>>...
>;
}
template
inline void
__construct_by_index(_Variant& __v, _Args&&... __args)
{
auto&& __storage = __detail::__variant::__get<_Np>(__v);
::new ((void*)std::addressof(__storage))
remove_reference_t
(std::forward<_Args>(__args)...);
// Construction didn't throw, so can set the new index now:
__v._M_index = _Np;
}
} // namespace __variant
} // namespace __detail
template
constexpr bool
holds_alternative(const variant<_Types...>& __v) noexcept
{
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
"T must occur exactly once in alternatives");
return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>;
}
template
constexpr _Tp& get(variant<_Types...>& __v)
{
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
"T must occur exactly once in alternatives");
static_assert(!is_void_v<_Tp>, "_Tp must not be void");
return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
}
template
constexpr _Tp&& get(variant<_Types...>&& __v)
{
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
"T must occur exactly once in alternatives");
static_assert(!is_void_v<_Tp>, "_Tp must not be void");
return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
std::move(__v));
}
template
constexpr const _Tp& get(const variant<_Types...>& __v)
{
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
"T must occur exactly once in alternatives");
static_assert(!is_void_v<_Tp>, "_Tp must not be void");
return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v);
}
template
constexpr const _Tp&& get(const variant<_Types...>&& __v)
{
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
"T must occur exactly once in alternatives");
static_assert(!is_void_v<_Tp>, "_Tp must not be void");
return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(
std::move(__v));
}
template
constexpr add_pointer_t>>
get_if(variant<_Types...>* __ptr) noexcept
{
using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
static_assert(_Np < sizeof...(_Types),
"The index must be in [0, number of alternatives)");
static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
if (__ptr && __ptr->index() == _Np)
return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
return nullptr;
}
template
constexpr
add_pointer_t>>
get_if(const variant<_Types...>* __ptr) noexcept
{
using _Alternative_type = variant_alternative_t<_Np, variant<_Types...>>;
static_assert(_Np < sizeof...(_Types),
"The index must be in [0, number of alternatives)");
static_assert(!is_void_v<_Alternative_type>, "_Tp must not be void");
if (__ptr && __ptr->index() == _Np)
return std::addressof(__detail::__variant::__get<_Np>(*__ptr));
return nullptr;
}
template
constexpr add_pointer_t<_Tp>
get_if(variant<_Types...>* __ptr) noexcept
{
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
"T must occur exactly once in alternatives");
static_assert(!is_void_v<_Tp>, "_Tp must not be void");
return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
__ptr);
}
template
constexpr add_pointer_t
get_if(const variant<_Types...>* __ptr) noexcept
{
static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>,
"T must occur exactly once in alternatives");
static_assert(!is_void_v<_Tp>, "_Tp must not be void");
return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>(
__ptr);
}
struct monostate { };
#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP, __NAME) \
template \
constexpr bool operator __OP(const variant<_Types...>& __lhs, \
const variant<_Types...>& __rhs) \
{ \
bool __ret = true; \
__detail::__variant::__raw_idx_visit( \
[&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \
{ \
if constexpr (__rhs_index != variant_npos) \
{ \
if (__lhs.index() == __rhs_index) \
{ \
auto& __this_mem = std::get<__rhs_index>(__lhs); \
__ret = __this_mem __OP __rhs_mem; \
} \
else \
__ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
} \
else \
__ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \
}, __rhs); \
return __ret; \
}
_VARIANT_RELATION_FUNCTION_TEMPLATE(<, less)
_VARIANT_RELATION_FUNCTION_TEMPLATE(<=, less_equal)
_VARIANT_RELATION_FUNCTION_TEMPLATE(==, equal)
_VARIANT_RELATION_FUNCTION_TEMPLATE(!=, not_equal)
_VARIANT_RELATION_FUNCTION_TEMPLATE(>=, greater_equal)
_VARIANT_RELATION_FUNCTION_TEMPLATE(>, greater)
#undef _VARIANT_RELATION_FUNCTION_TEMPLATE
constexpr bool operator==(monostate, monostate) noexcept { return true; }
#ifdef __cpp_lib_three_way_comparison
template
requires (three_way_comparable<_Types> && ...)
constexpr
common_comparison_category_t...>
operator<=>(const variant<_Types...>& __v, const variant<_Types...>& __w)
{
common_comparison_category_t...> __ret
= strong_ordering::equal;
__detail::__variant::__raw_idx_visit(
[&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable
{
if constexpr (__w_index != variant_npos)
{
if (__v.index() == __w_index)
{
auto& __this_mem = std::get<__w_index>(__v);
__ret = __this_mem <=> __w_mem;
return;
}
}
__ret = (__v.index() + 1) <=> (__w_index + 1);
}, __w);
return __ret;
}
constexpr strong_ordering
operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
#else
constexpr bool operator!=(monostate, monostate) noexcept { return false; }
constexpr bool operator<(monostate, monostate) noexcept { return false; }
constexpr bool operator>(monostate, monostate) noexcept { return false; }
constexpr bool operator<=(monostate, monostate) noexcept { return true; }
constexpr bool operator>=(monostate, monostate) noexcept { return true; }
#endif
template
constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...>
visit(_Visitor&&, _Variants&&...);
template
inline enable_if_t<(is_move_constructible_v<_Types> && ...)
&& (is_swappable_v<_Types> && ...)>
swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
noexcept(noexcept(__lhs.swap(__rhs)))
{ __lhs.swap(__rhs); }
template
enable_if_t && ...)
&& (is_swappable_v<_Types> && ...))>
swap(variant<_Types...>&, variant<_Types...>&) = delete;
class bad_variant_access : public exception
{
public:
bad_variant_access() noexcept { }
const char* what() const noexcept override
{ return _M_reason; }
private:
bad_variant_access(const char* __reason) noexcept : _M_reason(__reason) { }
// Must point to a string with static storage duration:
const char* _M_reason = "bad variant access";
friend void __throw_bad_variant_access(const char* __what);
};
// Must only be called with a string literal
inline void
__throw_bad_variant_access(const char* __what)
{ _GLIBCXX_THROW_OR_ABORT(bad_variant_access(__what)); }
inline void
__throw_bad_variant_access(bool __valueless)
{
if (__valueless) [[__unlikely__]]
__throw_bad_variant_access("std::get: variant is valueless");
else
__throw_bad_variant_access("std::get: wrong index for variant");
}
template
class variant
: private __detail::__variant::_Variant_base<_Types...>,
private _Enable_default_constructor<
__detail::__variant::_Traits<_Types...>::_S_default_ctor,
variant<_Types...>>,
private _Enable_copy_move<
__detail::__variant::_Traits<_Types...>::_S_copy_ctor,
__detail::__variant::_Traits<_Types...>::_S_copy_assign,
__detail::__variant::_Traits<_Types...>::_S_move_ctor,
__detail::__variant::_Traits<_Types...>::_S_move_assign,
variant<_Types...>>
{
private:
template
friend decltype(auto) __variant_cast(_Tp&&);
template
friend void
__detail::__variant::__construct_by_index(_Variant& __v,
_Args&&... __args);
static_assert(sizeof...(_Types) > 0,
"variant must have at least one alternative");
static_assert(!(std::is_reference_v<_Types> || ...),
"variant must have no reference alternative");
static_assert(!(std::is_void_v<_Types> || ...),
"variant must have no void alternative");
using _Base = __detail::__variant::_Variant_base<_Types...>;
using _Default_ctor_enabler =
_Enable_default_constructor<
__detail::__variant::_Traits<_Types...>::_S_default_ctor,
variant<_Types...>>;
template
static constexpr bool __not_self
= !is_same_v<__remove_cvref_t<_Tp>, variant>;
template
static constexpr bool
__exactly_once = __detail::__variant::__exactly_once<_Tp, _Types...>;
template
static constexpr size_t __accepted_index
= __detail::__variant::__accepted_index<_Tp, variant>::value;
template>
using __to_type = variant_alternative_t<_Np, variant>;
template>>
using __accepted_type = __to_type<__accepted_index<_Tp>>;
template
static constexpr size_t __index_of =
__detail::__variant::__index_of_v<_Tp, _Types...>;
using _Traits = __detail::__variant::_Traits<_Types...>;
template
struct __is_in_place_tag : false_type { };
template
struct __is_in_place_tag> : true_type { };
template
struct __is_in_place_tag> : true_type { };
template
static constexpr bool __not_in_place_tag
= !__is_in_place_tag<__remove_cvref_t<_Tp>>::value;
public:
variant() = default;
variant(const variant& __rhs) = default;
variant(variant&&) = default;
variant& operator=(const variant&) = default;
variant& operator=(variant&&) = default;
~variant() = default;
template,
typename = enable_if_t<__not_in_place_tag<_Tp>>,
typename _Tj = __accepted_type<_Tp&&>,
typename = enable_if_t<__exactly_once<_Tj>
&& is_constructible_v<_Tj, _Tp>>>
constexpr
variant(_Tp&& __t)
noexcept(is_nothrow_constructible_v<_Tj, _Tp>)
: variant(in_place_index<__accepted_index<_Tp>>,
std::forward<_Tp>(__t))
{ }
template
&& is_constructible_v<_Tp, _Args...>>>
constexpr explicit
variant(in_place_type_t<_Tp>, _Args&&... __args)
: variant(in_place_index<__index_of<_Tp>>,
std::forward<_Args>(__args)...)
{ }
template
&& is_constructible_v<_Tp,
initializer_list<_Up>&, _Args...>>>
constexpr explicit
variant(in_place_type_t<_Tp>, initializer_list<_Up> __il,
_Args&&... __args)
: variant(in_place_index<__index_of<_Tp>>, __il,
std::forward<_Args>(__args)...)
{ }
template,
typename = enable_if_t>>
constexpr explicit
variant(in_place_index_t<_Np>, _Args&&... __args)
: _Base(in_place_index<_Np>, std::forward<_Args>(__args)...),
_Default_ctor_enabler(_Enable_default_constructor_tag{})
{ }
template,
typename = enable_if_t&,
_Args...>>>
constexpr explicit
variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
_Args&&... __args)
: _Base(in_place_index<_Np>, __il, std::forward<_Args>(__args)...),
_Default_ctor_enabler(_Enable_default_constructor_tag{})
{ }
template
enable_if_t<__exactly_once<__accepted_type<_Tp&&>>
&& is_constructible_v<__accepted_type<_Tp&&>, _Tp>
&& is_assignable_v<__accepted_type<_Tp&&>&, _Tp>,
variant&>
operator=(_Tp&& __rhs)
noexcept(is_nothrow_assignable_v<__accepted_type<_Tp&&>&, _Tp>
&& is_nothrow_constructible_v<__accepted_type<_Tp&&>, _Tp>)
{
constexpr auto __index = __accepted_index<_Tp>;
if (index() == __index)
std::get<__index>(*this) = std::forward<_Tp>(__rhs);
else
{
using _Tj = __accepted_type<_Tp&&>;
if constexpr (is_nothrow_constructible_v<_Tj, _Tp>
|| !is_nothrow_move_constructible_v<_Tj>)
this->emplace<__index>(std::forward<_Tp>(__rhs));
else
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3585. converting assignment with immovable alternative
this->emplace<__index>(_Tj(std::forward<_Tp>(__rhs)));
}
return *this;
}
template
enable_if_t && __exactly_once<_Tp>,
_Tp&>
emplace(_Args&&... __args)
{
constexpr size_t __index = __index_of<_Tp>;
return this->emplace<__index>(std::forward<_Args>(__args)...);
}
template
enable_if_t&, _Args...>
&& __exactly_once<_Tp>,
_Tp&>
emplace(initializer_list<_Up> __il, _Args&&... __args)
{
constexpr size_t __index = __index_of<_Tp>;
return this->emplace<__index>(__il, std::forward<_Args>(__args)...);
}
template
enable_if_t