Bug 2302 – offsetof property not working properly

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2008-08-21T23:30:00Z
Last change time
2014-02-24T15:33:18Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
malagana15

Comments

Comment #0 by malagana15 — 2008-08-21T23:30:07Z
Please check these 2 short posts on the newsgroup. You'll notice that offsetof property is not working the way it should or the way it is described in the doc: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=13576 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=13579 Please reply to know it's a bug. DMD 1.030 Thanks for your support.
Comment #1 by malagana15 — 2008-08-26T12:46:46Z
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 ***