Template alias parameter does not accept types

Steven Schveighoffer schveiguy at yahoo.com
Thu May 9 06:27:33 PDT 2013


On Thu, 09 May 2013 06:58:57 -0400, ref2401 <refactor24 at gmail.com> 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();
> }

Others have answered, but the technical issue is that alias accepts a  
*symbol*, int is a *keyword*.

Annoying, I know, but I don't see how the compiler can make sense of it,  
the way it is built.  Keywords are special and must go in the right places.

You can alias int, and it works:

alias int intalias;

writeln(GetString!intalias); // writes "intalias"

You can also overload GetString to accept builtin types:

template GetString(T) if (is(T == int) || is(T == float) || ...)
{
    enum string GetString = T.stringof;
}

There may even already be a template for detecting a builtin type, not  
sure.

On Thu, 09 May 2013 07:19:37 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Template alias arguments don't yet accept built-in types as int. It will  
> be fixed.

I don't think this is true, alias strictly accepts symbols or literals,  
not keywords.  Do you have evidence of an official statement to the  
contrary?

-Steve


More information about the Digitalmars-d-learn mailing list