Inheriting template methods

Viewed 10954

I have a class similar to the following:

class SomeClass
{
    public:
        template<typename... Args>
        void doSomething(Args && ... args);

        //... other methods etc.
};

However, what I really want is to have two different kinds of SomeClass. Ideally, I would be able to derive from a common interface to make SomeOtherClass, but I need to have a different implementation of doSomething and templated methods cannot be virtual. I could make a templated class, but then every method that takes one of these (and there are many) would themselves have to be templates etc.

The best I've been able to come up with is to implement both types of doSomething in the base class and have that method call a virtual method to determine which to use at runtime.

Is there a better solution?

Further explanation

I have many methods that look similar to this:

void foo(SomeClass * obj);

foo calls obj->doSomething and this all works fine, however I've since realized that I need a different kind of SomeClass but want it to work with these same methods, for example:

class SomeClass
{
    public:
        // This won't work
        template<typename... Args>
        virtual void doSomething(Args && ... args) = 0;

        // ... other common methods
};

class TheFirstType
{
    public:
        template<typename... Args>
        void doSomething(Args && ... args);

        // ... other specific methods
};

class TheSecondType
{
    public:
        template<typename... Args>
        void doSomething(Args && ... args);

        // ... other specific methods
};

The above would be ideal, if it were legal, but virtual methods cannot be templated. I've thus far gotten around this limitation by only having doSomething defined in the base class, but with both the implementation for TheFirstType and TheSecondType separated by an if statement that checks on what type the instance actually is:

template<typename... Args>
void SomeClass::doSomething(Args && ... args)
{
    if (this->type() == FIRST_TYPE) {
        // ... the implementation that should rightfully be part of TheFirstType
    } else if (this->type() == SECOND_TYPE) {
        // ... the implementation that should be part of TheSecondType
    }
}

This seems messy, however, so I was wondering if there is a better way.

3 Answers

What might be helpful in this case is the use of a macro function in the wrapper class, SomeClass. The benefit of macros is text substitution. Normally we can't pass a function with all of its parameters as an object in C++ (at least before C++11, past that, I'm not sure). However, with macros, we can pass just about any type of text. The added benefit here is that it will remove the repeated code of if/else for every function that has this template-virtual conflict.

We could set up the macro as follows:

#define CLASS_SELECT(func) \
do { \
    if (this->type() == FIRST_TYPE) { \
        theFirstType.func; \
    } else if (this->type() == SECOND_TYPE) { \
        theSecondType.func; \
    } \
} while (0)

If you're unfamiliar with the do while encapsulation, look here. The class would use the macro like:

class SomeClass
{
    TheFirstType theFirstType;
    TheSecondType theSecondType;
public:
    template<typename... Args>
    void doSomething(Args && ... args)
    {
        CLASS_SELECT(doSomething (&args...) );
    }
};

In the example above, I instantiated the specialized objects inside SomeClass. I'm not sure if that is the design you would prefer, and if it isn't I'd look into using static getter functions or the like within the macro.

Anyways, I acknowledge my solution might not be ideal, but its the best I've got so far (I have the same issue as you in my code). I will admit the CRTP referenced in other answers is pretty cool, though it didn't fit my needs (having to specify template in parameter won't fly for me either, especially at runtime).

Related