Need help with movement from C to D

Mark Isaacson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 4 21:05:34 PDT 2014


On Monday, 5 May 2014 at 03:57:54 UTC, Andrey wrote:
> Guys, could someone help me with suitable template?
>
> I have C macro, which calculates the offset of the field in a 
> struct:
>
> #define offsetof(type, field)	((long) &((type *)0)->field)
>
> A similar D code is, as far as I know,
>
> type.field.offsetof
>
> Is there an any way to make a corresponding D template?
>
> Thank you!

Something like:

unittest {
   enum offsetof(string type, string field) = mixin(type ~ "." ~ 
field ~ ".offsetof");

   struct StrToBob {
     string str;
     int bob;
   }

   writeln(offsetof!("StrToBob", "bob"));
}

?

If not that then I'm not sure what you're asking for.


More information about the Digitalmars-d-learn mailing list