casting issue

Adam D. Ruppe destructionator at gmail.com
Thu Oct 24 12:08:47 PDT 2013


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.


More information about the Digitalmars-d-learn mailing list