Bill Baxter wrote:
On Thu, Dec 17, 2009 at 9:28 AM, dsimcha <[email protected]> wrote:
== Quote from Nick Sabalausky ([email protected])'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.
This 'auto ref' stuff and the multi-flavor 'vconst' functions are
basically templates with a known list of instantiations (ref /no-ref,
and const/immutable/plain)
In theory there's no reason you couldn't allow templates to also
create virtual functions, if you limit yourself to listing the
possible instantiations up front.
This notion of templates "restricted enough" to be virtual has been
intensively discussed a couple of years ago between Walter, Bartosz and
myself. Back then it was much less clear where to draw the line, but
right now the idea is well worth revisiting.
Andrei