Use generalized template class in specialized template functions
c++, inheritance, templates
Solution
The following code compiled and linked in g++
struct Interface { };
template<class T>
class Benchmark: public Interface, public T {
public:
virtual ~Benchmark() { }
virtual void Execute();
};
template<class S>
struct GenericBench {
GenericBench() { }
S var1, var2, var3;
};
// Specialization of the class
template<class S>
class Benchmark<GenericBench<S> >: public Interface, public GenericBench<S> {
public:
virtual ~Benchmark() { }
virtual void Execute() {
// do things
}
};
int main(int argc, char **argv) {
Benchmark<GenericBench<int> > myBench;
myBench.Execute();
}
Problem
I'm writing a wrapper for some benchmark code and want to execute the same code for every templated class type in an already templated function. There is the benchmark class: ``` template<class T> class Benchmark : public Interface, public T { virtual void Execute(); } ``` And as class T I want to use a type that is basically only there for initializing class variables e.g. ``` template<class S> struct GenericBench { GenericBench(); S var1, var2, var3; }; ``` The question now: is it somehow possible to define a specialized function Execute for every mutation of GenericBench for this kind of class inheritance constelation? ``` template<> void Benchmark<GenericBench>::Execute() { // my benchmark code } ``` A main call would then look something like this: ``` myBench->Execute<GenericBench<int>>(); ```