Games people play

Bill Baxter dnewsgroup at billbaxter.com
Wed Oct 4 18:36:49 PDT 2006


Kristian wrote:
> On Wed, 04 Oct 2006 08:04:03 +0300, Walter Bright 
> <newshound at digitalmars.com> wrote:
>> Bill Baxter wrote:
> [snip]
>>> arguments, resulting in repetitive code.  Some form of Variadic 
>>> Templates would be very nice to have there.
>>>  So instead of
>>> template Signal(T1) // for one argument ...
>>> template Signal(T1, T2) // for two arguments ...
>>> template Signal(T1, T2, T3) // for three arguments ...
>>> template Signal(T1, T2, T3, T4) // for four arguments ...
>>> template Signal(T1, T2, T3, T4, T5) // for five arguments ...
>>> template Signal(T1, T2, T3, T4, T5, T6) // for six arguments ...
>>> template Signal(etc...
>>>  You can just have one template
>>> template Signal(...) // for any number of arguments ...
>>
>> That would be better, but the user doesn't see that code, just the 
>> library implementer. So it isn't so bad.
> 
> Hey, library writers are people too! ;)
> 
> Actually I think it's absurd to divide people into two groups: ones 
> writing libraries and ones using them. If you write something more 
> complex than tiny programs, you're gonna need all the same features and 
> tricks than library writers. Eventually.

Amen to that!

But I think Walter's point is more that it's encapsulated.  After you 
write that gobbledygook once,  it becomes an implementation detail you 
can forget about.  At least for that one library.

Contrast that with something like the hacks for adding OO support to C. 
  Yes you can get inheritance and polymorphism, but you're still stuck 
with ugly syntax in your face every time you try to *use* it like:
     toolkitWidgetResize(widget, W,H);
instead of
     widget->Resize(W,H);

In the lack-of-variadic-template-args case Walter is arguing that you 
don't see it.  But I take issue with that, because I will indeed see it 
the first time I pass it a bogus parameter and the compiler chokes on 
it.  It's going to point me right at that file with the gobbledygook and 
tell me there's something wrong there.

But here's what Walter's Signal class could look like with variadic 
template parameters.  I've assumed the types of the variadic arguments 
can be accessed by some sort of static type array "_targuments":

template Signal(...) // for any number of arguments
{
     // _targuments.args can serve in place of a list of typed arguments
     //  so this would expand to T1 a, T2 b for Signal(T1,T2)
     void emit( _targuments.args )
     {
         foreach (dg; slots)
             // _targuments.args can be treated like varargs
             // they show up in the _arguments list
             dg(_arguments); //(can you forward arguments like this D?)
     }
     // _targuments.types expands to T1,T2 for Signal(T1,T2)
     void connect( void delegate(_targuments.types) dg)
     {
         slots ~= dg;
     }
     void disconnect( void delegate(_targuments.types) dg)
     {
     for (size_t i = 0; i < slots.length; i++)
     {
         if (slots[i] == dg)
         {
         if (i + 1 == slots.length)
             slots = slots[0 .. i];
         else
             slots = slots[0 .. i] ~ slots[i + 1 .. length];
         }
     }
     }
   private:
     void delegate(_targuments.types)[] slots;
}

I would prefer something user named like here:
http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html
rather than the "magic" variable, but that's a critique that applies to 
regular function varargs too.  Might as well put that in a separate message.

--bb



More information about the Digitalmars-d mailing list