www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - offsetof

reply Heinz Traub <malagana15 yahoo.es> writes:
Hi,

I've been doing Direct Input code and need to port C to D. I'm trying to get
the offset of a struct member
(http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset) but when i do this:

module dinputd;
struct DIMOUSESTATE
{
	long lX;
	long lY;
	long lZ;
	byte rgbButtons[4];
}

...
module dinputdriver;
case DIMOUSESTATE.lX.offsetof:

I get the following error:

Error: this for lX needs to be type DIMOUSESTATE not type dinputdriver.Mouse.

if i call offsetof in dinputd(the same module where DIMOUSESTATE struct is
declared) i get the following error:

dinputd.d(89): Error: no property 'lX' for type 'DIMOUSESTATE'

Damn! i'm doing everything as specified by the docs. But i can't get it to work.

Can someone help me please. Thanks in advance.

Heinz
Aug 21 2008
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Heinz Traub" wrote
 Hi,

 I've been doing Direct Input code and need to port C to D. I'm trying to 
 get the offset of a struct member 
 (http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset) but when i do 
 this:

 module dinputd;
 struct DIMOUSESTATE
 {
 long lX;
 long lY;
 long lZ;
 byte rgbButtons[4];
 }

 ...
 module dinputdriver;
 case DIMOUSESTATE.lX.offsetof:

 I get the following error:

 Error: this for lX needs to be type DIMOUSESTATE not type 
 dinputdriver.Mouse.

 if i call offsetof in dinputd(the same module where DIMOUSESTATE struct is 
 declared) i get the following error:

 dinputd.d(89): Error: no property 'lX' for type 'DIMOUSESTATE'

 Damn! i'm doing everything as specified by the docs. But i can't get it to 
 work.

 Can someone help me please. Thanks in advance.
