【发布时间】:2020-12-01 12:24:50
【问题描述】:
clang(trunk) 给出以下代码的错误:
consteval void f() {}
int main()
{
f(); // error: call to consteval function 'f' is not a constant expression
// note: subobject of type 'void' is not initialized
}
而 gcc(trunk) 编译它没有错误。
我觉得这可能是一个clang错误,因为gcc和clang都接受这个代码:
consteval int g() { return 42; }
int main()
{
g(); // ok
}
这是code 玩。
那么这是一个clang错误,还是代码格式错误,或者有ub,或者其他什么?
编辑:我觉得有必要指出 clang 允许从其他函数调用 f,如果它们也是 consteval 的话。仅当从非 consteval 函数调用 f 时才会出现错误:
consteval int h()
{
f(); // ok
return 42;
}
demo.
【问题讨论】:
-
如果 f 是 constexpr 是否有效? (强制 constexpr 时,如
constexpr int x = (f(), 5);) -
@Dani 不,它给出了相同的error。
-
@Barry True,但我的印象是它已经在主干中实施了一段时间。那么它只是一个不完整的功能呢?不过仍然是一个奇怪的错误。
-
那么我会说这是一个编译器错误。例如,理由是
std::sort。
标签: c++ language-lawyer c++20 constant-expression consteval