I\'ve got a template class:
template
class TemplateClass
{
//irrelevant what this class does
}
And two classes, one base
It is simple to solve this if you are not opposed to a more general situation:
I would simply add a template parameter to your DoSomething function:
template
void DoSomething(const TemplateClass &tc)
{
}
Of course, this doesn't allow you to directly constrain the parameter T to being a subclass of Base. But it will allow you to not write several overloads of DoSomething.
If your TemplateClass was intended to wrap a value of type T, then there may be a way to constrain to parameter for DoSomething.
Suppose TemplateClass were declared something like
template
class TemplateClass
{
public:
TemplateClass( T value )
: Value(value)
{
}
T Value;
};
Then DoSomething could be written as
template
void DoSomething( const TemplateClass &tc )
{
Base * b = tc.Value;
}
Now, during compilation, the line Base * b = tc.Value will only compile if T is set to a subclass of Base. Otherwise, it will fail due to an invalid conversion.
Of course, this is a very simple example which may ignore some elements of good design. But the principle is there, and this is the only way I am currently aware of the produce such a constraint on a template parameter.