Priority overload between different function templates with the same name

Sorry for the fuzzy name, feel free to edit it if you find a better one. A related topic was deeply discussed in Priority between the normal function and the Template function , but I did not find the answer to my question.

My code is:

template<typename T> void f(T t){std::cout << "Template 1" << std::endl;} // template 1 template<typename T, typename B> void f(T t){std::cout << "Template 2" << std::endl;} // template 2 int main () { f(1); // line 1, template 1 will be called f<int>(1); // template 1 will be called f<int,int>(1); // template 2 will be called } 

What is the possible reason that the function of template 1 is called on line 1? Is it well defined in the specification?

On line 1, I think the compiler should give a "ambiguous overload" error.

+7
c ++ overloading templates
source share
1 answer

B cannot be inferred (the parameter is not of type B ), so pattern 1 is the only possible overload on the left.

+5
source share

All Articles