Re:C's designated initializers give us named arguments
by neonz80 on Thursday November 19, 2026 @01:09PM
(#50563704)
One problem with this is if one of the initializers throws an exception. The already constructed members must be destroyed. With an arbitrary initialization order the compiler will have to create different code for each order used and make sure it is this code that is called during unwinding.
[ Parent
| on HN ]
Re:C's designated initializers give us named arguments
by adrian_b on Thursday November 19, 2026 @01:37PM
(#50564209)
Like I have said, a possible choice in the language specification in that whenever a set of structure members are initialized, e.g. in a function invocation, the initializations shall be done in the order from the structure definition.
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.
Re:C's designated initializers give us named arguments
by quietbritishjim on Thursday November 19, 2026 @03:33PM
(#50565963)
That would solve one problem, and create another one: fields get initialised in a different order depending on the caller. You have a valid point but don't pretend that there's no trade off at all.