The compiler selects the best overload it can see. Type deduction instantiates your template as f(B*), and that's clearly a better overload than f(A*). The usual solution for this is something like:
void f( A* p ) { } template <typename T> void doF( T a, A const* ) { f( static_cast<A*>( a ) ); } template <typename T> void doF( T a... ) { // your current template implementation } template <typename T> void f( T a ) { doF<T>( a, 42 ); } template <typename T> void f( T* a ) { doF<T*>( a, static_cast<T*>( 0 ) ); }
The above works because for non-pointer types, the only function which can be called is the first template above, which will end up in your template implementation. For pointer types, the last template is more specialized, and so will be a better match (unless the pointer is A*, in which case, the non-template function will be called); it will pass a null pointer of type T* to doF. If T* converts implicitly to an A*, the first doF is the better match; otherwise, the first doF cannot be called, and so the second will be called.