News for hackers. Stuff that matters.
Re:C's designated initializers give us named arguments by adrian_b on Thursday November 19, 2026 @01:54PM (#50564484)
But the programmers must be able to foresee all possible effects of a different initialization order, supposing that it matters, otherwise they cannot write correct code.

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.

Re:C's designated initializers give us named arguments by pjmlp on Thursday November 19, 2026 @03:37PM (#50566006)
Of course there is, you can do that in initializtion lists, passing fields from one field into the other.

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.

https://godbolt.org/z/66TjPK49b