Preserving const? -- A potential solution

Daniel Keep daniel.keep.lists at gmail.com
Sat Mar 7 23:59:00 PST 2009



Tim M wrote:
> On Sun, 08 Mar 2009 20:12:20 +1300, Daniel Keep
> <daniel.keep.lists at gmail.com> wrote:
> 
>>
>> One issue with this is that template functions can't be virtual.
>>
>> You can work around it, but it's really putting barriers up to easy use
>> of the new const system, which I think is a bad thing.
>>
>>   -- Daniel
> 
> I'd rather have virtual template functions, even if it means I have to
> re-compile all subclasses when the vtbls get modified. Is there any
> technicality causing it to be impossible to implement or just something
> rather tricky?

Tricky?!  /gobsmacked

> class C
> {
>     void fn(T);
> }

T can be anything, and can do anything.

The ONLY way for this to work, as far as I know, is for one of two
things to happen:

1. The compiler has to know every single invocation of fn that takes
place, and instantiate the template for that type for every class that
derives from C.

   This, of course, means that you can't ever load code at runtime
through, say, a dynamic library, because it might try to call a method
that doesn't exist, but is actually defined.

   This also means that the D compilers have to be changed to no longer
use single-file compilation.

   Additionally, you still can't ever have a compiled library with
virtual template members, since there's no way for the library to know
what types it should be compiled with: there's an infinite number!

2. The runtime has to be able to instantiate the template at run-time.
This means you have to store the source for the template in the
executable, along with a full symbol table.  The D runtime then has to
contain a full D compiler, including all libraries and probably a
linker, too.  Remember that the template could contain string mixins, so
you really can't be "clever": you have to include the whole thing.

Saying this is "tricky" is a tremendous understatement.

  -- Daniel



More information about the Digitalmars-d mailing list