3 ms·
> Those have problems, among other things the cost of double-initialization. In D: int x; // initialization eliminated as a 'dead store' by optimizer
by WalterBright 12y ago
> Those have problems, among other things the cost of double-initialization.
In D:
int x; // initialization eliminated as a 'dead store' by optimizer
x = 3;
You can also do:
int x = void;
making the "don't initialize this" proactively marked, rather than being the default behavior.
- TheLoneWolfling 12y agoRight, and in trivial cases that works. But if you've got, for example int x; if (somethingThatYouKnowIsTrueButTheCompilerDoesnt()) x = 3; there's no way to express that with either of those methods. You'll end up with the redundant store.
- WalterBright 12y agoIf you know it is always true, why is it in an if() ? You'd right it as: int x; if (somethingThatYouKnowIsTrueButTheCompilerDoesnt()) x = 3; else assert(0); and the compiler will remove the redundant store to x.