why does this c++ code snippet compile with std=c++17 but fails to compile with std=c++20?

331 Views Asked by At

In godbolt: https://godbolt.org/z/bY1a3e1Wz

the code in question is below (the error message says "error: either all initializer clauses should be designated or none of them should be" but I dont understand what it is saying..

struct A {
    int a;
    bool b;
};

struct B : A {
    long c;
};

int main(void) {
    B foo {{.a = 1, .b = false}, .c = 7};
}
1

There are 1 best solutions below

0
OrenIshShalom On

From what I see in the docs, it looks like this should work:

B foo = {{.a = 1, .b = false}, 7};

The reason seems to be that the first initializer is "nameless" (by order) and so the second one (c) should also be initialized by order rather than by name (it can not be designated)