考虑以下代码片段(使用 clang19 编译):
consteval bool IsEven(int n) {
return 0 == n % 2;
}
template<int n>
requires (IsEven(n)) // ok
void f1() {}
template<int n>
requires IsEven(n) // error: Atomic constraint must be of type 'bool'
// (found '<overloaded function type>')
void f2() {}
cppref 页面告诉我
IsEven(n)
不是主要表达式。我只是想知道:
为什么保守函数表达式 NOT 是主要表达式?
这与 consteval 或您收到的错误消息没有任何关系。主表达式只是表达式的语法类别,函数调用不属于该类别。