为什么这段代码会产生一个指向逗号操作符的警告?
Why does this code produce a warning referring to the comma operator?
在回答这个问题时,我遇到了这段代码…
#include <iostream>
int main()
{
int const income = 0;
std::cout << "I'm sorry your income is: " < income; // this is line 6
}
…里面有个错别字。第6行上的第二个(预定的)<<
运算符被意外地写成了<
。
prog.cpp: In function ‘int main()’:
prog.cpp:6: warning: right-hand operand of comma has no effect
我的问题是:为什么会产生这样的警告?它指的是哪个逗号操作符?
注意:我不提倡在cout
语句中故意使用单个<
。我只是偶然发现了这个警告,而试图找出一个答案的另一个问题,我已经链接到,我很好奇为什么编译器生成它。
我想他们只是忘记更改警告文本
int main() {
1, 2;
}
prog.cpp:2: warning: left-hand operand of comma has no effect
prog.cpp:2: warning: right-hand operand of comma has no effect
expr, expr
操作符先对左操作数求值,然后对右操作数求值,最后得到右操作数的求值结果。如果右操作数没有作用,并且没有使用它的值,则可能是程序中存在错误。
你给定的程序不会用MSVC2010为我产生警告,它只产生
警告C4552: '<':操作符无效;预期的有副作用的操作符
应该是<<
在income;
之前。(注意:Ideone根本不产生警告。)
相关文章:
- 警告处理为错误这里有什么问题
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 使用typeid警告未使用的变量
- 示例C++项目编译中的警告
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 如何修复编译器警告 C6386 和 C6385?
- 返回语句后的代码,没有警告
- 获取隐式转换溢出从无符号到已签名的警告
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 在未链接的部分上生成警告
- 警告 C4552:">>":未使用表达式的结果
- 禁止显示由于常量为零而比较始终为假的警告
- 警告赋值操作符类引用
- 为什么这段代码会产生一个指向逗号操作符的警告?