相关疑难解决方法(0)

在C++ 14 Standard中,它表示非constexpr函数不能用于constexpr函数的定义吗?

例如,除非incr()声明,否则下面的代码不会编译constexpr:

int incr(int& n) {
    return ++n;
}

constexpr int foo() {
    int n = 0;
    incr(n);
    return n;
}
Run Code Online (Sandbox Code Playgroud)

看看C++ 14中的§7.1.5/ 3,我们有:

constexpr函数的定义应满足以下约束条件:
(3.1) - 它不应是虚拟的(10.3);
(3.2) - 其返回类型应为字面类型;
(3.3) - 每个参数类型应为文字类型;
(3.4) - 其函数体应为= delete,= default或不包含的复合语句

(3.4.1) - asm-definition,
(3.4.2) - goto语句,
(3.4.3) - try-block,或
(3.4.4) - 非文字类型的变量的定义或静态或线程存储持续时间或未执行初始化的时间.

c++ language-lawyer constexpr c++14

7
推荐指数
2
解决办法
418
查看次数

标签 统计

c++ ×1

c++14 ×1

constexpr ×1

language-lawyer ×1