casting issue

Alexandr Druzhinin drug2004 at bk.ru
Thu Oct 24 12:25:54 PDT 2013


25.10.2013 02:08, Adam D. Ruppe пишет:
> On Thursday, 24 October 2013 at 17:59:03 UTC, Alexandr Druzhinin wrote:
>> foo(cast(float[]) points); // is it safe?
>
> Two options would be to make the points itself be a float[] with the
> names just properties into the index:
>
> struct Point {
>   float[7] data;
>   ref float x() { return data[0]; }
>   ref float y() { return data[1]; }
>   // etc etc etc
> }
>
> then to use it, just pass point.data[] instead of casting.
>
> Or you could also do a union:
>
> struct Point {
>     union {
>        float[7] data;
>        struct {
>           float x,y,z,r,g,b,a;
>        }
>     }
> }
>
> and again, pass point.data[] instead of casting it, while continuing to
> use the other members normally.
>
> These are both well defined so are less likely to break than the cast.
I thought to use union, but using struct with properties is good to 
know. Thanks for sharing!


More information about the Digitalmars-d-learn mailing list