Is it possible to wrap a std::holds_alternative
to a variadic template to use it with more types?
For example:
std::variant<bool, int, double, std::string> var = 4;
bool r = std::holds_alternative<bool, double>(var); // holds either bool or double
Yes, it's doable with a simple fold expression.
template<typename... Alts, typename... Ts>
constexpr bool holds_any_of(std::variant<Ts...> const& v) noexcept {
return (std::holds_alternative<Alts>(v) || ...);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With