Problem with implicit template instantiation
Bill Baxter
dnewsgroup at billbaxter.com
Sun Jul 6 19:48:10 PDT 2008
Clemens Hofreither wrote:
> 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.
It's just not that smart. It's a specialization so you have to use
specialization syntax. Something like:
void mytest(T : MyType!(S), S) {}
But note that using specialization also disables IFTI.
In D2 you should be able to use a constraint to do something like
void mytest(T) if(is(T : MyType!(S))) {}
My syntax is probably all wrong here, but that's the idea anyway.
Here's a bug marked fixed with a similar goal to yours:
http://d.puremagic.com/issues/show_bug.cgi?id=1661
The discussion may be useful.
--bb
More information about the Digitalmars-d-learn
mailing list