enumerating member offsetofs via foreach over members-tuple

Denis Koroskin 2korden at gmail.com
Fri Nov 14 13:11:38 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?

The following works but ...

struct Test
{
     short s;
     int i;
     float f;
     double d;

     void test()
     {
         foreach (i,m; this.tupleof) {
             // writefln(m.offsetof);
             writefln(cast(char*)&mixin(this.tupleof[i].stringof) -  
cast(char*)this);
         }
     }
}

There is definitely a room for improvement.



More information about the Digitalmars-d mailing list