Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 451
Starhowl Avatar asked Dec 05 '22 13:12

Starhowl


1 Answers

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
like image 184
Artemy Vysotsky Avatar answered Jan 04 '23 06:01

Artemy Vysotsky