Consider this code that static asserts false inside an if constexpr (false)
statement.
#include <iostream>
template<typename T>
struct False {
static constexpr bool value = false;
};
template<typename T>
struct Foo {
void foo() {
if constexpr (false) {
static_assert(False<T>::value);
}
std::cout
<< "Static assert expression was: "
<< False<T>::value
<< std::endl;
}
};
int main() {
Foo<int> foo;
foo.foo();
}
On both GCC 12.2 and LLVM 15.0.0, this code compiles and prints:
Static assert expression was: 0
With even minor changes, this code does not compile. Changing static_assert(False<T>::value);
to static_assert(False<int>::value);
or removing the if constexpr (false)
check or changing it to if constexpr (true)
, I get "static assertion failed" as expected.
Is my above code legal? My understanding was that static_assert(...evaluates to false)
is ill-formed, but the code compiles.