本文介绍了在C ++ 14 Standard中,哪里表示不能在constexpr函数的定义中使用非constexpr函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,除非声明constexpr constexpr,否则以下代码不会编译:

For example the code below doesn't compile unless incr() is declared constexpr:

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

constexpr int foo() {
    int n = 0;
    incr(n);
    return n;
}

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

Looking at §7.1.5/3 in C++14 we have:

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

(3.4.1) — an asm-definition,
(3.4.2) — a goto statement,
(3.4.3) — a try-block, or
(3.4.4) — a definition of a variable of non-literal type or of static or thread storage duration or for which no initialization is performed.

推荐答案

稍后在[dcl.constexpr]/5中的两个段落:

Two paragraphs later, in [dcl.constexpr]/5:

由于存在incr(),因此不存在使foo()成为核心常量表达式的参数,因此程序的格式不正确(NDR).

No argument exists such that foo() could be a core constant expression because of incr(), therefore the program is ill-formed (NDR).

这篇关于在C ++ 14 Standard中,哪里表示不能在constexpr函数的定义中使用非constexpr函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 23:53
查看更多