Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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};
}
like image 746
Palace Chan Avatar asked Oct 16 '25 01:10

Palace Chan


1 Answers

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)

like image 126
OrenIshShalom Avatar answered Oct 18 '25 19:10

OrenIshShalom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!