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

C++ReturnLanguage LawyerOperator PrecedenceComma Operator

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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)

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxyzView Question on Stackoverflow
Solution 1 - C++songyuanyaoView Answer on Stackoverflow
Solution 2 - C++BathshebaView Answer on Stackoverflow
Solution 3 - C++BiagioFView Answer on Stackoverflow
Solution 4 - C++LucianoView Answer on Stackoverflow