3
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.

4

1 回答 1

2

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.

于 2020-06-11T02:42:29.570 回答