News for hackers. Stuff that matters.
Re:The C++20 Naughty and Nice List for Game Devs by zalyalov on Monday December 25, 2023 @08:17AM (#38762349)

  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?
Re:The C++20 Naughty and Nice List for Game Devs by flohofwoe on Monday December 25, 2023 @08:57AM (#38762616)
The only useful solution for C++ would be to let the compiler reorder the initialization in declaration order, e.g. first A, then B, then C.

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.

Re:The C++20 Naughty and Nice List for Game Devs by IshKebab on Monday December 25, 2023 @09:33AM (#38762896)
This would be a big footgun though. That already happens for initialising class members in constructors and it's enough of a footgun that compilers have a warning to tell you if your initialisation order is different to the declaration order.
Re:The C++20 Naughty and Nice List for Game Devs by jzwinck on Monday December 25, 2023 @11:32AM (#38763884)
Doesn't the same footgun apply to function call arguments which are evaluated in an unspecified 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.
Re:The C++20 Naughty and Nice List for Game Devs by IshKebab on Monday December 25, 2023 @12:52PM (#38764542)
Yeah that is a footgun too. Enough that Rust defines argument evaluation order.

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.

Re:The C++20 Naughty and Nice List for Game Devs by gpderetta on Monday December 25, 2023 @05:56PM (#38767090)
Yes, and there was a strong push to define the order although it ultimately failed.

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.

Re:The C++20 Naughty and Nice List for Game Devs by calamari4065 on Monday December 25, 2023 @11:48AM (#38764013)
Is `int B = A` even legal? Aren't field initializers like that supposed to be compile time constant?
Re:The C++20 Naughty and Nice List for Game Devs by nemetroid on Monday December 25, 2023 @12:28PM (#38764370)
It's allowed since C++11.
Re:The C++20 Naughty and Nice List for Game Devs by rwmj on Monday December 25, 2023 @08:45AM (#38762529)
I wouldn't write that, and if someone did they'd get to keep all the pieces.
Re:The C++20 Naughty and Nice List for Game Devs by CamperBob2 on Monday December 25, 2023 @02:02PM (#38765107)
Fail to compile due to forward references in initializers.

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.