Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::holds_alternative variadic template

Tags:

c++

c++17

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
like image 340
Quest Avatar asked Jul 30 '19 07:07

Quest


1 Answers

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) || ...);
}
like image 125
StoryTeller - Unslander Monica Avatar answered Sep 17 '22 18:09

StoryTeller - Unslander Monica