Virtual templated functions. Previously: Preserving const? -- A potential solution

Daniel Keep daniel.keep.lists at gmail.com
Sun Mar 8 04:43:42 PDT 2009



Tim M wrote:
> On Sun, 08 Mar 2009 23:12:31 +1300, Daniel Keep
> <daniel.keep.lists at gmail.com> wrote:
>>
>> 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.

That doesn't have any bearing on this.

>> 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.

Virtual functions do NOT require the compiler to know about all
subclasses.  Subclasses override the entries in the vtable, which only
requires the subclass to know about the superclass, not vice versa.

In order to be able to instantiate the template member for B, the
compiler MUST KNOW ALL SUBCLASSES OF A.  It cannot know all subclasses
of A unless you feed it the entire program at compile time.

It cannot know about subclasses which are loaded at run time.  This
means a host application cannot use subclasses in a library, and a
library cannot use virtual template members in any class in the host
application unless it lucks out and they're compiled.

>> 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.

Changing a line in an unrelated module does NOT require an entire class
hierarchy to be recompiled.

>> 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.

If you can't see that, yes, there are extra complexities, then there's
no point in continuing this thread as we're obviously just talking past
each other.

  -- Daniel



More information about the Digitalmars-d mailing list