为什么此代码产生一个引用逗号运算符的警告? [英] Why does this code produce a warning referring to the comma operator?

查看:396
本文介绍了为什么此代码产生一个引用逗号运算符的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答 此问题 时,我遇到了此代码...

When answering this question, I came across this code...

#include <iostream>

int main()
{
    int const income = 0;
    std::cout << "I'm sorry your income is: " < income;    // this is line 6
}

...其中包含错字。第6行上的第二个(预期的)< <$ / code>运算符被意外地写为<

...which contains a typo. The second (intended) << operator on line 6 has been accidentally written as a <.

另外,编译代码使用GCC 4.3.4 或4.4.3会产生警告:

That aside, compiling the code using GCC 4.3.4 or 4.4.3 results in a warning:

prog.cpp: In function ‘int main()’:
prog.cpp:6: warning: right-hand operand of comma has no effect

为什么会出现这种特殊的警告?哪个逗号运算符是指?

My question: why is that particular warning produced? Which comma operator is it referring to?

注意:我不主张在<$ c $中故意使用单个< c> cout 语句。我只是偶然发现这个警告,试图找出一个答案,我链接到的另一个问题,我很好奇为什么编译器生成它。

NOTE: I'm not advocating deliberately using a single < in a cout statement. I merely stumbled across this warning while trying to figure out an answer to the other question I've linked to, and am curious as to why the compiler generates it.

推荐答案

我认为他们只是忘了更改警告文本

I think they just forgot to change the warning text

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 左操作数,然后计算右操作数并产生右操作数的计算结果。如果右操作数没有效果,它的值不使用,它可能是程序中的一个错误。

The expr, expr operator evaluates the left operand, then evaluates the right operand and yields the result of the right operand's evaluation. If the right operand has no effect and its value is not used, it's probably a bug in the program.

现在他们只是滥用了上面的警告文本来警告其他二进制操作符。

Now they just abused the above warning text to warn for other binary operators, it seems.

这篇关于为什么此代码产生一个引用逗号运算符的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