问题描述
我继承了一个相当大的codeBase的那里有人,不知何故,写了几个条件语句,像这样:
I've inherited a sizeable codebase where someone, somehow, has written several conditionals like so:
enum
{
FOO_TYPE_A,
FOO_TYPE_B,
FOO_TYPE_C,
FOO_TYPE_D
};
void bar(int fooType)
{
if (fooType == FOO_TYPE_A || FOO_TYPE_B) // <-- This will always be true, since FOO_TYPE_B is nonzero!
{
// Do something intended for only type A or B
}
// Do things general to A,B,C,D
}
在这里的条件检查应明确:
where the condition check should clearly be:
if (fooType == FOO_TYPE_A || fooType == FOO_TYPE_B)
有没有在海湾合作委员会警告我可以打开找到他们,类似MSDN的的?
具体来说,我使用了Android NDK r9d。
Specifically, I'm using the Android NDK r9d.
如果不是,为什么不呢?这似乎是一个有用的包罗了所有无意转让,无符号> 0,以及上述愚昧。
If not, why not? It seems like a useful catch-all for unintentional assignment, unsigned > 0 as well as the above foolishness.
编辑:提出的code更详细的说明问题。
Made the code more verbose to illustrate the problem.
推荐答案
我没有看到对应于MSDN C4127警告。 GCC确实有一个警告,是故意有些相似,但不是解决问题的方法:的
I do not see a warning that corresponds to MSDN C4127. GCC does have a warning that is somewhat similar in intent, but not to address your problem: -Wtype-limits
警告如果比较永远为真或假的因
有限范围的数据类型,但不警告恒定
前pressions。例如,警告如果无符号
变量与
与0 &LT;
或&GT; =
。这个警告也可通过启用
-Wextra
。
如你所见,海湾合作委员会明确指出它不发出警告的前不变pressions。这样做的动机可能是由于共同使用恒定的前pressions的利用编译器的死code消除,使宏(或其部分)可以通过使用编译时间常数被优化掉。它将被用来作为一种替代的条件编译(#如果定义()
和#如果点¯x==是
) ,作为宏更自然读起来就像一个普通的功能。作为一个假设的例子:
As you can see, GCC explicitly states it does not warn about constant expressions. The motivation for this may be due to the common use of constant expressions to leverage the compiler's dead code elimination so that macros (or portions of it) can be optimized away by using a compile time constant. It would be used as an alternative to conditional compilation (#if defined()
and #if X == Y
), as the macro reads more naturally like a regular function. As a hypothetical example:
#define VERIFY(E) \
do { \
if (NO_VERIFY) break; \
if (!(E) && (VERIFY_LOG_LEVEL >= log_level() || VERIFY_LOG_ALWAYS)) { \
log("validation error for: " #E); \
} \
} while (0)
这篇关于是否有一个gcc预警和QUOT;有条件的前pression是常数QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!