C++ which template would be call if both template function match the argument list

73 Views Asked by At
template<class T>
void fn(T t){}

template<class T>
void fn(std::vector<T> vt){}

void f() {
std::vector<int> vt;
fn(vt);
}

I know that the second template function would be called, but i dont know the rules for template function matching.

1

There are 1 best solutions below

0
leslie.yao On BEST ANSWER

Partial ordering takes place in the overload resolution for a call to a function template specialization.

Informally "A is more specialized than B" means "A accepts fewer types than B".

For this case, the 2nd fn is more specialized and wins in overload resolution, because it accepts types of instantiations of std::vector that is fewer than the 1st one, which could accept all the types.