문제

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 a constexpr constructor that would exceed the implementation defined recursion limits;

(fifth point in §5.19/2.). So the limit is implementation defined.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top