auto ref

Nick Sabalausky a at a.a
Fri Dec 18 09:21:01 PST 2009


"dsimcha" <dsimcha at yahoo.com> wrote in message 
news:hgdpn2$t1c$1 at digitalmars.com...
> == 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.

But we already can't instantiate templates without the source anyway, so I 
don't see how doing that vtable update for A would create any additional 
requirements on source availability that we don't already have, regardless 
of whether or not the function is virtual. Either way, the issue of "Do I 
need to provide the source?" still boils down to just "Am I defining a 
public template?" and is not at all dependent on "Is this going to be 
externally subclassed?".





More information about the Digitalmars-d mailing list