Inadvertent use of = instead of ==(无意中使用 = 而不是 ==)
问题描述
好像
if (x=y) { .... }
而不是
if (x==y) { ... }
是万恶之源.
为什么不所有编译器将其标记为错误而不是可配置的警告?
Why don't all compilers mark it as error instead of a configurable warning?
我有兴趣找出构造 if (x=y) 有用的情况.
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.
这是一个我们几乎被 atm 困住的邪恶,但有一些方法可以规避和减少它的危险.
This is an evil we're pretty much stuck with atm, but there are ways to circumvent and reduce the dangers of it.
例子:
void *ptr = calloc(1, sizeof(array));
if (NULL = ptr) {
// some error
}
这会导致编译错误.
这篇关于无意中使用 = 而不是 ==的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无意中使用 = 而不是 ==
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
