问题
Triggered by this answer I was reading in the core guidelines:
C.45: Don’t define a default constructor that only initializes data members; use in-class member initializers instead
The reasoning given is
Reason
Using in-class member initializers lets the compiler generate the function for you. The compiler-generated function can be more efficient.
Note that this is specifically about a default constructor that does nothing but initialize the members and the guideline suggests that one should not write such a constructor.
The "bad" example is:
Example, bad class X1 { // BAD: doesn't use member initializers string s; int i; public: X1() :s{"default"}, i{1} { } // ... };
What can the compiler generated constructor do more efficient than the user-provided one in that particular example (or in any other example)?
Naively I would expect the initializer list giving the same opportunities for optimization as in-class initializers.
来源:https://stackoverflow.com/questions/56272431/how-can-a-compiler-generated-default-constructor-be-more-efficient-than-a-self-w