Sorry I didn't know how to find a great title for this question, feel free to edit.
I have a range of classes that have the same role, meaning that they all implement the same interface (set of methods). Consider that they all inherit from the same abstract class (even if it's not the case in the minimal example; it's not the point of this question). These classes are all templated.
I want to define a class taking to templates arguments, say T
and U
, and one of the previous classes A
without template arguments. Internally, this new class will use A<T>
and A<U>
. Is this possible? C++11 answers are welcome, feel free to add the tag if necessary.
The example that fails is here:
#include <iostream>
// ------------------------------------------------------------------------
struct eng_tag {};
struct fr_tag {};
struct sp_tag {};
// First implementation of the "talk interface"
template <class T = eng_tag> struct A
{
void talk() { std::cout<<"Hello\n"; }
};
template <> struct A<fr_tag>
{
void talk() { std::cout<<"Salut\n"; }
};
template <> struct A<sp_tag>
{
void talk() { std::cout<<"Ola\n"; }
};
// Second implementations
template <class T = eng_tag> struct B
{
void talk() { std::cout<<"Bye\n"; }
};
template <> struct B<fr_tag>
{
void talk() { std::cout<<"A bientot\n"; }
};
template <> struct B<sp_tag>
{
void talk() { std::cout<<"Adios\n"; }
};
// etc...
// ------------------------------------------------------------------------
template <class T, class U, class I = A>
struct Wrapper
{
I<T> one() { return I<T>(); }
I<U> two() { return I<U>(); }
};
// ------------------------------------------------------------------------
int main()
{
Wrapper<fr_tag,sp_tag,B> w;
w.one().talk();
w.two().talk();
}