Can we just have struct inheritence already?

Olivier FAURE couteaubleu at gmail.com
Tue Jun 18 18:08:31 UTC 2019


On Saturday, 15 June 2019 at 08:40:52 UTC, XavierAP wrote:
> I'm not yet sure in the general case about void initialization, 
> but for me this interesting case shows that:
>
> - void initialization of bool should be illegal, not just in 
> @safe code, anywhere. Initializing an int void works as 
> (undefinedly) as intended, but with bool it breaks the spec;

It's not just bool, it's also enums at the very least.

I suspect someone familiar enough with their compiler could find 
other ways to leak memory; eg by casting a `byte` to an `int` in 
such a way that the compiler loads 4 bytes of uninitialized 
memory, instead of 3 zero-ed bytes and 1 uninitialized byte, but 
still assumes the resulting int is less than 255.

Also, Rust apparently had the same problem for a while:
https://gankro.github.io/blah/initialize-me-maybe/

> - I never understood why D adopted over from C/C++ that bool is 
> int and implicitly convertible; I don't understand when this 
> may be useful.

It doesn't really matter, the problem still occurs if you do an 
explicit cast.

The problem is that uninitialized memory lets the compiler make 
assumptions that aren't always true. Bounds checking is the 
easiest to demonstrate, but you can probably find other 
applications if you dig a little.


More information about the Digitalmars-d mailing list