Open
Description
Suppose I have an instance of meta_any
that I know may hold one of a closed set of different types.
With the current interface of meta_any
, I can try_cast
to each of the types, one after the other, and check the returned pointer until I find the correct type.
If there was e.g. an overload for try_cast
that took a parameter pack of those types and returned an std::variant
, containing the result of the first succesful cast, I could then std::visit
that std::variant
:
template <typename Arg, typename Out>
auto try_cast_impl(const entt::meta_any &any, Out &out) -> bool {
if (const Arg *value = any.try_cast<Arg>(); value != nullptr) {
out = *value;
return true;
}
return false;
}
template <typename... Args>
auto try_cast(const entt::meta_any &any) -> std::variant<Args...> {
std::variant<Args...> result;
(try_cast_impl<Args>(any, result) || ...);
return result;
}
...
const auto any_variant = try_cast<double, std::string>(any);
std::visit(
[](auto &&arg) {
if constexpr (std::is_same_v<T, double>) {
...
} else if constexpr (std::is_same_v<T, std::string>) {
...
}
}, any_variant);
This overload could be added to meta_any
, but some thought would need to be put into communicating an unsuccesful try.