您可以在consteval中有constexpr对象,但不能在constexpr中使用consteval。
为什么?
我认为consteval应该是某种“狭窄”的constexpr。
请帮助我从这种设计中理解。
constexpr int constexpr_sqr(int n) { return n*n; }
consteval int consteval_sqr(int n) { return n*n; }
constexpr int constexpr_sqr2(int n) {
// not allowed
// return consteval_sqr(n);
// not allowed
// constexpr imm = consteval_sqr(n);
// return imm;
return constexpr_sqr(n);
}
int main() {
// while can do this
constexpr auto imm = consteval_sqr(999);
}
[现场]
这是争论。constexpr
函数不需要进行常量求值。这意味着n
不能在常量表达式中使用。
我认为consteval应该是某种“狭窄”的constexpr。
不,这些都只是功能必须是恒定的评估。这意味着它们的参数必须始终在常量表达式中可用。
您可以constexpr
使用在常量表达式中不可用的参数来调用函数,只要您不在需要常量表达式的上下文中,它的格式仍然正确。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句