Int to float?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 5 16:57:14 PST 2015


On Thursday, 5 March 2015 at 23:50:28 UTC, Jesse Phillips wrote:
> I think I read somewhere you don't want to use unions like 
> this, but I think it is more because you generally don't want 
> to reinterpret bits.

It is non-portable, since some hardware architectures may use 
different representations (e.g. different byte order on int and 
float).

D claims to follow C, so using unions for type punning is 
ultimately implementation defined.

In C++ using unions for type punning is illegal/undefined 
behaviour, so in C++ you should use memcpy. Memcpy also has the 
advantage of explicitly copying thus avoiding some aliasing 
issues.


More information about the Digitalmars-d-learn mailing list