Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto return type of template and ambiguity

Tags:

I have an overloaded template function:

template<typename T1, typename T2>
auto overMax(T1 a, T2 b)
{
    std::cout << __FUNCSIG__ << std::endl;

    return b < a ? a : b;
}

template<typename RT, typename T1, typename T2>
RT overMax(T1 a, T2 b)
{
    std::cout << __FUNCSIG__ << std::endl;

    return b < a ? a : b;
}

If I call it like this:

auto a = overMax(4, 7.2); // uses first template
auto b = overMax<double>(4, 7.2); // uses second template

everything works perfect, but

auto c = overMax<int>(4, 7.2); // error

causes ambiguous call.

Why is it so with int, and OK which other types?

like image 267
amplifier Avatar asked Jan 15 '20 13:01

amplifier


1 Answers

RT is non deducible, so when not providing it, only template<typename T1, typename T2> auto overMax(T1 a, T2 b) can be called.

When you (partially) provide one template argument, both methods are viable,

but depending of argument, one can be a better candidate:

  • For auto b = overMax<double>(4, 7.2); // uses second template

    Both overMax<double, int, double> and overMax<double, double> are viable.
    But overMax<double, int, double> is exact match
    whereas overMax<double, double> requires int to double conversion.

  • For auto c = overMax<int>(4, 7.2); // Ambiguous call

    Both overMax<int, int, double> and overMax<int, double> are viable.
    But neither is a better match or more specialized, so the call is ambiguous.

like image 177
Jarod42 Avatar answered Oct 18 '22 22:10

Jarod42