Thread overview
Variadic template function?
Feb 08, 2007
Michiel
Feb 08, 2007
Michiel
Feb 08, 2007
Daniel Giddings
February 08, 2007
I need a function like this (I simplified the actual function):

void foo(T)(T[] elements ...) { /* code */ }

And while it compiles fine by itself, I can't (for example) use it like this:

foo(1, 2, 3, 4, 5);

I get the following compiler messages:

main.d(27): template tools.Set.set(T) does not match any template declaration
main.d(27): template tools.Set.set(T) cannot deduce template function from
argument types (int,int,int,int,int)

That's a shame. Any idea how I can make this work? I want to use it to create a sort of set literal for my set implementation.

Thanks!
February 08, 2007
Eh, I changed the function name in the post to 'foo' to make it more general (or no reason at all, really). But I didn't change the error message accordingly. The function name was originally 'set'.
February 08, 2007
both of the following should work:

import std.stdio;

void foo(T, R... )( T element, R remainder )
{
 writefln( element );
 static if( remainder.length > 0 )
  foo( remainder );
}

void bar(T...)( T element )
{
 foreach( e; element )
  writefln( e );
}

void main()
{
 foo(1,2,3,4,5);
 bar(1,2,3,4,5);
}

"Michiel" <nomail@hotmail.com> wrote in message news:eqest9$2iuk$1@digitaldaemon.com...
> Eh, I changed the function name in the post to 'foo' to make it more
> general (or
> no reason at all, really). But I didn't change the error message
> accordingly. The
> function name was originally 'set'.