How does comma operator work during assignment?

后端 未结 5 762
时光说笑
时光说笑 2020-12-06 17:46
int a = 1;
int b = (1,2,3);
cout << a+b << endl; // this prints 4
  1. Is (1,2,3) some sort of structure in c++ (some pri
5条回答
  •  臣服心动
    2020-12-06 18:16

    The second line is using the comma operator. An expression like

    a, b
    

    evaluates both a and b and returns b.

    In this case, the second line is parsed like:

    int b = ((1, 2), 3);
    

    so (1, 2) is evaluated (to 2) then thrown away, and the end result is simply 3.

提交回复
热议问题