AK: Simplify Variant's explicit overload detection mechanism a bit

This also allows us to remove the max-64-visitors restriction, and so
removes that assertion.
This commit is contained in:
Ali Mohammad Pur 2022-01-23 18:01:07 +03:30 committed by Ali Mohammad Pur
parent a2e791277e
commit bf82d9b5d7
Notes: sideshowbarker 2024-07-17 20:08:08 +09:00

View file

@ -81,22 +81,19 @@ struct Variant<IndexType, InitialIndex> {
template<typename IndexType, typename... Ts>
struct VisitImpl {
template<typename RT, typename T, size_t I, typename Fn>
static consteval u64 get_explicitly_named_overload_if_exists()
static consteval bool has_explicitly_named_overload()
{
// If we're not allowed to make a member function pointer and call it directly (without explicitly resolving it),
// we have a templated function on our hands (or a function overload set).
// in such cases, we don't have an explicitly named overload, and we would have to select it.
if constexpr (requires { (declval<Fn>().*(&Fn::operator()))(declval<T>()); })
return 1ull << I;
return 0;
return requires { (declval<Fn>().*(&Fn::operator()))(declval<T>()); };
}
template<typename ReturnType, typename T, typename Visitor, auto... Is>
static consteval bool should_invoke_const_overload(IndexSequence<Is...>)
{
// Scan over all the different visitor functions, if none of them are suitable for calling with `T const&`, avoid calling that first.
return ((get_explicitly_named_overload_if_exists<ReturnType, T, Is, typename Visitor::Types::template Type<Is>>()) | ...) != 0;
return ((has_explicitly_named_overload<ReturnType, T, Is, typename Visitor::Types::template Type<Is>>()) || ...);
}
template<typename Self, typename Visitor, IndexType CurrentIndex = 0>
@ -453,7 +450,6 @@ private:
template<typename... Fs>
struct Visitor : Fs... {
using Types = TypeList<Fs...>;
static_assert(Types::size < 64, "Variant::visit() can take a maximum of 64 visit functions.");
Visitor(Fs&&... args)
: Fs(forward<Fs>(args))...