auto ref

dsimcha dsimcha at yahoo.com
Thu Dec 17 09:28:02 PST 2009


== Quote from Nick Sabalausky (a at a.a)'s article
> Pardon my ignorance, but why is it that templated functions can't be
> virtual?

This is an unfortunate consequence of compilation model leaking out into language
design.  You're supposed to be able to subclass a base class even if you don't
have the full source code to it.  Let's say we have:

class A {
    void doStuff(T)(T arg) {}
}

class B : A {
    void doStuff(T)(T arg) {
        writeln("In B.");
    }
}

And then we do:

B b = new B;
b.doStuff(1);

The instantiation of B.doStuff() with an int would require that the vtable for A
be updated to include doStuff!(int).  This is not possible if we also allow base
classes to be compiled separately from derived classes and the code that uses the
derived class.

The only solution I see would be to completely get rid of separate compilation.
For my personal use, since most of my projects are under 10k lines and take a
negligible amount of time to compile anyhow, I'd be in favor of this.  However,
for larger projects or projects that require things to work based only on
interface, without access to the full source code, this is probably impractical.



More information about the Digitalmars-d mailing list