www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - property as template parameter

reply Sark7 <sark7 mail333.com> writes:
template foo(int i)
{
   void foo() { }
}

void main()
{
   struct S { int bar; }
   foo!(S.bar.offset)(); // error: identifier 'offset' of 'S.bar.offset' is  
not defined
}
Jul 06 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Sark7 wrote:
 template foo(int i)
 {
   void foo() { }
 }
 
 void main()
 {
   struct S { int bar; }
   foo!(S.bar.offset)(); // error: identifier 'offset' of 'S.bar.offset' 
 is  not defined
 }
I know this bug report has been out a while (so you might have already solved your problem), but I just looked at this. What is "offset"? I initially thought it was a property of int, but I can't find it on that page of the spec. It's not a keyword. I think that's why the compiler is confused. I'm confused, too. If I'm wrong and there is an offset built into the language, please tell me what it is (and where I can find out about it). It might come in handy. -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jul 24 2004
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
you're using a property without knowing what it means?  that's a new one to
me ;)

offset gives you the offset in bytes of a member variable from the start of
a struct.  because structs are left in the order they are, and assuming the
default alignment is 4 bytes, in struct S { int a,b; }, a.offset=0 and
b.offset=4.
Jul 24 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Jarrett Billingsley wrote:
 you're using a property without knowing what it means?  that's a new one to
 me ;)
 
 offset gives you the offset in bytes of a member variable from the start of
 a struct.  because structs are left in the order they are, and assuming the
 default alignment is 4 bytes, in struct S { int a,b; }, a.offset=0 and
 b.offset=4.
Who said I was using it? I'm sometimes careless, but I'm never psychic. ;) I didn't know there was such a thing, and I still can't find a reference to it. Here's what the spec lists as Struct Properties: .sizeof Size in bytes of struct .size Same as .sizeof .alignof Size boundary struct needs to be aligned on As far I can tell there isn't an "offset" property (although "alignof" may be what is desired). import std.stdio; struct S { int bar; } void main() { S myStruct; writef(myStruct.bar); // writef(myStruct.offset); // offset2.d(13): no property 'offset' for type 'S' writef(myStruct.bar.offset); // offset2.d(17): no property 'offset' for type 'int' } -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jul 24 2004
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
odd.  this is from the D docs, in the "converting C to D" section.
Getting the offset of a struct member.
The C Way
Naturally, another macro is used:
           #include <stddef>
           struct Foo { int x; int y; };

           off = offsetof(Foo, y);
The D Way
An offset is just another property:
           struct Foo { int x; int y; }

           off = Foo.y.offset;
but the compiler doesn't recognize it?
Jul 24 2004
parent J C Calvarese <jcc7 cox.net> writes:
Jarrett Billingsley wrote:
 odd.  this is from the D docs, in the "converting C to D" section.
 Getting the offset of a struct member.
 The C Way
 Naturally, another macro is used:
            #include <stddef>
            struct Foo { int x; int y; };
 
            off = offsetof(Foo, y);
 The D Way
 An offset is just another property:
            struct Foo { int x; int y; }
 
            off = Foo.y.offset;
 but the compiler doesn't recognize it?
Okay. I finally got the compiler to recognize offset. So I agree now that the original post does correctly identify a bug. I'm not sure if the bug is a poor error message or if the code is legal, but I'd call it a bug. I'm going to post a new bug complaining about how offset is essentially undocumented (yes, it's on ctod.html, but I think it should be on the struct page, too). import std.stdio; struct S { int bar; } void main() { S myStruct; // writef(myStruct.offset); // offset2.d(13): no property 'offset' for type 'S' // writef(myStruct.bar.offset); // offset2.d(16): no property 'offset' for type 'int' writefln(S.bar.offset); writefln(typeof(myStruct).bar.offset); } -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jul 25 2004