offsetof + foreach

Kenji Hara k.hara.pg at gmail.com
Sat Sep 8 00:38:43 PDT 2012


On Friday, 7 September 2012 at 17:32:43 UTC, Ellery Newcomer 
wrote:
> On 09/07/2012 10:31 AM, Ellery Newcomer wrote:
>> I have a struct buffer, and I want to print out its members' 
>> offsetof.
>>
>> This:
>>
>> foreach(i,_t; buffer.tupleof) {
>>             writefln("%s@: %s", _t.stringof, _t.offsetof);
>>         }
>>
>> complains
>>
>> Error: undefined identifier 'offsetof'
>>
>> what should I be doing?
>
> nevermind, I remember tupleof + foreach has always been broken
>
> writefln("%s@: %s", buffer.tupleof[i].stringof, 
> buffer.tupleof[i].offsetof);

I think this is expected behavior.
In foreach body, _t is a copy of field value, and it's not 
buffer's field itself.

Your code is equivalent with:

     foreach(i,_; buffer.tupleof) {
         auto _t = buffer.tupleof[i];	// _t is a normal variable
         writefln("%s@: %s", _t.stringof, _t.offsetof);
     }

Then you cannot get offsetof property from _t;

Regards.

Kenji Hara


More information about the Digitalmars-d-learn mailing list