www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variadic template function?

reply Michiel <nomail hotmail.com> writes:
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!
Feb 08 2007
parent reply Michiel <nomail hotmail.com> writes:
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'.
Feb 08 2007
parent "Daniel Giddings" <daniel.giddings gmail.com> writes:
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'. 
Feb 08 2007