Is using a union through more than one member legal in D?

bearophile bearophileHUGS at lycos.com
Tue May 4 12:38:45 PDT 2010


Ali Çehreli:
> It is unspecified behavior in C++ to access any member of a union other 
> than the one that was used last to store a value in it.
> Is that the case with D as well?

I have recently asked Walter about this, my purpose was to remove some undefined situations from D, but after a long discussion there was no clear answer, his answer to solve this problem was to say that SafeD modules can't use unions. Currently there is no official spec on this for unsafe D modules. You have to assume the worst (that is as in C, that assumes the optimization stages of the compiler can optimize away the last store into the union, so you can reliably read what can be the CPU registers only if you use the same type used last time), but I'd like some official way to do this, some way that forces the compiler to read and save the value (something like a volatile for unions, maybe). If you want, you can try asking again to Walter in the main group. (There are several other undecided things like this, like something related to pointer aliasing, etc).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list