
constexpr int f();constexpr int g() { return f();}constexpr int f() { return 42;}int main() { constexpr int i = g(); return i;} 这编译没有麻烦和工作.移动f的定义超过主触发器错误:’constexpr int f()’在其定义之前使用,正如我所料.
我认为它的工作原理是因为在调用g之前定义了f,因此两个调用都是常量表达式.
为什么f()和g()显然是常量表达式,即使f在被g调用时没有定义f?标准如何描述?
我在Coliru的GCC 6.1.0和Clang 3.8.0上测试了这个.
解决方法 在使用constexpr之前没有必要定义它.但是,在定义之前调用它的结果不是constexpr.因此,编译器正确地抱怨,因为您尝试使用非常量表达式初始化constexpr变量.§5.20/ p2常量表达式[expr.const](强调我的):
总结A conditional-Expression e is a core constant Expression unless the
evaluation of e,following the rules of the abstract machine (1.9),
would evaluate one of the following Expressions:…
(2.3) — an invocation of an undefined constexpr function or an undefined constexpr constructor;
以上是内存溢出为你收集整理的c – 在常量表达式上下文中定义之前嵌套的`constexpr`函数调用全部内容,希望文章能够帮你解决c – 在常量表达式上下文中定义之前嵌套的`constexpr`函数调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)