I have the situation that I have an existing template class which works fine for all kind of datatypes. But now I need to specialize it for classes which derive from a specific class. But not the whole class should be specialized but only some functions.
I tried to do it like it is described in this post.
class BaseClass
{
public:
bool DoSomething()
{
return true;
}
};
class SubClass : BaseClass
{
};
template<typename T, typename _Alloc = std::allocator<T>>
class TemplateClass
{
public:
template<typename U = T, typename std::enable_if<
!std::is_base_of<BaseClass, U>::value>::type>
void print_line()
{
std::cout << "Parameter of general Type T" << std::endl;
}
template<typename U = T, typename std::enable_if<
std::is_base_of<BaseClass, U>::value>::type>
void print_line()
{
std::cout << "Parameter of specific Type BaseClass" << std::endl;
}
};
I try to use the template like this:
TemplateClass<BaseClass>* tc1 = new TemplateClass<BaseClass>();
tc1->print_line();
TemplateClass<SubClass>* tc2 = new TemplateClass<SubClass>();
tc2->print_line();
TemplateClass<int>* tc3 = new TemplateClass<int>();
tc3->print_line();
For each function call i get the error there was no fitting method found. Another point is that in this article they say that enable_if shouldn't be used to select between implementations.
Does anyone has an idea what my mistake is or how to do this correctly? Thanks in advance!