0

I have a misunderstanding about the template type deduction :) as I couldn't figure out how the sample code below works:

template<typename T1, typename T2>
auto max(T1 a, T2 b)
{
    return  b < a ? a : b;
}

template<typename RT, typename T1, typename T2>
RT max(T1 a, T2 b)
{
    return  b < a ? a : b;
}


int main()
{
    auto c = ::max<int>(4, 7.2);
}

So in the main, I have an error saying that the resolution is ambiguous since the two templates fcts are candidate.

So far, what I understand (and hope is correct) is from ::max<int>(4, 7.2); I will have

  • temlplated Fct 1 Signature double int double (T1 will be deduced as int, T2 as double ans return type double).
  • temlplated Fct 2 Signature int int double (RT will be deduced as int, T1 as int and T2 as double).

As I know after type deduction there is no type conversion so from the signatures I would say the second fuction is the candidate.

Any clue for how the two functions are candidates for template argument int ?

Thank you

Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
  • @Nelfeal : in C++ Templates -- the complete guides,the signature of a function is defined : the return type is taken account if the function is generated from a function template – Blood-HaZaRd Nov 09 '18 at 17:28

1 Answers1

2

When you pass a type as argument, then the parameter will not be deduced. Think about it, this would cause all kinds of unexpected things. Hence, for

::max<int>(4, 7.2);

the two canditats are max<int,double> from the first overload and max<int,int,double> from the second, which according to their signature are the same (both take an int and a double as parameters).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • so what about ::max(4, 7.2), what signatures will be for the templated functions ? max and max and then call the 2nd function ? – Blood-HaZaRd Nov 09 '18 at 17:22