I have the following snippet of code
#include <iostream>
int main() {
int64_t res;
int some_val = 5;
if(false)
{
res = static_cast<uint32_t>(some_val);
}
else
{
res = -1;
}
std::cout << "first check: " << res << std::endl;
res = (false) ? static_cast<uint32_t>(some_val) : (-1);
std::cout << "second check: " << res << std::endl;
return 0;
}
To my surprise, the stream output is:
first check: -1
second check: 4294967295
whereas I would've expected both checks to return -1, given that they're basically the same exact expression.
I read the documentation and it says that the operator "?" should convert the 2 values to a common type, so I would've expected int64_t to be picked(which would allow for both datatypes to be represented) but this is clearly not the case and instead uint32_t is chosen, which causes the underflow when casted to -1.
Can someone explain to me what's going on here?
The conditional operator converts its operands to a common type. In your case:
The two sides of conditional operator are of type
uint32_tandintrespectively. Assuming thatuint32_tis a type alias forunsigned int, the-1(of typeint) is converted tounsigned int. In other words, the statement is equivalent to:int64_tcan represent all values ofuint32_t, so the conversionuint32_t -> int64_tdoesn't change the value.Converting
inttounsignedin such cases is decision that dates back to C (see also What happens when I mix signed and unsigned types in C++?).