Consider the following code:
constexpr unsigned f(unsigned x)
{
while (x & 1) x *= 3;
return x;
}
int main()
{
char a[f(2)];
char b[f(1)];
}
In case it isn't obvious: for odd integers x
, the function f
never terminates.
When I compile the above program with clang on coliru, b
seems to be a VLA, but not a
:
warning: variable length arrays are a C99 feature [-Wvla-extension]
char b[f(1)];
Is there a well-defined limit at which the compiler decides to stop evaluation of a constant expression? Or would it be perfectly fine for a conforming compiler to go into an infinite loop? Does f(1)
yield UB?
There are a number of things which means that an expression is not a core constant expression is
-- an invocation of a
constexpr
function or aconstexpr
constructor that would exceed the implementation defined recursion limits;
(fifth point in §5.19/2.). So the limit is implementation defined.
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