class scope virtual template functions

janderson askme at me.com
Fri May 30 19:54:14 PDT 2008


Chris Wright wrote:
> janderson wrote:
>> Something like:
>>
>> class A
>> {
>>   templateCallback()() {} //template
>>   TemplateMaker Templet() { return new TemplateMaker(this, 
>> templateCallbackFunc); }
>> }
>>
>>
>> class A : B
>> {
>>   templateCallback2()() {} //template
>>   TemplateMaker Templet() { return new TemplateMaker(this, 
>> templateCallbackFunc2); }
>> }
>>
>>
>> Then the templateMaker would be:
>>
>> class TemplateMaker(class, callback) //Functor/proxy
>> {
>>    TemplateMaker(class, callback) { record of stuff ... }
>>
>>    void opCall(T...)(T arg) { recordedclass->recoredcallback(arg); }
>> }
>>
>> Call it like:
>>
>> this.Templet()!("foo")(arg)
>>
>> //Might simplify to due to D's magic property stuff
>> this.Templet!("foo")(arg)
>>
>> Something like that. Haven't tried it so don't know what massaging 
>> would be needed and if it would work.
>>
>> -Joel
> 
> If TemplateMaker is templated, then you can't return it, just an 
> instantiation of it. This only allows you to access one template through 
> it.
> 
> If TemplateMaker is not templated, then it requires virtual templates in 
> order to work; it's expecting a BaseClass, and even if it got a 
> DerivedClass, it only knows about the template on BaseClass, so that's 
> what it uses.

Sorry, I haven't actually tried this example.  Its an C++ trick I've 
used for other purposes.  I think it looks something like this:

class TemplateMaker
{
    TemplateMakera(delegate) { record of stuff ... } //This might be a 
template too, can't remember. Maybe a reinterpret cast somewhere too.
    void opCall(T...)(T arg) { delegate->recoredcallback(arg); }
}

I'm sure given enough time it could be done in D like C++.  I don't want 
to put in the effort though atm.

-Joel



More information about the Digitalmars-d mailing list