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

后端 未结 2 704
被撕碎了的回忆
被撕碎了的回忆 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.

提交回复
热议问题