AK: Add explicit Variant conversion operators

This allows converting between Variants of different types with less
pain.
This commit is contained in:
Ali Mohammad Pur 2021-06-27 00:04:46 +04:30 committed by Linus Groh
parent dff3439ad0
commit bda19a9ff3
Notes: sideshowbarker 2024-07-18 11:27:46 +09:00

View file

@ -331,7 +331,7 @@ public:
}
template<typename... NewTs>
Variant<NewTs...> downcast() &
Variant<NewTs...> downcast() const&
{
Variant<NewTs...> instance { Variant<NewTs...>::invalid_index, Detail::VariantConstructTag {} };
visit([&](const auto& value) {
@ -342,6 +342,18 @@ public:
return instance;
}
template<typename... NewTs>
explicit operator Variant<NewTs...>() &&
{
return downcast<NewTs...>();
}
template<typename... NewTs>
explicit operator Variant<NewTs...>() const&
{
return downcast<NewTs...>();
}
private:
static constexpr auto data_size = integer_sequence_generate_array<size_t>(0, IntegerSequence<size_t, sizeof(Ts)...>()).max();
static constexpr auto data_alignment = integer_sequence_generate_array<size_t>(0, IntegerSequence<size_t, alignof(Ts)...>()).max();