Consider this code:
#include
//Number1
template
auto max (T1 a, T2 b)
{
std::cout << \"auto max(T
the template argument deduction does not take the return type into account,
Yes. Template argument deduction is performed based on function arguments.
so why
maxis ambiguous and notmax?
Given ::max, for the 1st overload, the 1st template parameter T1 is specified as int, and T2 is deduced as double from the 2nd function argument 4., then the instantiation would be double max(int, double). For the 2nd overload, the 1st template parameter RT is specified as int, T1 is deduced as int from 7, T2 is deduced as double from 4., then the instantiation would be int max(int, double). Overload resolution doesn't consider return type too, the two overloads are both exact match and then ambiguous.
Given ::max, for the 1st overload, the 1st template parameter T1 is specified as double, and T2 is deduced as double from 4., so the instantiation would be double max(double, double). For the 2nd overload, the 1st template parameter RT is specified as double, T1 is deduced as int from 7, T2 is deduced as double from 4., then the instantiation would be double max(int, double). Then the 2nd overload wins in overload resolution because it's an exact match, the 1st one requires the implicit conversion from int to double for the 1st argument 7.