I like to to specialize a function with respect to a template type. This works fine with std::is_same
.
Now, I like the code to not compile, if a not specialized type is given.
template <typename T>
constexpr double get(const T& item) const {
if (std::is_same<T, SOME_TYPE>::value)
return item.get();
if (std::is_same<T, SOME_OTHER_TYPE>::value)
return -1.0;
else {
// Compile time error, if this branch is created.
static_assert(false, "Getter not implemented for Type");
return -1.0;
}
}
This snippet does not compile, with the error message
error: static assertion failed with "Getter not implemented for Type"
although the function is only instantiated with SOME_TYPE
or SOME_OTHER_TYPE
.
How can I make sure at compile time, that no others types than SOME_TYPE
or SOME_OTHER_TYPE
are used?