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?