Template alias parameter does not accept types

Anonimous tr1 at google.com
Thu May 9 04:21:04 PDT 2013


On Thursday, 9 May 2013 at 10:59:02 UTC, ref2401 wrote:
> Version D 2.062
> http://dlang.org/template.html#TemplateAliasParameter
> Is is said in the documentation that is's possible but i get 
> compile time error.
>
> template GetString(alias Arg)
> {
> 	enum string GetString = Arg.stringof;
> }
>
> void main(string[] argv)
> {
> 	writeln(GetString!"1234");
> 	writeln(GetString!18);
> 	
> 	writeln(GetString!int); // Error: template instance 
> GetString!(int)
> 				// GetString!(int) does not match template
> 				// declaration GetString(alias Arg)
> 	
> 	readln();
> }

You just can't pass by alias built-in types like int or char

Workaround:

import std.stdio;

template GetString(Arg...)
	if(Arg.length == 1)
{
	enum string GetString = Arg[0].stringof;
}

void main(string[] argv)
{
	writeln(GetString!"1234");
	writeln(GetString!18);
	
	writeln(GetString!int); // Error: template instance 
GetString!(int)

				// GetString!(int) does not match template
				// declaration GetString(alias Arg)
	
	readln();
}


More information about the Digitalmars-d-learn mailing list