Zhongkun Ma
Zhongkun Ma

Reputation: 429

Overloading priority between different function templates with identical name

Sorry for the unclear title, feel free to edit it if you find a better one. A related topic has been deeply discussed in Priority between normal function and 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 template 1 function is called at line 1? Is it well defined in the specification?

At line 1, I think the compiler should give an "ambiguous overload" error.

Upvotes: 7

Views: 141

Answers (1)

user1804599
user1804599

Reputation:

B cannot be deduced (no parameter has type B) so template 1 is the only possible overload left.

Upvotes: 5

Related Questions