Consider that the sign (+1
or -1
) is known and there is a code that parses unsigned integer. That unsigned integer can be equal to -numeric_limits<int64_t>::max()
. How to correctly compare without triggering undefined behavior?
int8_t sign = /* +1 or -1 */;
uint64_t result = /* parse the remaining string as unsigned integer */;
if( result > uint64_t(numeric_limits<int64_t>::max()))
{
if(sign == 1) return false; // error: out of range for int64_t
// Is the below code correct or how to implement correctly its intent?
if(result == uint64_t(-numeric_limits<int64_t>::min()))
{
return true;
}
return false;
}