Problem with implicit template instantiation

Clemens Hofreither clemens.hofreither at gmx.net
Sun Jul 6 18:56:51 PDT 2008


Hi all.

I'm currently playing around with template metaprogramming in D. I've encountered a problem with implicit instantiation of templates that I don't really understand, and I thought somebody here could shed some light on it. Consider the following code:

template MyType(T)
{
	alias T MyType;
}

void mytest(T)(MyType!(T) x)
{
}

void main()
{
	MyType!(int) x;
	mytest(x);
}

This does not work. I would expect the template mechanism to recognize that the argument to mytest is an instance of MyType!(int) and hence instantiate T with int, but instead I get the errors
template mytest(T) does not match any template declaration
template mytest(T) cannot deduce template function from argument types (int)

I've also tried replacing alias by typedef, with the same results. Instantiating the template explicitly seems to work, but I would first like to know *why* I can't do it like this before I use any workarounds. Any pointers? Thanks in advance.

Oh, and this is DMD v1.029.

Regards,
Clemens.


More information about the Digitalmars-d-learn mailing list