This code compiles: module dinputd; struct DIMOUSESTATE { long lX; long lY; long lZ; byte rgbButtons[4]; } int main(char[][] args) { switch(args.length) // just to have a non-constant integer { case DIMOUSESTATE.lX.offsetof: break; } } This does not: module dinputd; struct DIMOUSESTATE { long lX; long lY; long lZ; byte rgbButtons[4]; } class Mouse { this(int i) { switch(i) { case DIMOUSESTATE.lX.offsetof: break; } } } With a similar error as what you said originally. I think this is a bug in DMD. You should file a bug report: http://d.puremagic.com/issues/enter_bug.cgi?product=D I can't think of a sensible workaround (besides saving the offsets in a static function, then using those in the member function). -Steve
Aug 21 2008
parent Heinz Traub <malagana15 yahoo.es> writes:
Steven Schveighoffer Wrote:

 "Heinz Traub" wrote
 Hi,

 I've been doing Direct Input code and need to port C to D. I'm trying to 
 get the offset of a struct member 
 (http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset) but when i do 
 this:

 module dinputd;
 struct DIMOUSESTATE
 {
 long lX;
 long lY;
 long lZ;
 byte rgbButtons[4];
 }

 ...
 module dinputdriver;
 case DIMOUSESTATE.lX.offsetof:

 I get the following error:

 Error: this for lX needs to be type DIMOUSESTATE not type 
 dinputdriver.Mouse.

 if i call offsetof in dinputd(the same module where DIMOUSESTATE struct is 
 declared) i get the following error:

 dinputd.d(89): Error: no property 'lX' for type 'DIMOUSESTATE'

 Damn! i'm doing everything as specified by the docs. But i can't get it to 
 work.

 Can someone help me please. Thanks in advance.
This code compiles: module dinputd; struct DIMOUSESTATE { long lX; long lY; long lZ; byte rgbButtons[4]; } int main(char[][] args) { switch(args.length) // just to have a non-constant integer { case DIMOUSESTATE.lX.offsetof: break; } } This does not: module dinputd; struct DIMOUSESTATE { long lX; long lY; long lZ; byte rgbButtons[4]; } class Mouse { this(int i) { switch(i) { case DIMOUSESTATE.lX.offsetof: break; } } } With a similar error as what you said originally. I think this is a bug in DMD. You should file a bug report: http://d.puremagic.com/issues/enter_bug.cgi?product=D I can't think of a sensible workaround (besides saving the offsets in a static function, then using those in the member function). -Steve
Thanks 4 your post, i'll report this bug. I'm using dmd 1.030 for your knowledge.
Aug 21 2008
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Heinz Traub" <malagana15 yahoo.es> wrote in message 
news:g8kft9$62$1 digitalmars.com...
 Hi,

 I've been doing Direct Input code and need to port C to D. I'm trying to 
 get the offset of a struct member 
 (http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset) but when i do 
 this:

 module dinputd;
 struct DIMOUSESTATE
 {
 long lX;
 long lY;
 long lZ;
 byte rgbButtons[4];
 }

 ...
 module dinputdriver;
 case DIMOUSESTATE.lX.offsetof:

 I get the following error:

 Error: this for lX needs to be type DIMOUSESTATE not type 
 dinputdriver.Mouse.

 if i call offsetof in dinputd(the same module where DIMOUSESTATE struct is 
 declared) i get the following error:

 dinputd.d(89): Error: no property 'lX' for type 'DIMOUSESTATE'

 Damn! i'm doing everything as specified by the docs. But i can't get it to 
 work.

 Can someone help me please. Thanks in advance.

 Heinz
It's actually exactly the opposite from how it's defined in the spec: you can only use offsetof on instances of user-defined types, not on the type itself. It makes _absolutely_ no sense, I know. I can't help but feel it's a bug, but it's been this way for over 4 years.
Aug 21 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:g8kh8p$6c4$1 digitalmars.com...

 It's actually exactly the opposite from how it's defined in the spec: you 
 can only use offsetof on instances of user-defined types, not on the type 
 itself.

 It makes _absolutely_ no sense, I know.  I can't help but feel it's a bug, 
 but it's been this way for over 4 years.
I take that back, it used to be this way but something seems to have changed.
Aug 21 2008
parent reply Heinz Traub <malagana15 yahoo.es> writes:
Jarrett Billingsley Wrote:

 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
 news:g8kh8p$6c4$1 digitalmars.com...
 
 It's actually exactly the opposite from how it's defined in the spec: you 
 can only use offsetof on instances of user-defined types, not on the type 
 itself.

 It makes _absolutely_ no sense, I know.  I can't help but feel it's a bug, 
 but it's been this way for over 4 years.
I take that back, it used to be this way but something seems to have changed.
Yes sir, it used to be this way. I also checked it in your engine (nonagon).
Aug 21 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Heinz Traub" <malagana15 yahoo.es> wrote in message 
news:g8kivv$b19$1 digitalmars.com...
 Jarrett Billingsley Wrote:

 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message
 news:g8kh8p$6c4$1 digitalmars.com...

 It's actually exactly the opposite from how it's defined in the spec: 
 you
 can only use offsetof on instances of user-defined types, not on the 
 type
 itself.

 It makes _absolutely_ no sense, I know.  I can't help but feel it's a 
 bug,
 but it's been this way for over 4 years.
I take that back, it used to be this way but something seems to have changed.
Yes sir, it used to be this way. I also checked it in your engine (nonagon).
Well how about that! I couldn't help but think DIMOUSESTATE sounded familiar.
Aug 21 2008
parent reply "Tim M" <a b.com> writes:
Did you find a sollution? I need to get the offset of a member of a struct  
so I can port a .h to a .di but dmd complains that the member doesn't  
exist on the struct. I dont think anyone has ever tried the code at:  
http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset

On Fri, 22 Aug 2008 10:25:05 +1200, Jarrett Billingsley  
<kb3ctd2 yahoo.com> wrote:

 "Heinz Traub" <malagana15 yahoo.es> wrote in message
 news:g8kivv$b19$1 digitalmars.com...
 Jarrett Billingsley Wrote:

 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message
 news:g8kh8p$6c4$1 digitalmars.com...

 It's actually exactly the opposite from how it's defined in the spec:
 you
 can only use offsetof on instances of user-defined types, not on the
 type
 itself.

 It makes _absolutely_ no sense, I know.  I can't help but feel it's a
 bug,
 but it's been this way for over 4 years.
I take that back, it used to be this way but something seems to have changed.
Yes sir, it used to be this way. I also checked it in your engine (nonagon).
Well how about that! I couldn't help but think DIMOUSESTATE sounded familiar.
Nov 07 2008
parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Fri, Nov 7, 2008 at 8:59 PM, Tim M <a b.com> wrote:
 Did you find a sollution? I need to get the offset of a member of a struct
 so I can port a .h to a .di but dmd complains that the member doesn't exist
 on the struct. I dont think anyone has ever tried the code at:
 http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset
Ehh, that code works with DMD 1.036, dude. What compiler are you using?
Nov 07 2008
parent reply "Tim M" <a b.com> writes:
I tried 1.036 and had the same problem. I'm using the offsetof and sizeof  
in a few constants which I've defined at the beginning of the file so I  
tried moving to the end of the file and it is no longer in error. I read  
somewhere that you don't have to forward declare everything in D like you  
do in C and also dmd wasn't reporting any undefined type errors,  just  
that the property didn't exist on it. Possible a bug?

On Sat, 08 Nov 2008 16:03:08 +1300, Jarrett Billingsley  
<jarrett.billingsley gmail.com> wrote:

 On Fri, Nov 7, 2008 at 8:59 PM, Tim M <a b.com> wrote:
 Did you find a sollution? I need to get the offset of a member of a  
 struct
 so I can port a .h to a .di but dmd complains that the member doesn't  
 exist
 on the struct. I dont think anyone has ever tried the code at:
 http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset
Ehh, that code works with DMD 1.036, dude. What compiler are you using?
Nov 07 2008
parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Fri, Nov 7, 2008 at 11:38 PM, Tim M <a b.com> wrote:
 I tried 1.036 and had the same problem. I'm using the offsetof and sizeof in
 a few constants which I've defined at the beginning of the file so I tried
 moving to the end of the file and it is no longer in error. I read somewhere
 that you don't have to forward declare everything in D like you do in C and
 also dmd wasn't reporting any undefined type errors,  just that the property
 didn't exist on it. Possible a bug?
Oh, yeah, that'll do it. It's definitely a bug. Rather, it's an entire class of bugs. The thing is resolving forward references can be kind of tricky. But it's pretty much inexcusable at this point for the compiler not to be able to do so. Walter's view is that fixing forward reference bugs would not give enough benefit to be worth it. Pshuh. My view is that Walter only cares about the newest, coolest features that D has that no other language does. And as soon as one thing gets implemented, it's onto the next. Nothing is ever polished, and old bugs stagnate. Sigh. It's hard to build a castle on sandy footing.
Nov 07 2008