Why does it not compile?

bearophile bearophileHUGS at lycos.com
Thu Mar 24 04:19:57 PDT 2011


Morlan:

> I did not ask what to do to compile it. I knew that 54L would do. The problem is
> that in the example I explicitely specify the template parameter as long* so there
> is no reason for the compiler to try and guess T from the type of the function
> argument. There is something wrong here.

The compiler is not guessing the type here. It's just that the type the template is explicitly instantiated with, and the type T of the data, aren't the same. You see it better with this simpler example:


import std.stdio;

void foo(T)(T x) if(is(T == int)) {
    writeln("1");
}

void foo(T)(T x) if(!is(T == int)) {
    writeln("2");
}

void main() {
    foo(1); // 1
    foo(1L); // 2
    foo!(int)(1); // 1
    foo!(long)(1L); // 2
    foo!(long)(1); // error
    foo!(int)(1L); // error
}

Bye,
bearophile


More information about the Digitalmars-d mailing list