When all does comma operator not act as a comma operator?

后端 未结 5 1928
别那么骄傲
别那么骄傲 2020-12-20 16:38

If you see this code,

class A{
public:
    A(int a):var(a){}
    int var;
};

int f(A obj) {
    return obj.var;
}

int main() {
    //std::cout<

        
5条回答
  •  眼角桃花
    2020-12-20 17:36

    From a grammatical point of view, the parameters of a function call form an optional expression-list inside parentheses. An expression-list consists of one or more assignment-expression separated by a comma token. A comma can only signify a comma operator where an expression is expected.

    The comma operator makes an expression out of an expression, a , and an assignment-expression, but an expression involving a comma operator is not itself an assignment-expression so can't appear in an expression-list except where it's a constituent of something that is an assignment-expression.

    For example, you can surround any expression (including one using the comma operator) inside parentheses to from a primary-expression which is an assignment-expression and hence valid in an expression-list.

    E.g.

    postfix-expression where the expression-list consists of two assignment-expression each of which is an identifier.

    f( a, b );
    

    postfix-expression where the expression-list consists of a single assignment-expression which is a primary-expression which is a parenthesized expression using the comma operator.

    f( (a, b) );
    

提交回复
热议问题