I have an aggregate structure B
derived from another aggregate A
. I would like to initialize it and there are two options: ordinary aggregate initialization and C++20 designated initializers:
struct A {};
struct B : A { int x; };
int main() {
[[maybe_unused]] B x{{},1}; //ok everywhere
[[maybe_unused]] B y{.x=1}; //warning in GCC
}
The ordinary initialization {{},1}
works fine, but it looks too clumsy in this case due to extra {}
for parent aggregate.
And designated initializers {.x=1}
look better to me here, but they produce a weird warning in GCC:
warning: missing initializer for member 'B::<anonymous>' [-Wmissing-field-initializers]
6 | [[maybe_unused]] B y{.x=1}; //warning in GCC
| ^
Demo: https://gcc.godbolt.org/z/8jEYY16c6
Is there a way to use designated initializers here and calm down GCC at the same time to eliminate the warning?