考慮下面的代碼:非終止constexpr函數是否定義良好?
constexpr unsigned f(unsigned x)
{
while (x & 1) x *= 3;
return x;
}
int main()
{
char a[f(2)];
char b[f(1)];
}
如果不是很明顯:對奇數x
,功能f
永遠不會終止。
當我編譯上面的程序with clang on coliru,b
似乎是一個VLA,但不a
:
warning: variable length arrays are a C99 feature [-Wvla-extension]
char b[f(1)];
有沒有在該編譯器決定停止常量表達式的評價一個明確的限制嗎?或者,如果一個符合標準的編譯器進入一個無限循環,它會非常好嗎? f(1)
產量UB?
替代標題:「是否constexpr解決暫停問題?」 (當然是在開玩笑) – stefan