Template Instantiation Bug
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Tue Nov 4 08:55:09 PST 2014
On 11/4/14 11:01 AM, Jonathan Marler wrote:
> In the past I don't get much response when I file a bug in BugZilla so I
> wanted to see if anyone could tell me anything about this issue in the
> forums.
>
> I believe a bug has already been created here:
> https://issues.dlang.org/show_bug.cgi?id=2372
>
> Here's the code to reproduce it:
> --------------------------------------
> import std.stdio;
>
> template Transform(T)
> {
> alias Transform = T;
> }
> void testTransform(T)(Transform!T t)
> {
> }
> void testNoTransform(T)(T t)
> {
> }
> void main(string[] args)
> {
> testTransform(3); // FAILS "cannot deduce function..."
> testTransform!int(3);
>
> testNoTransform(3);
> testNoTransform!int(3);
> }
> --------------------------------------
>
> The test(3) line fails with this error message:
>
> main.d(15): Error: template main.testTransform cannot deduce function
> from argument types !()(int), candidates are:
> main.d(7): main.testTransform(T)(Transform!T t)
>
> Does anyone know anything about this, maybe this is "as designed"?
> Thanks in advance for the info.
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.
People have argued in the past that anything with a simple alias should
be doable. I don't know if that's true or not, but I don't think it
works today. There may be a bug report somewhere on it, I didn't search.
The bug you found is not it.
-Steve
More information about the Digitalmars-d
mailing list