问题
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
- an unevaluated operand,
- a constant-expression,
- an init-declarator or a mem-initializer, including the constituent expressions of the initializer,
- an invocation of a destructor generated at the end of the lifetime of an object other than a temporary object, or
- 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