Avoiding virtual method in template subclass?
I want to make a template that subclasses a parent but changes just one method.
This should be easy, technically speaking, since it should all be generated code. Virtual method is not necessary, but how do I tell the compiler to see the new method?
So for example:
Code:
template <typename type> class CalcA
{
protected:
long calculate();
};
template <typename type> class CalcB: public CalcA<type>
{
protected:
long calculate();
};
It's forcing me to use virtual methods, but there has to be some way around this. Anyone know?
Re: Avoiding virtual method in template subclass?
You can use the Curiously Recurring Template Pattern to call a member function of the derived class without using virtual functions.
Re: Avoiding virtual method in template subclass?
Thank you, I think that will help.
Re: Avoiding virtual method in template subclass?
Quote:
Originally Posted by
originalfamousjohnsmith
It's forcing me to use virtual methods, but there has to be some way around this. Anyone know?
Why do you want to avoid virtual functions?
Re: Avoiding virtual method in template subclass?
Because of performance and about 2 million other reasons. I don't mind virtual methods but it would be a big mistake here.
Not sure there are any better options for me, though. The suggested solution is a possibility but it's a little complex. Like I said it's two one line methods I want to change, but if I did it that way I'd need to change every place I call those two methods and doing that everywhere will make an unreadable mess.
I may as well spell out the whole problem. I have a vector class, and I want to have it so that it can either use the default allocation or it can use a different allocation method for when it's being used as a system object. However, I don't want things to get too complex, and I don't want to add in extra performance penalties, and I especially don't want its usage to change (ie extra unwieldly syntax every time you create something).
I guess the options are:
1. Preprocessor. Kind of ugly, but I can generate out a duplicate with small changes using preprocessor.
2. File generation. I hate doing this, but it does work.
3. Allocator helper object. Then I have to pass one in somehow, and I just transfer the problem of virtualness. Virtual inline static methods are almost guaranteed to make the compiler go crazy. Or else I have a few static concrete allocator objects, which I have to ensure get initialized before anything else.
4. function pointers. No inlining for certain, then, but at least will work.
5. CRT - kind of like 3, I guess.
6. boolean? I can't use a switch it boils down to same problem with needing to be virtual, but I can stick a boolean in, though it increases class size.
7. something else?
Re: Avoiding virtual method in template subclass?
Quote:
Originally Posted by originalfamousjohnsmith
I have a vector class, and I want to have it so that it can either use the default allocation or it can use a different allocation method for when it's being used as a system object.
Must this decision be made at run time, or can it always be made at compile time?
Re: Avoiding virtual method in template subclass?
Quote:
Originally Posted by
originalfamousjohnsmith
7. something else?
Can't you use templates to make a policy out of the allocation strategy, like
Code:
template <typename AllocPolicy>
class CalcA {
public:
void calculate() {
AllocPolicy::calculate();
}
};
Re: Avoiding virtual method in template subclass?
Ok, yes I guess it's basically a 'policy'. Some of this does not gel well in my brain, but I see how helpful a policy can be now and what it means, and it is sort of like the CRT.
Anyway I end up with code like:
Code:
template <typename type, class AllocationClass=SStandardAllocation> class Array: public SBaseArray, public AllocationClass
{
protected:
type *contents;
long count;
long contiguousSize;
long totalSize;
SSimpleChunk* firstChunk;
inline void* allocate(size_t size)
{
return AllocationClass::allocate(size);
}
inline void deallocate(void* address, size_t size)
{
AllocationClass::deallocate(address, size);
}
Re: Avoiding virtual method in template subclass?
Quote:
Originally Posted by
originalfamousjohnsmith
Some of this does not gel well in my brain,
Well, if you want polymorphism there's either object orientation or generics. If you rule out virtual methods there's only templates left.
Re: Avoiding virtual method in template subclass?
How CRTP help in this case ?
Re: Avoiding virtual method in template subclass?
Quote:
Originally Posted by
Peter_APIIT
How CRTP help in this case ?
Read the link I provided. It explains how you can simulate virtual functions without the runtime overhead using CRTP.