Is it allowed in standard:
struct A
{
int a = 3;
int b = 3;
};
A a{0,1}; // ???
Is this class still aggregate?
clang
accepts this code, but gcc
doesn't.
Is it allowed in standard:
struct A
{
int a = 3;
int b = 3;
};
A a{0,1}; // ???
Is this class still aggregate?
clang
accepts this code, but gcc
doesn't.
In C++11 having in-class member initializers makes the struct/class not an aggregate — this was changed in C++14, however. This is something I found surprising when I first ran into it, the rationale for this restriction is that in-class initializers are pretty similar to a user defined constructor but the counter argument is that no one really expects that adding in-class initializers should make their class/struct a non-aggregate, I sure did not.
From the draft C++11 standard section
8.5.1
Aggregates (emphasis mine going forward):and in C++14 the same paragraph reads:
This change is covered in N3605: Member initializers and aggregates which has the following abstract:
This comment basically sums up the reluctance to allowing them to be aggregates:
The revised version N3653 was adopted in May 2013.
Update
emsr points out that G++ 5.0 now supports C++14 aggregates with non-static data member initializers using either
std=c++1y
or-std=c++14
:See it working live.