From what I gather from this answer, a constexpr function's result is not a constant-expression if the function has not been declared yet. What surprises me is the following code snippet [ˈsnɪpɪt] 片段:

constexpr int f();constexpr int g() {return f();
}constexpr int f() {return 42;
}int main() {constexpr int i = g();return i;
}

This compiles without trouble and works. Moving f's definition past main triggers error: 'constexpr int f()' used before its definition, as I would expect.

I presume that it works because f has been defined before the call to g, thus both calls are constant expressions.

Why are f() and g() apparently constant-expressions, even though f isn't defined when it is called by g? How is this described by the Standard?

I have tested this on Coliru's GCC 6.1.0 and Clang 3.8.0.

2个回答

最佳答案

  • It is not necessary to define a constexpr before it is used. However, the result of calling it before its definition is not constexpr. Consequently, the compiler rightfully complains because you're trying to initialize a constexpr variable with a non-constant expression.

    §5.20/p2 Constant expressions [expr.const] (Emphasis Mine) :

    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;

参考答案2

  • As linked by T.C. in his comment, this is subject to a defect report.

    According to 5.20 [expr.const] bullet 2.3, an expression is a constant expression unless (among other reasons) it would evaluate

    • an invocation of an undefined constexpr function or an undefined constexpr constructor;

    This does not address the question of the point at which a constexpr function must be defined. The intent, in order to allow mutually-recursive constexpr functions, was that the function must be defined prior to the outermost evaluation that eventually results in the invocation, but this is not clearly stated.

    This makes it clear that the example is well-formed and should indeed work as expected as long as f is defined before the call to g.

Nested `constexpr` function calls before definition in a constant-expression context相关推荐

  1. 4.2.8. Window Function Calls

    4.2.8. Window Function Calls 4.2.8.窗口函数调用 A window function call represents the application of an ag ...

  2. NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

    NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. ...

  3. Remote table-valued function calls are not allowed.

    远程访问其它服务器数据库的表 select top 100 * from [服务器名/IP].[数据库名].dbo.table(nolock) 提示:Remote table-valued funct ...

  4. constexpr specifier

    转载自: constexpr specifier constexpr - specifies that the value of a variable or function can appear i ...

  5. 《C++ Primer 5th》笔记(2 / 19):变量和基本类型

    文章目录 基本内置类型 算术类型 内置类型的机器实现(类型在物理层面上的说明) 建议:如何选择类型 类型转换 建议:避免无法预知和依赖于实现环境的行为 算术表达式里使用布尔值 含有无符号类型的表达式( ...

  6. [golang][history]The Go Annotated Specification\ Go注释规范18c5b488a3b2e218c0e0cf2a7d4820d9da93a554...

    18c5b488a3b2e218c0e0cf2a7d4820d9da93a554 20080303 https://github.com/landv/golang The Go Annotated S ...

  7. RIPS user guide for fresh

    RIPS download NOTE: RIPS 0.5 development is abandoned since 2013 due to its fundamental limitations. ...

  8. c语言编译常见错误提示,c语言编译常见错误

    c语言编译常见错误 (2012-04-17 21:33:53) 标签: 杂谈 分类: 计算机 1."c" not an argument in function sum 该标识符不 ...

  9. (5)风色从零单排《C++ Primer》 const,typedef,auto,decltype

    从零单排<C++ Primer> --(5)const,typedef,auto,decltype   CONST 多文件下 //file_1.cc defines and initial ...

最新文章

  1. npm你不知道的8个快捷键
  2. 告诉你KVC的一切-b
  3. Linux C 串口属性设置
  4. 作为 IT 行业的过来人,你有什么话想对后辈说的?
  5. 大华股份携手阿里云计算 涉足智能家居
  6. Spring源码之ApplicationContext(一)
  7. html入门的一些东西
  8. ABAQUS单位统一
  9. 惠普T5325 惠普T5565 惠普T5400 瘦客机评测
  10. Excel如何实现随机不重复抽取
  11. 【VBA】汉字转换全拼函数
  12. windows xp sp2的产品密钥
  13. base64解码中文乱码
  14. sphinxapi.php 详解,【转】Sphinx PHP api全文检索的例子
  15. docker 内安装字体
  16. PYthon 获取快捷方式(Lnk文件)指向文件
  17. matlab运用函数随机点名,matlab随机点名程序
  18. 罗技c270摄像头支持linux,电信局点使用罗技C270摄像头调试指导
  19. Like What You Like: Knowledge Distill via Neuron Selectivity Transfer 论文翻译
  20. Python的数据分析可视化十种技能总结

热门文章

  1. nodejs随记04
  2. git 提交修改到github上
  3. 【转载】数字签名是什么?
  4. ListView的分页显示
  5. 域名转入Godaddy详解
  6. [CentOS Python系列] 四.阿里云服务器CentOS连接远程MySQL数据库及pymsql
  7. App设计灵感之十二组精美的数据图表展示App设计案例
  8. LANGUAGE MODELS ARE OPEN KNOWLEDGE GRAPHS —— 读后总结
  9. 1.3 Hive架构原理
  10. 括号的匹配(表达式的合法性检查)