Variadic templates

Bruno Medeiros brunodomedeiros+spam at com.gmail
Sun Nov 5 07:18:18 PST 2006


Walter Bright wrote:
> See http://www.digitalmars.com/d/variadic-function-templates.html
> 
> Why now? Because it's such a pain to do template programming without 
> them, and because I wanted to have a good signals and slots 
> implementation. That was the last piece needed to make S&S work right 
> (unless I'm way off track with it).
> 
> There's a lot of unexplored territory with the tuples, they should be 
> able to do a lot more than the current rather limited ability.

Cool, it really quite helps the (many) template code out there that had 
to replicate code for each number of parameters it wanted to support.

Also, tuples are quite a good framework to implement compile-time 
reflection upon. For example, something more or less like this:


struct Foo {
   int x, y;
   void func(int a) {}
}
...

   foo = new Foo();
   // Then any kind of aggregate has a membersTuple property where
   // each membersTuple element is an alias to the member
   foo.membersTuple[0]++;   //foo.membersTuple[0] same as foo.x
   foo.membersTuple[1]--;   //foo.membersTuple[1] same as foo.y
   foo.membersTuple[2](42); //foo.membersTuple[2] same as foo.func


Then we could have generic serializers (and other stuff) like:

template serialize( TYPE ) {
   void serialize(TYPE t ) {

     foreach(member; t.membersTuple) {
       // Check to see if the member is a field
       // and not a method or an inner type:
       static if( !is(member) && !is(member == function) ) {
         writeln("member: ", typeid(typeof(member)), " value: ", member);
       }
     }
   }
}


-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list