This program:
constexpr void f() { x: ; }
is compiled by gcc, but clang says:
error: statement not allowed in constexpr function
So is this code valid?
Short answer: static_assert(false) should never appear in a constexpr if expression, regardless of whether it's in a template function or whether it's in the discarded branch.
The easiest way to check whether a function (e.g., foo ) is constexpr is to assign its return value to a constexpr as below: constexpr auto i = foo(); if the returned value is not constexpr compilation will fail.
A call to a constexpr function produces the same result as a call to an equivalent non- constexpr function , except that a call to a constexpr function can appear in a constant expression. The main function cannot be declared with the constexpr specifier.
As pointed out in a comment by Nathan Pierson, Clang is correct, and the code is ill-formed. According to the current working draft (which includes C++20), dcl.constexpr#3 says:
The definition of a constexpr function shall satisfy the following requirements:
...
its function-body shall not enclose
...
an identifier label,
...
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With