[英]Compiler Error: function call must have a constant value in a constant expression
[英]“function call must have a constant value in a constant expression”
我有以下代码:
constexpr unsigned long long power_function(const unsigned long long prime, const unsigned long long iterations) {
return iterations > 0 ? prime * power_function(prime, iterations - 1) : prime;
}
/* Inside my Class Definition*/
private:
static constexpr unsigned long long prime = power_function(-363, 1'000'000); //Error occurs here
IntelliSense抱怨power_function
使用不当。 但对于我的生活,我无法弄清楚问题是什么。 我正在使用Visual Studio 2015,仅供参考。
错误消息:
Error C2131 expression did not evaluate to a constant Basic Server c:\<snip> 28
Error C2131 expression did not evaluate to a constant Basic Server c:\<snip> 33
第28行对应于返回函数所在的行,第33行对应于定义constexpr的行。
gcc和clang编译器中constexpr
的递归限制为512。 因为编译器将constexpr
函数解释为内联函数(C ++ Standard 7.1.5 subsec.2),所以它们必须在编译时解析。 如果在512次迭代后编译器无法将表达式解析为常量,则会停止编译并引发错误。 该标准建议至少为递归constexpr
函数调用512,但不要求它(参见标准中的附录B [implimits] 2.38)。
此限制可能适用于Visual Studio,但我不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.