为什么从返回0x80000000的函数返回int32_t会导致警告? [英] Why doesn't returning 0x80000000 from a function that returns int32_t cause a warning?
问题描述
请考虑:
int32_t f() {
return 0x80000000;
}
0x80000000超出范围 int32_t
( INT32_MAX
为 0x7fffffff
)。
Why doesn't that cause a compiler warning (at least on GCC)? 0x80000000 is out of the range of int32_t
(INT32_MAX
is 0x7fffffff
). I believe this should cause an implicit cast - is that correct?
更多信息:
if (f() == 0x80000000)
foo();
上述在GCC上不会产生警告。但
The above causes no warning on GCC. However
int32 ret = f();
if (ret == 0x80000000)
baz();
导致警告:有符号和无符号整数表达式之间的比较。我相信这是因为0x80000000有类型 unsigned int
,因为出了 int
的范围。那是对的吗?
Causes "warning: comparison between signed and unsigned integer expressions". I believe this is because 0x80000000 has type unsigned int
due to being out of int
's range. Is that correct?
假设我的假设没有错误,为什么第一次比较不会产生警告?
Assuming none of my assumptions are wrong, why doesn't the first comparison cause a warning?
推荐答案
相关的警告开关显示为 -Wconversion
,它不是由 -Wextra
激活的。
The relevant warning switch appears to be -Wconversion
, which is not activated by -Wextra
.
这篇关于为什么从返回0x80000000的函数返回int32_t会导致警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!