Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This case of template function overloading eludes my understanding

#include <iostream>  template<typename T> struct identity {     typedef T type; };  template<typename T> void bar(T) { std::cout << "a" << std::endl; } template<typename T> void bar(typename identity<T>::type) { std::cout << "b" << std::endl; }  int main () {     bar(5); // prints "a" because of template deduction rules     bar<int>(5); // prints "b" because of ...?      return EXIT_SUCCESS; } 

I expected bar<int>(5) to result in an ambiguity, at the very least. What crazy rule about template function overload resolution is involved here?

like image 311
gd1 Avatar asked Jul 13 '15 18:07

gd1


People also ask

What do you mean by overloading of template function?

Template Function Overloading:The name of the function templates are the same but called with different arguments is known as function template overloading. If the function template is with the ordinary template, the name of the function remains the same but the number of parameters differs.

What is the difference between template and function overloading?

overloading is used when we have various functions , doing SIMILAR operations . template is used when we have various functions , doing IDENTICAL operations .


1 Answers

Once we get our candidate functions set (both bars), and then whittle it down to the viable functions (still both bars) we have to determine the best viable function. If there is more than one, we get an ambiguity error. The steps we take to determine the best one are laid out in [over.match.best]:

[A] viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then
— for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,

Both functions take an argument of type int, so both conversion sequences are identical. We continue.

— the context is an initialization by user-defined conversion [...]

Does not apply.

— the context is an initialization by conversion function for direct reference binding (13.3.1.6) of a reference to function type, [...]

Does not apply.

— F1 is not a function template specialization and F2 is a function template specialization, or, if not that,

Both bar<int>s are function template specializations. So we move onto the very last bullet point to to determine the best viable function.

— F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.6.2.

The partial ordering rules basically boil down to us synthesizing new unique types for the arguments of both bar overloads and performing template deduction on the other overload.

Consider the "b" overload first. Synthesize a type typename identity<Unique1>::type and attempt to perform template deduction against T. That succeeds. Simplest cast of template deduction there is.

Next, consider the "a" overload. Synthesize a type Unique2 and attempt to perform template deduction against typename identity<T>::type. This fails! This is a non-deduced context - no deduction can succeed.

Since template type deduction only succeeds in a single direction, the bar(typename identity<T>::type) overload is considered more specialized, and is chosen as the best viable candidate.


bogdan presents another interesting case for looking at partial ordering. Consider instead comparing:

template <typename T> void bar(T, T); // "c" template <typename T> void bar(T, typename identity<T>::type ); // "d"  bar(5,5); bar<int>(5, 5); 

Again, both candidates are viable (this time even without explicitly specifying T) so we look at the partial ordering rule.

For the "c" overload, we synthesize arguments of type UniqueC, UniqueC and attempt to perform deduction against T, typename identity<T>::type. This succeeds (with T == UniqueC). So "c" is at least as specialized as "d".

For the "d" overload, we synthesize arguments of type UniqueD, typename identity<UniqueD>::type and attempt to perform deduction against T, T. This fails! The arguments are of different types! So "d" is not at least as specialized as "c".

Thus, the "c" overload is called.

like image 139
Barry Avatar answered Oct 05 '22 08:10

Barry