I am refactoring a code that previously used SFINAE in C++14 to handle different categories of types (float, ints, strings, etc.). Something like the following:
template<typename T, typename Enable = void>
struct S;
template<typename Float>
struct S<Float, std::enable_if_t<is_floating_point<Float>::value>> {
int f() { ... }
};
// other template specializations for other categories of types
I am trying to turn it into something like this in C++17:
template <typename T>
int f() {
if constexpr (is_floating_point<T>::value) {
} else if constexpr(...) {
...
} else {
// compile-time error with useful message ?
}
}
What is the proper way to generate a useful compile-time error message in the final else statement for all the types that couldn't be handled? static_assert
seems to be triggered regardless of whether the else
block is considered by the compiler. Simply removing the final else
statement works, but gives a "no return statement ..." error, which is not informative, and also if my function had returned void, it would not have led to any error at all.