why is overload with unsigned __int64 and __int64 ambiguous

2.1k views Asked by At

Using VS2008, why is this OK (not allowed to use 2010).

void assert(int exp, int actual) {if (exp!=actual) printf("assert failed\n");}
void assert(unsigned int exp, unsigned int actual) {if (exp!=actual) printf("assert    failed\n");}

But this is ambiguous.

void assert(__int64 exp, __int64 actual) {if (exp!=actual) printf("assert failed\n");}
void assert(unsigned __int64 exp, unsigned __int64 actual) {if (exp!=actual) printf("assert failed\n");}

Sample error text

d:\my documents\visual studio 2008\projects\classtest\classtest\classtest.cpp(31) : error C2668: 'assert' : ambiguous call to overloaded function
d:\my documents\visual studio 2008\projects\classtest\classtest\classtest.cpp(12): could be 'void assert(unsigned __int64,unsigned __int64)'
d:\my documents\visual studio 2008\projects\classtest\classtest\classtest.cpp(10): or       'void assert(__int64,__int64)'
while trying to match the argument list '(int, int)'

It only gets ambiguous with regards to the 'unsigned' overload. Having and "int" version and an "__int64" version is not ambiguous.

2

There are 2 answers

1
CashCow On BEST ANSWER

Your code is actually using int and int as the parameters. In the first case it has an exact match. In the second case it does not, and it treats int->uint64 and int->int64 as equally valid conversions so it doesn't know which one to pick.

0
Luchian Grigore On

You're getting this because int can be implicitly converted to both __int64 and unsigned __int64.

The following also doesn't compile:

void assert(__int64 exp, __int64 actual) {if (exp!=actual) printf("assert failed\n");}
void assert(unsigned __int64 exp, unsigned __int64 actual){if (exp!=actual) printf("assert failed\n");}

int x = 0;
assert(x,x);

But if x is of type __int64 the ambiguity is solved.

void assert(__int64 exp, __int64 actual) {if (exp!=actual) printf("assert failed\n");}
void assert(unsigned __int64 exp, unsigned __int64 actual){if (exp!=actual) printf("assert failed\n");}

__int64 x = 0;
assert(x,x);\
//compiles