It's my understanding that char
may have a different underlying type depending on the architecture.
During implicit casting, an unsigned char
may become and int
or an unsigned int
.
Does that mean the following code has unspecified behaviour?
#include <iostream>
void function(unsigned int){
std::cout << "unsigned\n";
}
void function(int){
std::cout << "signed\n";
}
int main() {
char c;
function(c);
}
I don't get any compiler warnings. Will it always resolve to "signed"?
Your diagram is correct for the case of integer promotion. However, in the code function(c)
, integer promotion does not happen. This is actually overload resolution. If there were only one function
then c
is converted (not promoted) to the type of the parameter, whichever it is.
In the rules for overload resolution, if the argument could be made to match the parameter by integer promotion, then that function would be considered a better match than another function where integer conversion is required to match.
See Table 12 in C++14 (part of section 13.3.3.1.2) which lists the ranks of implicit conversion sequences for overload resolution.
So your code sample would call function(unsigned int)
on a system with CHAR_MAX > INT_MAX
.
However the sizes of types are implementation-defined, not unspecified.