gpt4 book ai didi

c++ - 当规则说初始化的完整表达式时,它是完整表达式

转载 作者:行者123 更新时间:2023-12-01 14:07:03 25 4
gpt4 key购买 nike

struct S {
constexpr S(int i): I(i),D(i) { } // full-expressions are initialization of I and initialization of D
private:
int I;
int D;
};
int main(){
constexpr S s1 = 1; //full-expression comprises call of S​::​S(int)
}
根据全表达式的定义:

A full-expression is

  1. an unevaluated operand,
  2. a constant-expression,
  3. an init-declarator or a mem-initializer, including the constituent expressions of the initializer,
  4. an invocation of a destructor generated at the end of the lifetime of an object other than a temporary object, or
  5. an expression that is not a subexpression of another expression and that is not otherwise part of a full-expression.


For an initializer, performing the initialization of the entity (including evaluating default member initializers of an aggregate) is also considered part of the full-expression.


子弹 3 说 s1 = 1是一个完整的表达式,因为它是一个初始化声明符和 I(i)是一个完整的表达式,因为它是 mem-initializerD(i) 类似.表示初始化实体 s1可以包含多个完整表达式吗?在这种情况下,这组全表达式中初始化的全表达式是哪个?

最佳答案

当然全表达式可以是动态 嵌套:考虑

void f(int i) {
++i; // (useless) full-expression
}
void g() {
f(1); // full-expression
}
因此,初始化 s1 之间没有冲突。作为 init-declarator full-expression 的一部分,同时还包含其 mem-initializers 的完整表达式。

关于c++ - 当规则说初始化的完整表达式时,它是完整表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62988291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com