Thread overview
Template method overloading
Jan 23, 2007
Dominic Letz
Jan 23, 2007
Kirk McDonald
Jan 23, 2007
Frits van Bommel
January 23, 2007
Hello,

I'm trying to overload a template method. But it seems not to produce the expected result, although everything is compiling fine.

My sample is this: (Compiled with win32 dmd version 1.0). I expect:
A is called with 1
B is called with 2
But I got this:
A is called with 1
A is called with 2

the file:
import std.stdio;
class A
{
    void someMethod(T)(T i)
    {
        writefln("A is called with %d", i);
    }
}
class B : A
{
    void someMethod(T)(T i)
    {
        writefln("B is called with %d", i);
    }
}
int main()
{
    A test = new A();
    test.someMethod(1);
    test = new B();
    test.someMethod(2);
    return 0;
}

January 23, 2007
Dominic Letz wrote:
> Hello, 
> 
> I'm trying to overload a template method. But it seems not to produce the expected result, although everything is compiling fine.
> 
> My sample is this: (Compiled with win32 dmd version 1.0). I expect:
> A is called with 1
> B is called with 2
> But I got this:
> A is called with 1
> A is called with 2
> 
> the file:
> import std.stdio;
> class A
> {
>     void someMethod(T)(T i)
>     {
>         writefln("A is called with %d", i);
>     }
> }
> class B : A
> {
>     void someMethod(T)(T i)
>     {
>         writefln("B is called with %d", i);
>     }
> }
> int main()
> {
>     A test = new A();
>     test.someMethod(1);
>     test = new B();
>     test.someMethod(2);     return 0;
> }
> 

Template member functions are implicitly 'final.' This is a consequence of how templates work. They cannot exist in the vtable.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
January 23, 2007
Kirk McDonald wrote:
> Template member functions are implicitly 'final.' This is a consequence of how templates work. They cannot exist in the vtable.

It's a consequence of how templates work, but in combination with how the vtable works. I could imagine a hash-table being added for member templates, which would then be filled at startup time[1]...


[1] Current compiler & (especially) linker technology don't allow this to be done at compile or link time. A linear table could be done at link time in a special section, but that might entail too big a performance penalty for lookup :(.




Note: No, this was not a particularly serious suggestion :P.