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

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 a comma operator, this is:

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

outputs 3 .

Why does the comma operator behave differently with return ?

+74
c ++ language-lawyer operator-precedence return comma-operator
Sep 07 '16 at 7:56
source share
4 answers

According to Operator Priority , the comma operator has lower priority than operator= , so x = 2,3; equivalently (x = 2),3; . (The operator’s priority determines how the operator will be tied to its arguments, which are tougher or weaker than other operators, according to their priorities.)

Note that the expression for the comma (x = 2),3 here, not 2,3 . First, x = 2 is evaluated (and its side effects are completed), then the result is discarded, then 3 is evaluated (actually does nothing). Therefore, the value of x is 2 . Note that 3 is the result of the entire comma expression (i.e. x = 2,3 ), it will not be used to assign x . (Change it to x = (2,3); x will be assigned 3 )

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




Additional Information on Expressions and Statements

An expression is a sequence of operators and their operands that defines a calculation.

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

An expression followed by a semicolon is an expression.

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

return 2,3; return statement , 2,3 is the expression here.

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

+129
Sep 07 '16 at 7:58
source share

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

Score x = 2,3; equal to (x = 2), 3; due to operator priority. The score is still left to right, and the whole expression has a value of 3 with side effect x , assuming a value of 2.

+30
Sep 07 '16 at 7:59
source share

This statement:

  x = 2,3; 

consists of two expressions:

 > x = 2 > 3 

Since the operator’s priority , = has more priority than the comma, therefore x = 2 is evaluated even after 3 . Then x will be 2 .




Instead of return :

 int f(){ return 2,3; } 

Language Syntax:

 return <expression> 

Note return not part of the expression.

So, in this case two expressions will be evaluated:

 > 2 > 3 

But only the second will be returned ( 3 ).

+19
Sep 07 '16 at 8:03
source share

Try a simplified approach by simply prioritizing with parentheses:

( x = 2 ), 3;

return ( 2, 3 );

Now we can see that the binary operator "," works the same on both, from left to right.

+2
Sep 15 '16 at 13:58
source share



All Articles