[draft, proposal] Virtual template functions

Dmitry Olshansky dmitry.olsh at gmail.com
Tue May 29 11:04:18 PDT 2012


On 29.05.2012 21:04, Andrej Mitrovic wrote:
> On 5/29/12, Dmitry Olshansky<dmitry.olsh at gmail.com>  wrote:
>> I'm reiterating prior info on the subject and provide some motivating
>> examples. For those convinced of necessity of virtual function templates
>> already - skip to the bottom ;)
>
> I've run into an issue like this, currently I use this sort of workaround:
>
> class Base
> {
>      abstract void run();
>
>      void runAll(this Child)(Child ch)
>      {
>          foreach (i; 0 .. 10)  // demo
>              ch.templateMethod(i);
>      }
> }
>
> class Child : Base
> {
>      override void run()
>      {
>          runAll(this);
>      }
>
>      void templateMethod(T)(T t) { }
> }
>
> void main()
> {
>      auto ch = new Child;
>      ch.run();
> }

Would this work if say:
       Base ch = new Child;
  	ch.run();

Since all this template this parameter stuff is arcane to me :)
In any event it look like doing hand-written thunks. And that's clear 
indication to me that it needs a better hook in compiler.

>
> All subclasses of Base need to implement 'templateMethod', but it has
> to be a templated function. Unfortunately that means it can't be
> virtual so I'm using a 'this' template parameter in a base function
> that has to be called. Not very pretty!

The bloat is the same, and even some extra call overhead if I'm not 
mistaken.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list