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

后端 未结 2 702
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 18:30

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

#include 

int main()
{
    int const income = 0;
    std::cout << \"I\'m sorr         


        
相关标签:
2条回答
  • 2020-12-06 18:51

    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.

    0 讨论(0)
  • 2020-12-06 18:52

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

    0 讨论(0)
提交回复
热议问题