I need to find a way to recursively build a class given a set of template arguments so that the class inherits from itself and build a method f for the current first template argument in the list of template arguments and then inherits from itself by passing on the rest of the list.
So, basically I want to achieve the following interface for a class C:
C<T1, T2, T3> c;
c now has methods C::f(T1), C::f(T2) and C::f(T3)
My approach so far was like this:
// primary template
template <class H, class...>
class C {};
// base case where class... is empty
template <class H, class...>
class C<H>
{
public:
void f(const H& h){
// std::cout << typeid(h).name() << "\n";
}
};
// recursive case where T is nonempty
template <class H, class... T>
class C : public C<T...>
{
public:
void f(const H& h){
// std::cout << typeid(h).name() << "\n";
}
};
This does not actually compile, as I get
error: redefinition of 'C' class C : public C
Is my approach basically possible and just a matter of semantically and or syntactically invalid code or does this approach not work in principle?