top | item 46431247

(no title)

koyote | 2 months ago

> Hopefully future versions of C++ will mandate default initialization for all cases that are UB today and we can be free of this class of bug.

I have production code where we specifically do not initialise some data in order to be more performant (it gets set before it is read, but not at declaration as we do not have the values at that point).

I do agree that this (and numerous other footguns) make C++ a pain to work with. I also think it's too late to fix.

Ideally, all values would be initialised by default and instead you could forcefully construct something that is not initialised (e.g. something like `no_init double x[100];`). Instead, we have the bug-prone default and twenty different ways to initialise something, each with their own caveats.

C++ would be a much better language if most if not all defaults were reversed.

discuss

order

simonask|2 months ago

Every C++ compiler is perfectly able to optimize stale writes, so I'm always skeptical of code that leaves uninitialized fields around. I would always strongly prefer rearranging the code to be easier on the optimizer.