我有以下代码:
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的行。
constexpr
gcc和clang编译器的递归限制为512 。由于编译器将constexpr
函数解释为内联函数(C ++ Standard 7.1.5 subsec。2),因此必须在编译时进行解析。如果经过512次迭代,编译器无法将表达式解析为常量,则编译器将停止并引发错误。该标准建议递归constexpr
函数调用的最小值为512 ,但不要求这样做(请参阅标准中的附录B [限制] 2.38)。
此限制可能适用于Visual Studio,但我不确定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句