为什么这段代码会产生一个指向逗号操作符的警告?

时间:2021-12-29 17:05:41

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
}

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

…它包含一个错字。第6行上的第二个(预期的) <操作符被意外地写成<。< p>

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

除此之外,使用GCC 4.3.4或4.4.3编译代码会导致警告:

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?

我的问题是:为什么会产生这种特别的警告?它指的是哪个逗号运算符?

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.

注意:我并不是提倡在cout语句中故意使用单个<。我只是无意中发现了这个警告,并试图找到我链接到的另一个问题的答案,我很好奇为什么编译器会生成它。

2 个解决方案

#1


6  

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

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.

expr操作符计算左操作数,然后计算右操作数,得到右操作数的计算结果。如果正确的操作数没有效果,并且没有使用它的值,那么它可能是程序中的一个错误。

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

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

#2


1  

Your given program doesn't produce that warning for me with MSVC2010, it only produces

您的给定程序并没有为我提供MSVC2010的警告,它只生成。

warning C4552: '<' : operator has no effect; expected operator with side-effect

警告C4552: '<':操作员无影响;预计运营商与副作用

As that should be a << before income;. (Note: Ideone doesn't produce a warning at all.)

因为它应该是< before income;(注意:Ideone根本不会发出警告。)

#1


6  

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

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.

expr操作符计算左操作数,然后计算右操作数,得到右操作数的计算结果。如果正确的操作数没有效果,并且没有使用它的值,那么它可能是程序中的一个错误。

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

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

#2


1  

Your given program doesn't produce that warning for me with MSVC2010, it only produces

您的给定程序并没有为我提供MSVC2010的警告,它只生成。

warning C4552: '<' : operator has no effect; expected operator with side-effect

警告C4552: '<':操作员无影响;预计运营商与副作用

As that should be a << before income;. (Note: Ideone doesn't produce a warning at all.)

因为它应该是< before income;(注意:Ideone根本不会发出警告。)