Why does this code produce a warning referring to the comma operator?
Asked Answered
B

2

8

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 <.

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?

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.

Biles answered 14/4, 2011 at 15:27 Comment(4)
Are you sure you posted the right code? I got: ` warning C4552: '<' : operator has no effect; expected operator with side-effect`Acetous
Yes, that's the real code - see the hyperlink I added to example on IDEOne. See also the other question, where others have seen similar behavior.Biles
You can produce this with: void * v() { return 0; } ... v() < 0; - I'd say it's a bug in gcc.Tiber
Reworded the question after feedback from @TimBiles
C
7

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.

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

Caught answered 14/4, 2011 at 15:30 Comment(2)
Looks like they fixed it. @Xeo has demonstrated that it doesn't generate a warning in GCC 4.5.1 - see his C++0x IDEOne link.Biles
Though as Xeo also points out, you'd think that 4.5.1 would still produce a warning similar to that seen in VC2010.Biles
N
1

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

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

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

Nard answered 14/4, 2011 at 15:29 Comment(5)
@Nard - Yes, sorry, I have added context re the < - see the other question.Biles
@razlebe: Okay, then there's no problem with <. But the code still fails to produce said warning on VC10 and Ideone.Nard
@Nard - Thanks - Perhaps it's a GCC bug.Biles
@Nard Your IDEOne hyperlink is using C++0x (GCC 4.5.1) rather than 4.3.4. Again, perhaps this is a gcc bug (which they've fixed in a later version).Biles
@razlebe: You're right, 4.3.4 on Ideone produces that error - seems like @Johannes is right and they forgot to change the text.. doesn't explain though, why 4.5.1 doesn't even produce a warning anymore...Nard

© 2022 - 2024 — McMap. All rights reserved.