Template Codegen + Tupleof

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Feb 6 02:52:20 PST 2007


Kyle Furlong wrote:
> So I've been trying to write a function that will yield an array of 
> structs containing the types and offsets of each member of a UDT. 
> Naturally, I looked to use tupleof and templating to pull this off.
> 
> In the process, I think I've come across a bug. Although, since I don't 
> regularly program in D, I'm not sure if what I'm trying to do is the 
> Right Way. Its also why I'm not posting a Bugzilla on this just quite yet.
> 
> The attachment is a bit lengthy because I couldnt reduce it any more 
> than that. Let me know what you think.

That's a definite bug. Wrong code generation for the second template 
instantiation. It seems to think your second struct has the same member 
types as the first one. The tupleof reports the right tuple, though, so 
the bug seems to be in the code that implements the static foreach.

More reduced version:
-----
void Fields(C)()
{
     foreach(i, a; typeof(C.tupleof))
     {
         // fails for second instantiation:
         // "test.d(7): static assert  (is(int == real)) is false"
         static assert(is(typeof(a) == typeof(C.tupleof)[i]));
     }
}

struct MyStruct1 {
     int afield;
}

struct MyStruct2 {
     real afield;
}

void main() {
     Fields!(MyStruct1);
     Fields!(MyStruct2);
}
-----


P.S. is there any particular reason you're computing offsets manually 
when you have the type as a template parameter and could just use 
"C.tupleof[i].offsetof" :P ?
That would save you from having to worry about alignment issues, 
interface vtable pointers and more.

Your code also doesn't take base classes into account, but that may just 
be because you reduced it?
If not, be warned that tupleof doesn't seem to return base class 
members. Use of "static if(is(C Base == super))" and recursion should be 
able to help you here.


More information about the Digitalmars-d-bugs mailing list