Why is this function overloaded with an error in C ++?

#include <iostream> int min(int a, int b){ return a < b ? a : b; } long long min(long long a, long long b){ return a < b ? a : b; } int main(){ int a = 1; long long b = 2; std::cout<<min(a, b); return 0; } 

Compilation Error:

test.cpp: In the function 'int main (): test.cpp: 15: 19: error: calling overloaded' min (int &, long long int &) is ambiguous sob <

But why is not long long min(long long a, long long b) not displayed automatically?

Does auto casting int a to long long a not degrade quality?


 #include <iostream> long long min(long long a, long long b){ return a < b ? a : b; } int main(){ int a = 1; long long b = 2; std::cout<<min(a, b); return 0; } 

This one will work without error compilation.


 #include <iostream> #include <algorithm> #include <functional> int main(){ int a = 1; long long b = 2; std::cout<<std::min(a, b); return 0; } 

In the file included in / usr / include / c ++ / 4.8 / algorithm: 62: 0, from test.cpp: 2: / usr / include / c ++ / 4.8 / bits / stl_algo.h: 4226: 5: note: template _Tp std :: min (std :: initializer_list <_Tp> _Compare) min (initializer_list <_Tp> __l, _Compare __comp) ^ / usr / include / c ++ / 4.8 / bits / stl_algo.h: 4226: 5: note: failure template / replacement argument: test.cpp: 8: 29: note:
mismatched types 'std :: initializer_list <_Tp> and' int std :: soy <

+2
c ++ overloading
source share

No one has answered this question yet.

See similar questions:

2437
Why "use the std namespace;" considered bad practice?
31
Why is it ambiguous to call overloaded ambig (long) and ambig (unsigned long) with an integer literal?
thirteen
error: there is no corresponding function to call 'min (long unsigned int &, unsigned int &)
8
Why is less than the operator accepting different types of parameters, and std :: min not?

or similar:

23498
Why is processing a sorted array faster than processing an unsorted array?
8499
What is the "->" operator in C ++?
4247
The ultimate guide and list of books in C ++
3076
What are the differences between a pointer variable and a reference variable in C ++?
1994
What are the basic rules and idioms for operator overloading?
1675
Why is reading strings from stdin much slower in C ++ than Python?
1138
Why do we need virtual functions in C ++?
729
Function Overloading in Javascript - Best Practices
4
Using this next_combination code
2
Paste with object as key fails to compile?

All Articles