如何理解"manifestly constant-evaluated"的定义?
How to understand the definition of "manifestly constant-evaluated"?
P0595引入了std::is_constant_evaluated()
功能。本文讨论了在某些情况下,包含表达式是常量表达式,但编译器不需要在编译时求值的情况。给出的例子是:
constexpr double power(double b, int x) {
if (std::is_constant_evaluated() && x >= 0) {
// ...
// return r;
} else {
// Let the code generator figure it out.
return std::pow(b, (double)x);
}
}
double thousand() {
return power(10.0, 3);
}
编译器可能会在编译时评估 power(10.0, 3)
,但这不是必需的。因此,is_constant_evaluated
returns false.
论文因此引入了“明显常数求值”的概念:
Our approach is to precisely identify a set of expressions that are "manifestly constant-evaluated" (a new technical phrase) and specify that our new function returns true
during the evaluation of such expressions and false
otherwise.
Specifically, we include two kinds of expressions in our set of expressions "manifestly constant-evaluated". The first kind is straightforward: Expressions in contexts where the standard already requires a constant result, such as the dimension of an array or the initializer of a constexpr variable. ...
这对我来说很有意义。然而,标准中的实际措辞让我感到困惑:
An expression or conversion e
is manifestly constant-evaluated if it is:
- a constant-expression, or ...
换句话说,该标准指定所有常量表达式都明确地进行常量求值,这(对我来说)似乎没有包含表达式出现在需要常量表达式的上下文中的要求。 proposal 备注 power(10.0, 3)
是一个核心常量表达式,这也是我的理解;这使它成为一个常量表达式。如果所有常量表达式都明确地进行常量求值,那么 is_constant_evaluated
在这里似乎必须 return 为真。
我应该如何理解标准中的定义,使其具有与提案意图一致的准确含义?
这是我最喜欢的。它没有说“常量表达式”。它说“一个常量表达式。”†
constant-expression 是语法术语。它是 C++ 语法中强制使用常量表达式的那些地方的替代。
例如,template-argument 的语法(特别是语法)是 constant-expression(或 type-id 或 id-expression),所以这个规则意味着当评估 constant-expression 时,它显示为 模板参数,评估显然是常量评估。
相比之下,if constexpr
中的文法不采用常量表达式,它只是takes a condition.所以这个项目符号不足以涵盖 if constexpr
,这就是为什么有一个特殊的额外项目符号来涵盖“constexpr if 语句的条件”。
† 想象一下必须口头回答这个问题。你能听到连字符,对吧?
P0595引入了std::is_constant_evaluated()
功能。本文讨论了在某些情况下,包含表达式是常量表达式,但编译器不需要在编译时求值的情况。给出的例子是:
constexpr double power(double b, int x) {
if (std::is_constant_evaluated() && x >= 0) {
// ...
// return r;
} else {
// Let the code generator figure it out.
return std::pow(b, (double)x);
}
}
double thousand() {
return power(10.0, 3);
}
编译器可能会在编译时评估 power(10.0, 3)
,但这不是必需的。因此,is_constant_evaluated
returns false.
论文因此引入了“明显常数求值”的概念:
Our approach is to precisely identify a set of expressions that are "manifestly constant-evaluated" (a new technical phrase) and specify that our new function returns
true
during the evaluation of such expressions andfalse
otherwise.Specifically, we include two kinds of expressions in our set of expressions "manifestly constant-evaluated". The first kind is straightforward: Expressions in contexts where the standard already requires a constant result, such as the dimension of an array or the initializer of a constexpr variable. ...
这对我来说很有意义。然而,标准中的实际措辞让我感到困惑:
An expression or conversion
e
is manifestly constant-evaluated if it is:
- a constant-expression, or ...
换句话说,该标准指定所有常量表达式都明确地进行常量求值,这(对我来说)似乎没有包含表达式出现在需要常量表达式的上下文中的要求。 proposal 备注 power(10.0, 3)
是一个核心常量表达式,这也是我的理解;这使它成为一个常量表达式。如果所有常量表达式都明确地进行常量求值,那么 is_constant_evaluated
在这里似乎必须 return 为真。
我应该如何理解标准中的定义,使其具有与提案意图一致的准确含义?
这是我最喜欢的。它没有说“常量表达式”。它说“一个常量表达式。”†
constant-expression 是语法术语。它是 C++ 语法中强制使用常量表达式的那些地方的替代。
例如,template-argument 的语法(特别是语法)是 constant-expression(或 type-id 或 id-expression),所以这个规则意味着当评估 constant-expression 时,它显示为 模板参数,评估显然是常量评估。
相比之下,if constexpr
中的文法不采用常量表达式,它只是takes a condition.所以这个项目符号不足以涵盖 if constexpr
,这就是为什么有一个特殊的额外项目符号来涵盖“constexpr if 语句的条件”。
† 想象一下必须口头回答这个问题。你能听到连字符,对吧?