www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alias parameters for templates does not accept built-in types

reply Deokjae Lee <asitdepends gmail.com> writes:
import std.stdio;

template T(alias X) {
}

void main() {
    writeln(typeid(typeof(T!int)));
}

dmd 2.048 on linux produces an error for the above code.

Error: template instance T!(int) does not match template declaration T
(alias X)

Built-in primitive types cannot be used as template alias parameters. 
User-defined types like structs or classes work well. I think this is a 
bug and reported it.

http://d.puremagic.com/issues/show_bug.cgi?id=4639

However, I got a reply that the behaviour is intended.
I think the limitation that the built-in types are not accepted as alias 
parameters for templates is not beautiful :(
That's a kind of lack of symmetry and practice :)
I'd like to know the reason of such a limitation.
Aug 13 2010
parent Michal Minich <michal.minich gmail.com> writes:
On Sat, 14 Aug 2010 04:03:15 +0000, Deokjae Lee wrote:

 template T(alias X) {

 Error: template instance T!(int) does not match template declaration T
 (alias X)

 However, I got a reply that the behavior is intended. I think the
 limitation that the built-in types are not accepted as alias parameters
 for templates is not beautiful :( That's a kind of lack of symmetry and
 practice :) I'd like to know the reason of such a limitation.

I would guess that it is because name of built-in types are keywords, while other names are not. But it is possible to overload templates with and without alias: template T(alias X) { pragma(msg, "alias X ->"~ X.stringof); } template T(X) { pragma(msg, "X ->" ~ X.stringof); } ...given the ability to send keyword aliases to template, templates would come closer to macros.
Aug 14 2010