Please note that this issue is related to offsetof for structures. Offsetof for classes have had many bugs but have been fixed.
The thing here is that offsetof for a structure can only be called from a static function and not from anywhere, so, porting some C code to D modules is impossible or full of workarounds.
Comment #2 by malagana15 — 2008-08-27T13:39:52Z
I'm posting a few workarounds to get offsetof to work on structures:
1) Create a static function to return the offset, how you do it will depend on your code. This seems to work fine.
2) Use this template i did, it compiles ok but needs more testing, i call it from a static enviroment and works perfect:
(This try to simulate the Win32 macro FIELD_OFFSET)
template FIELD_OFFSET(alias T)
{
const uint FIELD_OFFSET = T.offsetof;
}
Example:
module x;
const uint DIMOFS_X = FIELD_OFFSET!(DIMOUSESTATE.lX);
const uint DIMOFS_BUTTON1 = FIELD_OFFSET!(DIMOUSESTATE.rgbButtons) + 1;
struct DIMOUSESTATE
{
long lX;
long lY;
long lZ;
byte rgbButtons[4];
}
Comment #3 by dlang-bugzilla — 2009-05-27T05:22:20Z
*** This issue has been marked as a duplicate of issue 899 ***