c++ - C++11 default class member initialization with initializer list , simultaneously -
can point me please, corresponding paragraph of c++ standard, or maybe can provide explanation why code not compile if uncomment text ({123})
?
generally speaking understand wrong usage of default member initialization , initialization via initializer list, can't refer exact reasons.
enum class my: int { = 1 }; struct abc { int a;/*{123};*/ //compilation failed if uncommented m; }; abc = {1, my::a};
compiler error, in case of uncommented text:
error: not convert ‘{1, a}’ ‘<brace-enclosed initializer list>’ ‘abc’
the below syntax:
abc = {1, my::a};
is list-initialization may perform differently depending on type being initialized. without non-static data member initializer (/*{123};*/
), struct aggregate , case falls under [dcl.init.list]/p3:
- otherwise, if
t
aggregate, aggregate initialization performed.
however, aggregate type, following conditions must met in c++11:
an aggregate array or class (clause 9) no user-provided constructors (12.1), no brace-or-equal-initializers non-static data members (9.2), no private or protected non-static data members (clause 11), no base classes (clause 10), , no virtual functions (10.3).
that is, usage of nsdmi (non-static data member initialization) breaks above set of rules, , result, instance of type can no longer list-initialized.
this rule has changed in c++14, , current wording reads [dcl.init.aggr]/p1:
an aggregate array or class with
(1.1) no user-provided, explicit, or inherited constructors ([class.ctor]),
(1.2) no private or protected non-static data members ([class.access]),
(1.3) no virtual functions, and
(1.4) no virtual, private, or protected base classes ([class.mi]).
[ note: aggregate initialization not allow accessing protected , private base class' members or constructors. — end note ]
wiki
Comments
Post a Comment