Reputation: 50775
I expected this simple code to compile:
void Foo(long a, long);
void Foo(long a, double b);
int main()
{
Foo(1, 2);
Foo(1, 2.0);
}
Compiler output:
prog.cpp: In function ‘int main()’:
prog.cpp:8:11: error: call of overloaded ‘Foo(int, int)’ is ambiguous
Foo(1, 2);
^
prog.cpp:3:6: note: candidate: void Foo(long int, long int)
void Foo(long a, long);
^~~
prog.cpp:4:6: note: candidate: void Foo(long int, double)
void Foo(long a, double b);
^~~
I don't understand why the call is ambigous. For me Foo(unsigned long a, unsigned long)
is clearly a closer match than void Foo(unsigned long a, double b)
for Foo(1, 2)
.
But if I replace long
by int
it compiles:
void Foo(int a, int);
void Foo(int a, double b);
int main()
{
Foo(1, 2);
Foo(1, 2.0);
}
I'm really lost here.
Upvotes: 0
Views: 43
Reputation: 234705
1
and 2
are literals of type int
, and 2.0
is a double
.
So neither overload is an exact match for your types, they are ranked similarly for the purposes of overload resolution, so the compiler is required to issue a diagnostic. (Note that int
and long
must be different types even if they have the same size and complementing scheme.)
You could fix with Foo(1L, 2L);
.
Upvotes: 5