When I try to compile the following code using clang the static assertion fails:
#include <type_traits>
template<typename T>
int TypeTest()
{
if constexpr (std::is_same_v<bool, T>) {
return 1;
} else if constexpr (std::is_same_v<std::uint8_t, T>) {
return 2;
} else {
static_assert(false, "check");
return 3;
}
}
int main()
{
return TypeTest<bool>();
}
It even fails if there is no TypeTest is not used at all in the code. Can anybody tell me how the compiler arrives at the static assert during compile time?
if constexprto detect template types. I would rather use template specialization, and then let the compiler detect the missing template implementation by itself for unsupported types.if constexpris simpler: avoid the combination and repeting preceding negated condition.