enumerating member offsetofs via foreach over members-tuple
Denis Koroskin
2korden at gmail.com
Fri Nov 14 16:43:20 PST 2008
15.11.08 в 00:30 Steven Schveighoffer в своём письме писал(а):
> "Denis Koroskin" 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
>
> Bug reports without actual errors listed in them (i.e. 'try this and see
> what happens') will probably be ignored. It would do you good to show
> what
> the errors are.
>
>> 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?
>
> Yes, in the code:
>
> foreach(m; this.tupleof)
>
> m is a variable local to the for loop, not a reference to the actual
> tuple
> member.
>
> I'm not sure foreach(ref m; this.tupleof) will work, I haven't done much
> with tuples.
>
> But certainly if ref m does work, the
>
> writefln( cast(char *)&m - cast(char *)this);
>
> should be accurate.
>
> But m.offsetof probably won't, since at that point, m is simply a ref
> local
> variable, not a member of the struct.
>
> I think possibly the only way offsetof works is in the form
> aggregate.member.offsetof. I could be totally wrong.
>
> -Steve
>
>
Ooops, sorry. I *hate* when others do like this but accidentially fell to
the same trap. Thank you!
More information about the Digitalmars-d
mailing list