This isn't usually a huge deal, because C++ gives you other ways to have overloaded functions and default parameters, but it is sometimes mildly annoying.
So this is not a valid excuse for the choice made by the C++ specification.
The compiler doesn't read binary libraries, nor can foresee all possible side effects for each field constructor, or base classes.
Normally classes/structures should be defined in such a way that the order of initializing the members does not matter.
If the order of initialization matters, because there exists a really good reason to make the values of some members dependent on the values of other members (which adds superfluous information to the structure, but which might save some execution time), then I think that any programming language that allows this must enforce a unique order of initialization, i.e. the member order from the structure definition. Otherwise there would be no way to guarantee that when a constructor for a member accesses another member, that member does not contain uninitialized memory, because it has not been constructed yet.
I am not sure whether there exists a reasonable way in modern C++ to access a member of a class/structure from the constructor or destructor of another member, so unless one makes special efforts to circumvent this, e.g. by using some global variable that points to the structure whose constructors or destructors are currently executed, I think that in C++ you cannot easily define a class/structure where the order of executing the member constructors and destructors matters.
If you want for some members to depend on others, that can be easily done only in a constructor of the entire structure, not in the constructors of the members.
This is a tiny example, with constructor initialization lists, which is possible since C++ARM, and I am not getting creative with what is actually possible.
This would solve your problem.
Nonetheless, I do not see why in any correct code the order of member initialization may be relevant, even when different initialization can produce different results, because the values of some members depend on the values of other members. If the destructor of some member accesses another member that may have already been destructed, or not, there must be a way to determine this, e.g. destructing a member should replace it with some default value for that type, e.g. a zero or a NaN or a null pointer.
Accessing other members that have already been destructed may happen also in other contexts than during the explicit initialization of the structure, so it must never lead to unpredictable behavior.
Point foo = {
.y = foo(),
.x = bar(),
};
calls foo(), sets x to it, calls bar(), and sets y to it. And Point foo = {
.x = bar(),
.y = foo(),
};
calls bar(), sets x to it, then calls foo(), and sets y to it.I'm sure you can give me the reason they didn't do that, and it will be something like "this conflicts with LRR-value warp semantics when invoked from concept inside a consteval coroutine" or something equally stupid, but the problem is I don't care. This is a good piece of syntax sugar (and the only reason I use C++20) and it's being handicapped by insane features that I don't use and don't care about.
Point foo = {
.y = foo(),
.x = bar(),
};
calls foo(), sets x to it, calls bar(), and sets y to it. And
I think you meant the other way around... struct FooParams {
int x = getX();
const char* y = getY();
};
It turns out this was an issue all the way back in C++11, which didn't even have designated initialisers, but inline initialisers like this prevented the struct from being an aggregate, which meant you couldn't write FooParams{1, "y"}. That was fixed in C++14 (as I've just discovered).
https://gcc.godbolt.org/z/srz4P6nzM