I have a long series of if constexpr
statements and would like to trigger a compile-time error if none of them succeed.
Specifically, I have an abstract syntax tree whose result I would like to convert to a specific set of types that I might need. I have AsInt(), AsDouble(), etc. working, but I need to be able to do so more dynamically based on a supplied type.
As things stand, I've written a templated As() member function, but it's error-checking is clunky. Specifically, using static assert requires an unwieldy test condition. Here's a simplified version:
template <typename T>
T As() {
if constexpr (std::is_same_v<T,int>) return AsInt();
if constexpr (std::is_same_v<T,double>) return AsDouble();
...
if constexpr (std::is_same_v<T,std::string>) return AsString();
static_assert( std::is_same_v<T,int>
|| std::is_same_v<T,double>
|| ...
|| std::is_same_v<T,std::string>,
"Invalid template type for As()" );
}
Is there a simpler way to trigger the static assert (or equivalent) if all of the conditions fail?