c++ - C++11 aggregate initialization for classes with non-static member initializers -


is allowed in standard:

struct {   int = 3;   int b = 3; };  a{0,1}; // ??? 

is class still aggregate? clang accepts code, gcc doesn't.

in c++11 having in-class member initializers makes struct/class not aggregate — changed in c++14, however. found surprising when first ran it, rationale restriction in-class initializers pretty similar user defined constructor counter argument no 1 expects adding in-class initializers should make class/struct non-aggregate, sure did not.

from draft c++11 standard section 8.5.1 aggregates (emphasis mine going forward):

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).

and in c++14 same paragraph reads:

an aggregate array or class (clause 9) no user-provided constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), , no virtual functions (10.3).

this change covered in n3605: member initializers , aggregates has following abstract:

bjarne stroustrup , richard smith raised issue aggregate initialization , member-initializers not working together. paper proposes fix issue adopting smith's proposed wording removes restriction aggregates can't have member-initializers.

this comment sums reluctance allowing them aggregates:

aggregates cannot have user-defined constructors , member-initializers kind of user-defined constructor (element) (see core defect 886). i'm not against extension, has implications on our model of aggregates is. after acceptance of extension i know how teach aggregate is.

the revised version n3653 adopted in may 2013.

update

emsr points out g++ 5.0 supports c++14 aggregates non-static data member initializers using either std=c++1y or -std=c++14:

struct { int i, j = i; }; a = { 42 }; // a.j 42 

see working live.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -