Palace Chan
Palace Chan

Reputation: 9183

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

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};
}

Upvotes: 4

Views: 817

Answers (1)

OrenIshShalom
OrenIshShalom

Reputation: 7112

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)

Upvotes: 5

Related Questions