c++c++11c++14language-lawyersequencing

Comma operator in C++11 (sequencing)


The standard mentions f(a,(t=3,t+2),c); which would be an assignment-expression followed by an expression for the 2nd operator according to my understanding.

But the grammar lists it juxtaposed:

expression:

assignment-expression

expression, assignment-expression

Working Draft, Standard for Programming Language C ++ Revision N4140 (November 2014)

Is someone so nice as to explain to me please what it is that I'm missing here?


Solution

  • When you see

     expression:
        assignment-expression
        expression, assignment-expression
    

    It mean that there are 2 possibilities for expression. One possibility that it is just assignment-expression that is defined somewhere earlier. Or it is recursively represented as expression, assignment-expression

    So after extending it you receive that expression is comma separated list of one or more assignment-expression tokens.

    In the sample you're mentioned second parameter is expression (t=3,t+2) which consists of 2 comma-separated assignment-expressions - and since it appears "In contexts where comma is given a special meaning" it has to "appear only in parentheses".

    To find out why assignment-expression could take a form of t+2 you have to go back from its definitions and choose first choice always

    assignment-expression
    -> conditional-expression
    --> logical-or-expression
    ---> logical-and-expression
    ----> inclusive-or-expression
    -----> exclusive-or-expression
    ------> and-expression
    -------> equality-expression
    --------> relational-expression
    ---------> shift-expression
    ----------> additive-expression - this is what you see