Код: Выделить всё
#include
#include
template
auto max(T1 a, T2 b) {
return b < a ? a : b;
}
template
RT max(T1 a, T2 b) {
return b < a ? a : b;
}
int main(void) {
max(4, 7.2);
return 0;
}
Код: Выделить всё
: In function 'int main()':
:15:13: error: call of overloaded 'max(int, double)' is ambiguous
15 | max(4, 7.2);
| ~~~~~~~~^~~~~~~~
:5:6: note: candidate: 'auto max(T1, T2) [with T1 = int; T2 = double]'
5 | auto max(T1 a, T2 b) {
| ^~~
:10:4: note: candidate: 'RT max(T1, T2) [with RT = int; T1 = int; T2 = double]'
10 | RT max(T1 a, T2 b) {
| ^~~
Compiler returned: 1
Код: Выделить всё
double max(int a, double b) {
return b < static_cast(a) ? static_cast(a) : b;
}
int max(int a, double b) {
return b < static_cast(a) ? static_cast(a) : b;
}
Почему в этом случае возникает неоднозначность?
Подробнее здесь: https://stackoverflow.com/questions/793 ... le-matches