Casting Structs

Paul D Anderson via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 1 10:35:21 PDT 2014


This has been discussed on the learning forum but I wanted to 
bring this to a wider audience.

Apparently structs can be cast to other structs as long as 
they're the same size. In my case I have a decimal struct which 
is parameterized with the precision, max exponent, rounding mode, 
etc.:

     struct Decimal!(99,999,Rounding.HALF_EVEN)

can be cast to

     struct Decimal!(16,259,Rounding.FLOOR)

without explicitly overriding the opCast function.

Timon Gehr showed an example of this for non-parameterized 
structs,also.

My question is is this expected behavior? If so, does this affect 
constness? Or type safety?

Using this certainly makes my life easier but is this a feature 
or is it a bug that will be disallowed at some point?

Paul


More information about the Digitalmars-d mailing list