News for hackers. Stuff that matters.
Re:C's designated initializers give us named arguments by stephencanon on Thursday November 19, 2026 @09:07AM (#50560300)
As is usual for C++, the committee made arbitrary changes from what C had already laid out that made the feature worse. In particular, C++ requires that the fields appear in order, which means you cannot use it to do what the linked page shows.

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.

Re:C's designated initializers give us named arguments by pjmlp on Thursday November 19, 2026 @09:41AM (#50560749)
C doesn't have constructors, and field initialisation order matters.
Re:C's designated initializers give us named arguments by adrian_b on Thursday November 19, 2026 @10:08AM (#50561102)
This should be handled by the compiler, which shall ignore any overridden initializers and then the structure fields shall be initialized in some specified order, either the order from the definition of the structure or the initializer order.

So this is not a valid excuse for the choice made by the C++ specification.

Re:C's designated initializers give us named arguments by im3w1l on Thursday November 19, 2026 @10:14AM (#50561184)
[delayed]
Re:C's designated initializers give us named arguments by pjmlp on Thursday November 19, 2026 @10:16AM (#50561200)
Rice theorem enters the room.

The compiler doesn't read binary libraries, nor can foresee all possible side effects for each field constructor, or base classes.

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

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.
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.
Re:C's designated initializers give us named arguments by sorrywhat on Thursday November 19, 2026 @11:05AM (#50561901)
That doesn't stop the C++ committee from doing the obvious, trivial and correct thing.

  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.

Re:C's designated initializers give us named arguments by pjmlp on Thursday November 19, 2026 @11:24AM (#50562161)
International standards aren't made for a single person.
Re:C's designated initializers give us named arguments by radiospiel on Thursday November 19, 2026 @01:25PM (#50563987)
I am pretty sure that the execution order of the calls to foo() and bar() is not specified, regardless of how you want to order the assignment.
Re:C's designated initializers give us named arguments by jbarberu on Friday November 20, 2026 @04:44AM (#50572368)

    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...
Re:C's designated initializers give us named arguments by Someone on Thursday November 19, 2026 @11:15AM (#50562027)
[delayed]