class scope virtual template functions

janderson askme at me.com
Thu May 29 20:48:19 PDT 2008


BCS wrote:
> Reply to Walter,
> 
>> BCS wrote:
>>
>>> However in highly limited cases, this is not an issue.
>>> 1) the template members are enumerations (just build the whole set)
>>> 2) the function has no parameters (only one version can be built)
>>> 3) no general form of the template exists, only explicit
>>> specializations
>>> (ditto enum)
>>> 4) class scope aliases of member templates are used. (instance the
>>> template and stuff it in as a normal function)
>> True, but now you have the issue that minor, seemingly innocuous
>> changes to a template function can have dramatic changes to its
>> behavior. It's a lot easier to understand "template functions are not
>> virtual" than "template functions are not virtual except in these
>> rather complex scenarios."
>>
>> If you need virtual behavior from a template function, the best way is
>> to wrap a virtual function call within it.
>>
> 
> in my case, I need this syntax
> 
> this.Templet!("foo")(arg)
> 
> to be virtual.
> 
> The use case is inside of dparse where I have template code calling 
> template code.
> 
> 

You could use a functor/proxy type thing.  That is return a separate 
object for each class level that has opCall overloaded.  The opCall 
could be templated.  The functor object probably could be generalized so 
that you could use it in any case you needed a virtual function (it 
could callback its owner by template or maybe delegate).

Of course that's more complex then a simple template virtual inheritance 
however it does provide more functionality as well.

-Joel



More information about the Digitalmars-d mailing list