为什么没有带有“#if X”的警告当X未定义? [英] Why no warning with "#if X" when X undefined?
问题描述
我偶尔写下这样的代码:
// file1.cpp
#define DO_THIS 1
#if DO_THIS
// stuff
#endif
在代码开发期间,我可以切换 DO_THIS
在0和1之间的定义。
最近我不得不重新排列我的源代码,并将一些代码从一个文件复制到另一个。但我发现我犯了一个错误,这两个部分已经分离如下:
// file1.cpp
#define DO_THIS 1
和
// file2.cpp
#if DO_THIS
// stuff
#endif
显然我修复了错误,但后来想到自己,为什么编译器不警告我?我的警告级别设置为4.为什么在X未定义时 #if X
可疑?
还有一个问题:有没有系统的方法,我可以找出是否我在其他地方犯了同样的错误?该项目是巨大的。
编辑:我可以理解没有警告,#ifdef,这是完全有道理。但是确实#if是不同的。
gcc可以为此产生警告,但它可能不是标准要求: / p>
-Wundef
如果在#if指令中评估未定义的标识符,请警告。
I occasionally write code something like this:
// file1.cpp
#define DO_THIS 1
#if DO_THIS
// stuff
#endif
During the code development I may switch the definition of DO_THIS
between 0 and 1.
Recently I had to rearrange my source code and copy some code from one file to another. But I found that I had made a mistake and the two parts had become separated like so:
// file1.cpp
#define DO_THIS 1
and
// file2.cpp
#if DO_THIS
// stuff
#endif
Obviously I fixed the error, but then thought to myself, why didn't the compiler warn me? I have the warning level set to 4. Why isn't #if X
suspicious when X is not defined?
One more question: is there any systematic way I could find out if I've made the same mistake elsewhere? The project is huge.
EDIT: I can understand having no warning with #ifdef that makes perfect sense. But surely #if is different.
gcc can generate a warning for this, but its probably not required by the standard:
-Wundef
Warn if an undefined identifier is evaluated in an `#if' directive.
这篇关于为什么没有带有“#if X”的警告当X未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!