A templated function gives me the convenience to operate on a variety of types:
template<typename T> void destroy(T* obj) {
delete obj;
}
But at some point I want to do some specialization on a class hierarchy:
class Base { virtual void doSomething(); };
class Derived : public Base {};
void destroy(Base* obj) {
obj->doSomething();
delete obj;
}
The intended specialized function did invoked if I a pass the exact type Base*
, however the rules of overload resolution seems to prefer the generic templated version instead of performing a static up cast if I a pass Derived*
to void detroy()
.
Certainly I can make all the overloaded functions for all possible derived types, but it's less maintainable.
I am using Visual C++ 2008, is there any way around the above problem?