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

后端 未结 4 620
清歌不尽
清歌不尽 2021-02-01 11:40

This (note the comma operator):

#include 
int main() {
    int x;
    x = 2, 3;
    std::cout << x << \"\\n\";
    r         


        
4条回答
  •  悲&欢浪女
    2021-02-01 12:18

    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)

提交回复
热议问题