Why Template Function call is ambiguous?

ambiguous call to overloaded function template
call of overloaded is ambiguous
sqrt is ambiguous
error c2668 log ambiguous call to overloaded function
member function templates in c++
template member function
template specialization c++
c++ template class specialization pointer
#include <iostream>
using namespace std;
template <typename T>
T max(T x, T y)
{
    return (x > y) ? x : y;
}
int main()
{
    cout << max(3, 7) << std::endl;
    cout << max(3.0, 7.0) << std::endl;
    cout << max(3, 7.0) << std::endl;
    return 0;
}

I'm Expecting max's Instance here

cout << max(3, 7) << std::endl; // max (int, int)
cout << max(3.0, 7.0) << std::endl; // max (double, double)
cout << max(3, 7.0) << std::endl; // max(int, double)

Then what is the problem ? Why I'm getting

11 25 [Error] call of overloaded 'max(double, double)' is ambiguous

If you view the compile error in full, you'd see why. Here's what gcc 5.2 gives me:

main.cpp: In function 'int main()':
main.cpp:10:21: error: call of overloaded 'max(int, int)' is ambiguous
     cout << max(3, 7) << std::endl;
                     ^
main.cpp:4:3: note: candidate: T max(T, T) [with T = int]
 T max(T x, T y)
   ^
In file included from /usr/local/include/c++/5.2.0/bits/char_traits.h:39:0,
                 from /usr/local/include/c++/5.2.0/ios:40,
                 from /usr/local/include/c++/5.2.0/ostream:38,
                 from /usr/local/include/c++/5.2.0/iostream:39,
                 from main.cpp:1:
/usr/local/include/c++/5.2.0/bits/stl_algobase.h:219:5: note: candidate: constexpr const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
     max(const _Tp& __a, const _Tp& __b)
     ^

Basically, there are two max functions - yours and std::max, which is included by some chain of other #includes from <iostream>. The latter is found by lookup because of your

using namespace std;

Effectively, we have:

template <typename T> T max(T, T);                      // yours
template <typename T> T const& max(T const&, T const&); // std

Neither is better than the other, hence ambiguous. This is a great reason to avoid using namespace std. Or a great reason to not reinvent the wheel when it comes to standard library functions - just use std::max. Or both.


On the other hand, this one

max(3, 7.0)

will fail regardless as a template deduction failure. It would deduce T as int for the first argument, and T as double for the second - but there can only be one T! You would have to explicitly call either max<int>(3, 7.0) or max<double>(3, 7.0) to get around the deduction failure, depending on which of the two arguments you want to cast.

Partial ordering of function templates (C++ only), Therefore, the function call h(q) would also be ambiguous. The compiler uses partial ordering in the following cases: Calling a function template specialization​  How to call a template function in a template class? Template function problem; Template Function Pointers + Inherited Return Types; specialization of a template function in a template class?!?!? why can't a template function have extern "C" linkage? Help: template function specialisation must accept scalar types only

The line

using namespace std;

makes things complicated, for sure. However, even after you remove that line, the problem continues to exist.

The call max(3, 7.0) can be resolved to max<int> or max<double>. To resolve to max<int> a double has to be converted to an int. To resolve to max<double>, an int has to be converted to a double. Since both require a conversion and one conversion cannot be given higher priority than the other, the compiler is not able to resolve which one to use.

You'll have to be explicit which version of max you want to use.

max<int>(3, 7.0) will convert 7.0, a double, to an int.

max<double>(3, 7.0) will convert 3, an int, to a double.

Compiler Error C2668, 'function' : ambiguous call to overloaded function. The specified You can also get this error through template use. If, in the same class, you  In the above example, the is_rvalue_ref_func overloads are obtained by performing precisely this rewrite of the is_rvalue_ref primary template and partial specialization. Why is the function call ambiguous but the class template matching well-defined?

Max is a library function.just change the max identifier for your function to max1 or any other name that is not a function defined any header files. That should solve your problem.

Partial Ordering of Function Templates (C++), C++ defines a partial ordering of function templates to specify which be equally specialized, and an ambiguous call results when you attempt  Function overloading example, when call to function is ambiguous In this example we are overloading a function sum. While overloading we gave different types of arguments in both the functions so its a valid case of overloading but still we get compilation error in this program.

SFINAE: cleaner way to solve ambiguous function call error?, and resulted in a call of overloaded 'func(int)' is ambiguous compiler error: #​include <iostream> #include <type_traits> template<typename T> void func(const​  There is no way to explicitly specify template arguments to overloaded operators, conversion functions, and constructors, because they are called without the use of the function name. The specified template arguments must match the template parameters in kind (i.e., type for type, non-type for non-type, and template for template).

Ambiguous call to overloaded function. I have issues with 2 different , using namespace std; // template function definition // interchanges the values of the two T type parameters template<typename T> void swap(T &x, T &y)  SFINAE: cleaner way to solve ambiguous function call error? OPEN Let's say we want to write a templated function with a specialized version for integral types and a generic version for all other types.

Bogus error C2668: "ambiguous call to overloaded function" for call , Bogus error C2668: "ambiguous call to overloaded function" for call to overloaded function templates with non-type template parameter. Fixed - Pending  Ambiguous virtual function calls (C++ only) You cannot override one virtual function with two or more ambiguous virtual functions. This can happen in a derived class that inherits from two nonvirtual bases that are derived from a virtual base class.

Comments
  • You should try again without using namespace std to remove a source of confusion.
  • @juanchopanza still same..
  • Do it anyway. Fix your code and post the error message.
  • @LetDoit That is not the same error as "call of overloaded max is ambiguous"!
  • @LetDoit That is because you two need two different template parameters as you are using two different types.
  • which is best compiler? I heard allot about GCC how to download and use it?
  • @LetDoit There is no best compiler. I just presented sample output to indicate that compiler output is valuable. If you want to use gcc, you can just google gcc.
  • @LetDoit Why do you think you're getting the incorrect output? That prints 7 three times.
  • I'm expecting about 7.0
  • @LetDoit Wrong expectation.
  • Please, try to format your answer in such a way that it clearly distinguishes code examples from prose. You can enclose code using backticks (`).