Can't use variadic arguments to functions that use templates

JS js.mdnq at gmail.com
Sun Jul 21 00:22:06 PDT 2013


On Saturday, 20 July 2013 at 18:57:18 UTC, Jesse Phillips wrote:
> On Saturday, 20 July 2013 at 18:27:23 UTC, JS wrote:
>> is there any way to pass t directly to A?
>>
>> template A(T...) doesn't seem work nor does using an alias. 
>> (or at least, when I try to foreach over it, it doesn't work).
>
> template A(size_t L) {
>     enum A = L;
> }
>
> template B(T...) {
> void B(T b) {
>     import std.stdio;
>     foreach(Type; T) {
>         pragma(msg, Type.stringof);
>     }
>     foreach(v; b) {
>         writeln(v);
>     }
> }}
>
> void foo(T...)(string s, T t) {
>     auto n = A!(t.length);
>     B(t);// t is runtime information
>          // T is compile time information
> }
>
> void main() {
>     foo("x", 1, 2);
> }

I know I tried that but:

import std.cstream, std.stdio;


template B(T...) {
	string B(T b) {
		string s;
		foreach(Type; T) pragma(msg, Type.stringof);
		foreach(v; b) s ~= v.stringof;
		return s;
	}
}

void foo(T...)(T t)
{
     pragma(msg, B(t));
}

void main() {
     foo("x", "a", "b");
	din.getc();
}


does work. I need to have B generate compile time code so it is 
efficient. Your method calls an actual function at runtime so it 
is nearly as fast as it can be.



More information about the Digitalmars-d-learn mailing list