enumerating member offsetofs via foreach over members-tuple

Denis Koroskin 2korden at gmail.com
Fri Nov 14 13:04:57 PST 2008


On Fri, 14 Nov 2008 23:55:08 +0300, Denis Koroskin <2korden at gmail.com>  
wrote:

> I'm doing some meta stuff and came across a few problems.
>
> First of is described here:  
> http://d.puremagic.com/issues/show_bug.cgi?id=2454
>
> Second one is slightly different and this time I am less sure whether it  
> is a bug.
> The following code is indetended to print offsets of all its members:
>
> struct Test
> {
>      short s;
>      int i;
>      float f;
>      double d;
>
>      void test()
>      {
>          writefln("expected output:");
>          writefln( cast(char*)&s - cast(char*)this ); //  
> writefln(s.offsetof);
>          writefln( cast(char*)&i - cast(char*)this ); //  
> writefln(i.offsetof);
>          writefln( cast(char*)&f - cast(char*)this ); //  
> writefln(f.offsetof);
>          writefln( cast(char*)&d - cast(char*)this ); //  
> writefln(d.offsetof);
>
>          writefln("actual output:");
>          foreach (m; this.tupleof) {
>              // writefln(m.offsetof); // Error: no property 'offsetof'  
> for type 'short'
>              writefln( cast(char*)&m - cast(char*)this);
>          }
>      }
> }
>
> expected output:
> 0
> 4
> 8
> 16
>
> actual output:
> -52
> -48
> -44
> -40
>
> Am I doing something wrong?

One more found, can anyone explain error message, please? Is it valid or  
not?

template Test()
{
     void test()
     {
         foreach (m; this.tupleof) {
             writefln(typeof(m).stringof); // test.d(8): function  
test.B.Test!().test cannot access frame of function test
         }
     }
}

struct A
{
     float i;
     mixin Test!();
}

struct B
{
     A a;
     mixin Test!();
}



More information about the Digitalmars-d mailing list