Different behaviour of comma operator in C++ with return?
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)
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.
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.
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.