[draft, proposal] Virtual template functions

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue May 29 10:04:14 PDT 2012


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();
}

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!


More information about the Digitalmars-d mailing list