Different behaviour of comma operator in C++ with return?

后端 未结 4 614
清歌不尽
清歌不尽 2021-02-01 11:40

This (note the comma operator):

#include 
int main() {
    int x;
    x = 2, 3;
    std::cout << x << \"\\n\";
    r         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 12:23

    Try to apply the simplistic approach just highlighting the precedence with parenthesis:

    ( x = 2 ), 3;

    return ( 2, 3 );

    Now we can see the binary operator "," working in the same way on both, from left to right.

提交回复
热议问题