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

感情迁移 提交于 2019-12-20 09:25:59

问题


This (note the comma operator):

#include <iostream>
int main() {
    int x;
    x = 2, 3;
    std::cout << x << "\n";
    return 0;
}

outputs 2.

However, if you use return with the comma operator, this:

#include <iostream>
int f() { return 2, 3; }
int main() {
    int x;
    x = f();
    std::cout << x << "\n";
    return 0;
}

outputs 3.

Why is the comma operator behaving differently with return?


回答1:


According to the Operator Precedence, comma operator has lower precedence than operator=, so x = 2,3; is equivalent to (x = 2),3;. (Operator precedence determines how operator will be bound to its arguments, tighter or looser than other operators according to their precedences.)

Note the comma expression is (x = 2),3 here, not 2,3. x = 2 is evaluated at first (and its side effects are completed), then the result is discarded, then 3 is evaluated (it does nothing in fact). That's why the value of x is 2. Note that 3 is the result of the whole comma expression (i.e. x = 2,3), it won't be used to assign to x. (Change it to x = (2,3);, x will be assigned with 3.)

For return 2,3;, the comma expression is 2,3, 2 is evaluated then its result is discarded, and then 3 is evaluated and returned as the result of the whole comma expression, which is returned by the return statement later.


Additional informations about Expressions and Statements

An expression is a sequence of operators and their operands, that specifies a computation.

x = 2,3; is expression statement, x = 2,3 is the expression here.

An expression followed by a semicolon is a statement.

Syntax: attr(optional) expression(optional) ; (1)

return 2,3; is jump statement (return statement), 2,3 is the expression here.

Syntax: attr(optional) return expression(optional) ; (1)




回答2:


The comma (also known as the expression separation) operator is evaluated from left to right. So return 2,3; is equivalent to return 3;.

The evaluation of x = 2,3; is (x = 2), 3; due to operator precedence. Evaluation is still from left to right, and the entire expression has the value 3 with the side-effect of x assuming the value of 2.




回答3:


This statement:

  x = 2,3;

is composed by two expressions:

> x = 2
> 3

Since operator precedence, = has more precedence than comma ,, so x = 2 is evaluated and after 3. Then x will be equal to 2.


In the return instead:

int f(){ return 2,3; }

The language syntax is :

return <expression>

Note return is not part of expression.

So in that case the two expression will be evaluated will be:

> 2
> 3

But only the second (3) will be returned.




回答4:


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.



来源:https://stackoverflow.com/questions/39364263/different-behaviour-of-comma-operator-in-c-with-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!