which is the full-expression when the rule says the full-expression of initialization

荒凉一梦 提交于 2020-07-20 23:43:15

问题


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)
}

According to the definition of full-expression:

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.

The bullet 3 says s1 = 1 is a full-expression because it's an init-declarator and I(i) is a full-expression due to it's a mem-initializer and similarly for D(i). It means that initialize entity s1 can contain more than one full-expression? In this case, Which is the full-expression of the initialization in this set of full-expressions?


回答1:


Of course full-expressions can be dynamically nested: consider

void f(int i) {
  ++i;   // (useless) full-expression
}
void g() {
  f(1);  // full-expression
}

As such, there’s no conflict between initializing s1 being part of the init-declarator full-expression while also containing full-expressions for its mem-initializers.



来源:https://stackoverflow.com/questions/62988291/which-is-the-full-expression-when-the-rule-says-the-full-expression-of-initializ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!