I have a question related to a template class implementation design.
A template AT can be specialized with template parameters A1 and A2 only:
AT<A1> a;
or
AT<A2> a;
The template has a function that may use B1 and B2 classes. Specifically, when the template is specialized with A1 class, the function should use B1 and for A2, B2 should be used.
For example:
template< class T > class AT : public A
{
int size;
public:
int f()
{
if ( dynamic_cast<A1*> this != 0 ) {
size = sizeof( B1 );
}
else {
size = sizeof( B2 );
}
}
...
};
As B1 and B2 are classes related to internal A1 and A2 implementation, it is preferably to not make end user aware about their existence, so specialization of the template like
AT<A1, B1> a;
is not acceptable.
What is the best way to design such template class and permit internal differentiation based on the class that the template was specialized with?
Thank you!
You can create a simple type trait to map the types it:
And then use it internally: