无意中使用=而不是== [英] Inadvertent use of = instead of ==
问题描述
似乎
if(x = y){....}
而不是
if == y){...}
是许多邪恶的根源。
为什么不将所有编译器标记为错误,而不是可配置的警告?
我有兴趣发现构造 if(x = y)
有用的情况。
大多数情况下,编译器会努力保持向下兼容性。错误会破坏现有的合法代码,甚至开始抛出警告,这将导致自动系统的问题,通过自动编译和检查错误和警告来跟踪代码。
例如:
$ b$ b
void * ptr = calloc(1,sizeof(array));
if(NULL = ptr){
// some error
}
这会导致编译错误。
It seems that
if (x=y) { .... }
instead of
if (x==y) { ... }
is a root of many evils.
Why don't all compilers mark it as error instead of a configurable warning?
I'm interested in finding out cases where the construct if (x=y)
is useful.
Most of the time, compilers try very hard to remain backward compatible.
Changing their behavior in this matter to throw errors will break existing legit code, and even starting to throw warnings about it will cause problems with automatic systems that keep track of code by automatically compiling it and checking for errors and warnings.
This is an evil we're pretty much stuck with atm, but there are ways to circumvent and reduce the dangers of it.
Example:
void *ptr = calloc(1, sizeof(array));
if (NULL = ptr) {
// some error
}
This causes a compilation error.
这篇关于无意中使用=而不是==的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!