I want to define a base template class in a way so that it takes variadic template arguments and defines a virtual method for each argument, where the parameter is the argument type.
E.g. Base<int, bool, string>
should give me 3 virtual methods: Foo(int)
, Foo(bool)
, and Foo(string)
.
I tried the following:
template <typename Param>
struct BaseSingle
{
virtual void Foo(Param) {};
};
template <typename... Params>
struct Base : public BaseSingle<Params>...
{
};
Unfortunately, Foo becomes ambiguous. I can't get the using BaseSingle<Params>::Foo...
syntax to work. Is there a way?
I know that, alternatively, I can recursively inherit from BaseSingle and pass in the remaining params. Are there perf implications of that?
Here is a suggestion that requires exact type matching:
But IMO your own suggestion using recursive inheritance sounds more elegant.