2

When I have a couple of partial specializations of template classes, and I instantiate a object of that template class, TAD tries to find the most specialized version of the class that fits the template arguments,and that class gets instantiated, that is how I understood it.

But what happens with functions, when there are several overloaded template functions with the same number of function parameters, and arbitrary number of template parameters. How is a function call resolved, are all the functions that can be matched with TAD sent to overload resolution, or is only the most specialized function sent to overload resolution?

For example, how does the compiler know to call the overload for vector *?

template <typename A> void f(vector<A> *g){}

template <typename B> void f(B*){}

template <typename C> void f(C){}

int main(){
    vector<int> vec;
    f(&vec);
    return 0;
} 
vladimirm
  • 261
  • 1
  • 2
  • 8
  • 2
    Overload resolution happens first. Your code doesn't contain any explicit specializations, so that's a non-issue. – Kerrek SB May 18 '14 at 11:51
  • Reference: 14.8.2.4 "Deducing template arguments during partial ordering", also 14.5.6.2 "Partial ordering of function templates". – Kerrek SB May 18 '14 at 11:54
  • As @KerrekSB already mentioned, fix your example. – 101010 May 18 '14 at 12:04
  • A surprising example would be if you added the explicit specialization `template <> void f>(vector *)`. ([Live demo](https://ideone.com/rBXlUS).) – Kerrek SB May 18 '14 at 12:06
  • @KerekSB, I read about partial ordering, and from what I understand it is part of TAD for template functions, and when partial ordering is done, it sends one function to overload resolution (the most specialized that matches the function call), is this correct? – vladimirm May 18 '14 at 12:19
  • The other way around. See [over.match.best]/1: Overload resolution between several functions (including function template specializations) happens "first" (using partial ordering is the last step in overload resolution). If there are two functions where the conversion sequences for the arguments are considered "equal" (none is better), only then the partial ordering is taken into account. – dyp May 18 '14 at 12:29
  • See this for a discussion http://stackoverflow.com/questions/7108033/template-specialization-vs-function-overloading – vsoftco May 18 '14 at 23:25

0 Answers0