Is there any way to force the compiler to fail if a constexpr if branch is not supposed to be hit?
this code below explains it all better than I could:
template<unsigned int number_base>
class arithmetic_type
{
if constexpr(number_base == 0 || number_base == 1)
{
//hey, compiler! fail this compilation please
}
else
{
//go on with class implementation
}
}
You want
static_assert()
. In this case, you can drop theif constexpr
and just assert directly:But generally, there may be places where you just need
static_assert(false)
. But that is ill-formed, that assertion would always trigger because it's a non-dependent conditional. In that case, you need to provide something that looks dependent but actually isn't: