I'm reading the standard and trying to figure out why this code won't be resolved without a cast.
void foo(char c) { }
// Way bigger than char
void foo(unsigned long int) { }
int main()
{
foo(123456789); // ambiguous
foo((unsigned long int) 123456789); // works
}
Here's what it says:
4.13 Integer conversion rank [conv.rank]
Every integer type has an integer conversion rank defined as follows:
— The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type.
— The rank of char shall equal the rank of signed char and unsigned char.
In particular, what rustles my jimmies is that it doesn't say ANY unsigned integral type, just unsigned char. My guess is that char is being promoted to an unsigned type via conversion. Is this true?
char
is not being promoted tounsigned
... the issue is as AndreyT says - that 123456789 is anint
and it's not clearly better to truncate it as achar
or pass it as anunsigned long
(long
would be just as bad -unsigned
is not significant here). – Clathrateunsigned char
. – Teide