I need some way to achieve a “reverse template alias”. So I’d use a template typedef to select the correct class to use at compile time. I’d like to do the following:
typedef ClassA Temp<int>;
typedef ClassB Temp<char>;
ClassA and ClassB are not template classes, but I’d like to select the right class through the use of a template. So if a Temp< int > was needed it would actually use ClassA. Is anything like this even possible in C++? I tried the following but it didn’t work.
template<>
typedef ClassA Temp<int>;
template<>
typedef ClassB Temp<char>;
I got the following error in GCC
error: template declaration of ‘typedef’
No,
typedefcan’t define type templates, only types. The two closest things you can do are:so you write just
Temp<int>, but it’s a derived class, not the class itself, orso you can get the
ClassAandClassBthemselves, but you have to writeTemp<int>::Type.