struct / cast / ? design problem

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 15 16:51:32 PDT 2015


On Sun, 15 Mar 2015 16:34:14 -0700, Charles Hixson via Digitalmars-d-learn
wrote:

if you know the exact layouts of `spare`, you can use union for that:

struct S {
  // ...
  union {
    ulong[61] spare;
    struct { int vala; ubyte valb; }
    // etc.
  }
}

and then you can use it like this:

  auto s = S();
  s.vala = 42;
  assert(s.spare[0] == 42);

you can also write a CTFE function that builds struct to cast where 
`spare` is changed to something else, using `S` as a source, but this 
solution will not be pretty. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150315/f93b3815/attachment.sig>


More information about the Digitalmars-d-learn mailing list