As of C++14, C
is an aggregate (like A
), and C c{}
syntax performs aggregate initialization. This includes, in part:
[dcl.init.aggr]/8 If there are fewer initializer-clauses in the list than there are elements in a non-union aggregate, then each element not explicitly initialized is initialized as follows:
(8.1) — If the element has a default member initializer (12.2), the element is initialized from that initializer.
(8.2) — Otherwise, if the element is not a reference, the element is copy-initialized from an empty initializer list (11.6.4).
(8.3) — Otherwise, the program is ill-formed.
So C c{};
is equivalent to C c{{}, 3};
. Initializing an int*
member with empty list causes it to be zero-initialized.
In C++11, C
is not an aggregate (having a default member initializer was disqualifying), and C c{};
calls an implicitly-defined constructor that leaves c.a
member uninitialized.
In all versions of the standard, B
is not an aggregate due to the user-defined constructor. B b{};
calls that constructor, which explicitly initializes b
member and chooses to leave a
uninitialized.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…