The code is from https://en.cppreference.com/w/cpp/language/template_argument_deduction, now I add void f(T*)
, and f(&d)
will call the f(T*)
.
(1) Can you explain why f(T*)
is called?
In https://en.cppreference.com/w/cpp/language/function_template it mentions "Template argument deduction takes place after the function template name lookup (which may involve argument-dependent lookup) and before overload resolution. "
The f(T*) is selected because of the 'Exact match' in 'overload resolution', right? So in template argument deduction phase, the f(B) is selected, then in later overload resolution phase, the f(T) is selected and takes over the f(B*), is this correct?
Thanks
(2) What changes should I make to make the call f(&d)
to call the f(B<T>*)
? I also need the f(T*)
one, so f(T*)
has to be remained.
#include <iostream>
using namespace std;
template<class T> struct B { };
template<class T> struct D : public B<T> { };
template<class T> void f(T*) { cout<< "T*"<< endl; }
template<class T> void f(B<T>*) { cout<< "B<T>*"<< endl; }
int main() {
D<int> d;
f(&d);
return 0;
}