Virtual templated functions. Previously: Preserving const? -- A potential solution
Tim M
a at b.com
Sun Mar 8 03:38:13 PDT 2009
On Sun, 08 Mar 2009 23:12:31 +1300, Daniel Keep
<daniel.keep.lists at gmail.com> wrote:
> That's how I think: it's easy to come up with simple cases where a
> design works. The trick is to come up with a design that works even for
> the complex cases.
What I am suggesting has strong importance without any changes to syntax,
or any changes that may brake existing code.
>
> It's somewhat hypocritical to say "the compiler [needing to know] every
> possible call doesn't make that much sense" and then require the
> compiler know of every possible subclass of a given class.
That argument has no validity as non templated functions have been working
as virtual for years.
>
> Avoiding dynamic instantiation, you HAVE to know what template instances
> are going to be needed at compile time. You can't wave your hands and
> make it go away.
I'm not saying guess the template instance. I'm saying use the same exact
types for instance in class A to instantiate template in class B where the
signature is exactly the same. There is absolutely no guess work as
everything is KNOWN.
> OK: an instance has a pointer to its vtable. Virtual method resolution
> works by assigning each method an offset in this table at compile time.
> When called, the method's slot is found and the method's address is
> read out.
>
> So in order to have virtual templated members, you have to stuff the
> instantiations into the vtable, or something like the vtable, somewhere.
Don't worry the class won't feel a thing as long as the subclasses are
recompiled. This is required for non templated functions anyway but a
different subject for another day.
>
> Let's take your example: the compiler knows all subclasses. This
> requires all-at-once compiling to be mandated by the language; any given
> file will depend not only on the modules it directly references, but all
> modules that subclass any classes it references. It also means you
> cannot ever have classes subclassed by an external library, since these
> will be unavailable at compile time.
There is really no extra complexities that non template functions already
deal with.
PS: .net is in a whole different ball game and is storred in a common
intermediate language not source but that kind of a high level is still
not required to have a virtual templated function.
More information about the Digitalmars-d
mailing list