In C ++ 17, the rules about aggregates have changed.
For example, you can do it now in C ++ 17:
struct A { int a; }; struct B { B(int){} }; struct C : A {}; struct D : B {}; int main() { (void) C{2}; (void) D{1}; }
Note that we do not inherit the constructor. In C ++ 17, C and D are now aggregates, even if they have base classes.
With {} , the initialization of the aggregate takes effect and does not send any parameters, it will be interpreted in the same way as calling the default parent constructor from the outside.
For example, aggregate initialization can be disabled by changing class D to this:
struct B { protected: B(){} }; struct D : B { int b; private: int c; }; int main() { (void) D{};
This is due to the fact that aggregation initialization is not applied when there are members with different access specifiers.
The reason that = default works is because it is not a user-provided constructor. More info on this .
Guillaume Racicot Dec 05 '17 at 2:58 p.m. 2017-12-05 14:58
source share