c++templatesoverload-resolutiontemplate-argument-deductionpartial-ordering

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


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.


Solution

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