| Thread overview | |||||
|---|---|---|---|---|---|
|
February 08, 2007 Variadic template function? | ||||
|---|---|---|---|---|
| ||||
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 Re: Variadic template function? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Michiel | 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 Re: Variadic template function? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Michiel | 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'.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply