Search code examples
c++templatesinheritanceoverloading

Calling Templated Function Overload of a Parent class with a Derived Class


I have a problem of the following form:

class A {
    ...
};
template <...> class B : public A {
    ...
}

f(A*) {...}
f(C*) {...}
template <...> f(D*) {...}

The issue is that if I call the function f with an argument of type B*, it chooses the generic type D* (which isn't related to A or B at all). I want it to choose the specialization for A*.

Is what I'm trying to do valid, and, if so, why isn't it working?


Solution

  • It seems that what I want to do isn't directly possible; apparently template specialization takes precedence to polymorphism, and working around that isn't possible without Boost or C++11.

    In the end, I solved the problem simply by casting everything to type "A*", before calling "f".

    Thanks everyone,