Template Instantiation Bug

Jonathan Marler via Digitalmars-d digitalmars-d at puremagic.com
Tue Nov 4 09:51:59 PST 2014


On Tuesday, 4 November 2014 at 16:55:10 UTC, Steven Schveighoffer 
wrote:
>
> This is as designed.
>
> The reason is because the compiler cannot deduce *backwards* 
> how to instantiate a template to get the right result.
>
> I ran into this a long time ago when porting Tango.
>
> Here is the counter-case:
>
> template Transform(T)
> {
>     static if(is(T == string)) alias Transform = int;
>     else static if(is(T == int)) alias Transform = string;
>     else alias Transform = T;
> }
>
> Basically, the compiler has to figure out how to instantiate 
> Transform in order for it to result in an int. While it's easy 
> for us to see what it needs to do, it doesn't have that 
> capability.

Ah I see now.  After reading your comment I had to walk through 
how the compiler works out the types but after doing so it makes 
sense.  I've included my thoughts to aid any others with the same 
question.

Given a template signature like this:

void mytemplate(T)(T t)

Whenever a call to the template does not specify the template 
parameter T, the compiler needs to be able to "deduce" the type 
of T.  In the example given above, T is equal to the type of the 
first argument "typeof(t)".

If you were to apply some type of transformation to the argument, 
then the compiler cannot deduce T because it cannot reverse the 
transformation. For example, if you had the following type 
transformation:

template Transform(T)
{
     alias Transform = T;
}

The compiler does not know how to "reverse" this template, 
meaning, given the output of Transform, the compiler cannot 
deduce what the input of Transform was EVEN IF THE TEMPLATE IS AS 
SIMPLE AS THIS ONE.

I hope this helps for anybody else with the same question.


More information about the Digitalmars-d mailing list