struct S {
int A;
int B = A;
int C;
};
int i = 0;
S s = {.C = i++, .A = ++i};
What would you expect code like this to do?This would still be a lot better for 99% of real world use cases than requiring the programmer to manually place the items in declaration order.
f(a++, b++)
That footgun doesn't seem to have very much impact in the real world that I've seen. Largely because people do not write complex expressions like that anymore. Since we already mostly avoid such expressions, we may as well take the benefit for designated initializers.It is a much smaller footgun though and I don't recall ever being bitten by it. I have definitely been bitten by member initialisation order though multiple times. Generally because of questionable designs where one member is passed as a parameter of another member and it hasn't actually been initialised yet.
Really I think the answer is to initialise in the order that initialisation is written (this is what Rust does) rather than declaration order. But that would be a breaking change so I guess they opted for the conservative choice.
On the other hand evaluation order of aggregate initializers is well defined (left to right) and there was little appetite to weaken it.
It might still happen in the future of course.
The problem is, someone thought this was a good idea, but the act of supporting it ruled out a lot of more-useful future improvements.
...no chaining of designated initializers:
const bla_t bla = { .a.b.c = 23 };
...and no array indexing: const blub_t blub = {
.arr = {
[4] = 23,
[2] = 1
}
};
...all those limitations taken together, and the C++ designated initialization feature is pretty much useless except for the most trivial structs - while in C99, designated initialization really shines with complex, nested structs.The funny thing is that none of those limitations would be required. Clang had supported full C99 designated init in C++ mode just fine for many years before C++20 appeared.
I think you can do a similar sort of array initialization in C#, but definitely not chained initializers. Those are both so useful, but I can see why they aren't included in C++/++
We find it pretty useful even with the limitations. Certainly not “pretty much useless.”
[0] https://gitlab.com/nbdkit/nbdkit/-/blob/cd761c9bf770b23f678f...
Even though many only know it with C, Fortran, Pascal and Ada were also common when C++ was gaining adoption.
It was never about being a polyglot compiler.
I also thought that the behaviour as standardized was useless, but recently I started writing more minimalist code eschewing constructors where aggregate initialisation would suffice, and I haven't really missed the ability to reorder initializers or skip them.
struct foo {
int a = 0;
int b = a+1;
}
If the compiler just did the initialization in the order of declaration, regardless of the order in the initialization list this would not do what you expect:
struct obj {
int a;
int b;
} int ival = 0;
auto o = obj {.b = ++ival, .a = ival};
o.a would not equal o.b.I would like to have the initialization syntax of C because then one could reorder elements (say for packing reasons) and the designated initialization would “just work”…except it wouldn’t.
C++ designated initialization does buy you two things: 1- documentation, but more importantly 2- if you do reorder a struct or class data members the compiler will warn you that your initialization lists are now invalid rather than silently failing. I don’t know how to even find them all in a large code base any other way!
From what I can tell, the snippet you posted would compile fine in C++20 mode.
I hate this about C++! In C you can initialize them in any order, and this allowed us to write nbdkit plugins in a very natural way:
where the order is not related to the order the fields appear in the struct (that has to be maintained for ABI reasons), and not all fields need to appear (the others are initialized with 0/NULL).For C++ we have to do this mess:
https://bugzilla.redhat.com/show_bug.cgi?id=1418328#c3
Anyway my question is .. why is this, C++ people?