c++ aggregate initializer g++ bug?

Viewed 112

I have a problem compiling this simple aggregate named initialization with g++

struct A  {
    int a;
};
struct B {
    int b;
};
struct C {
    A a;
    B b;
};

void fun() {
    A a{0};
    B b {12} ;
    // g++ is OK with this
    C c{  .a=a , .b=b };
    // g++ is OK with this
    C cc{ 0, {12}};
    // msvc, clang accept this but g++ does not
    C ccc{.a=0, .b={12}};
}

g++ complains with error: 'A' has no non-static data member named 'b

Is this a gcc bug ?

1 Answers

On the contrary, it's an everyone-else bug (I filed llvm bug #49020, which Richard Smith has already fixed on trunk). gcc is correct in rejecting (although the error makes no sense. A indeed does have no non-static data member named b but that isn't the issue...)

This:

C ccc{.a=0, .b={12}};

should be ill-formed. What this means is you're initializing the a member of C with =0 and the b member of C with ={12}.

But you can't initialize an A from = 0. This is ill-formed:

A a = 0;

Hence, the broader initialization is also ill-formed. I don't know why everyone else accepts.

Related