在使用严格溢出标志进行编译时,以下代码在第二个测试中告诉我,r可能不是我所认为的那样:
int32_t r(my_rand());
if(r < 0) {
r = -r;
if(r < 0) { // <-- error on this line
r = 0;
}
}
错误信息为:
/build/buildd/libqtcassandra-0.5.5/tests/cassandra_value.cpp:
In function 'int main(int, char**)':
/build/buildd/libqtcassandra-0.5.5/tests/cassandra_value.cpp:2341:13:
error: assuming signed overflow does not occur when simplifying
conditional to constant [-Werror=strict-overflow]
if(r < 0) {
^
我不理解的是:为什么错误不会在前一行生成?因为实际上,溢出是发生在这里,对吗?
r = -r;