-Wall
选项时,会得到一个警告。使用以下代码:
#include <stdio.h>
int main(int argc, char* argv[])
{
/* const */ unsigned int i = 0;
if (i != argc)
return 1;
return 0;
}
我收到了这个警告:
<source>: In function 'int main(int, char**)':
<source>:6:8: warning: comparison of integer expressions of different signedness: 'unsigned int' and 'int' [-Wsign-compare]
6 | if (i != argc)
| ~~^~~~~~~
Compiler returned: 0
然而,如果我取消注释这个const
,编译器就会满足。我可以在几乎每个GCC版本上重现这个问题(请参见https://godbolt.org/z/b6eoc1)。这是GCC的一个bug吗?
const
更准确是一种特性,而不是 bug。 - John Bollinger