Partial ordering of function templates (C++ only)

A function template specialization might be ambiguous because template argument deduction might associate the specialization with more than one of the overloaded definitions. The compiler will then choose the definition that is the most specialized. This process of selecting a function template definition is called partial ordering.

A template X is more specialized than a template Y if every argument list that matches the one specified by X also matches the one specified by Y, but not the other way around. The following example demonstrates partial ordering:

template<class T> void f(T)  { }
template<class T> void f(T*) { }
template<class T> void f(const T*) { }

template<class T> void g(T) { }
template<class T> void g(T&) { }

template<class T> void h(T) { }
template<class T> void h(T, ...) { }

int main() {
   const int *p;
   f(p);

   int q;
//  g(q);
//  h(q);
}

The declaration template<class T> void f(const T*) is more specialized than template<class T> void f(T*). Therefore, the function call f(p) calls template<class T> void f(const T*). However, neither void g(T) nor void g(T&) is more specialized than the other. Therefore, the function call g(q) would be ambiguous.

Ellipses do not affect partial ordering. Therefore, the function call h(q) would also be ambiguous.

The compiler uses partial ordering in the following cases:

Related information