是否从任何被视为未定义行为的整数中减去 INT_MIN? [英] Is INT_MIN subtracted from any integer considered undefined behavior?
问题描述
如果我有这样的东西怎么办:
What if I have something like this:
int a = 20;
int min = INT_MIN;
if(-a - min)
//do something
假设 INT_MIN 如果正大于 INT_MAX.min 是否会被编译器转换为类似于 -INT_MIN 中的 -min 之类的内容,这可能是未定义的?
Assume that INT_MIN if positive is more than INT_MAX. Would min ever be converted by the compiler to something like -min as in -INT_MIN, which could be undefined?
推荐答案
您是对的,应用于 INT_MIN
的一元减号可以是未定义的,但这在您的示例中不会发生.
You are right that unary minus applied to INT_MIN
can be undefined, but this does not happen in your example.
-a - min
被解析为 (-a) - min
.变量min
只涉及二进制减法,第一个操作数只需要严格为负即可定义结果.
-a - min
is parsed as (-a) - min
. Variable min
is only involved in binary subtraction, and the first operand only needs to be strictly negative for the result to be defined.
如果编译器将减法转换为其他东西,它有责任确保新版本始终计算与旧版本相同的东西.
If the compiler transforms the subtraction to something else, it is its responsibility to ensure that the new version always computes the same thing as the old version.
这篇关于是否从任何被视为未定义行为的整数中减去 INT_MIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!