2
  • In C++, the comma token (i.e., ,) is either interpreted as a comma operator or as a comma separator.

  • However, while searching in the web I realized that it's not quite clear in which cases the , token is interpreted as the binary comma operator and where is interpreted as a separator between statements.

  • Moreover, considering multiple statements/expressions in one line separated by , (e.g., a = 1, b = 2, c = 3;), there's a turbidness on the order in which they are evaluated.

Questions:

  1. In which cases a comma , token is interpreted as an operator and in which as a separator?
  2. When we have one line multiple statements/expressions separated by comma what's the order of evaluation for either the case of the comma operator and the case of the comma separator?
101010
  • 41,839
  • 11
  • 94
  • 168
  • 2
    @haccks the duplicate you flagged refers explicitly to function calls. Thus this question is not a duplicate of the one you flagged. – 101010 Jun 15 '14 at 21:36
  • Do you mean 'unclear' when you say 'turbidness'? – pmr Jun 15 '14 at 21:39
  • For second part of your question: Order of evaluation for comma operator is from left to right. No order is specified for comma separator. – haccks Jun 15 '14 at 21:45
  • 3
    The question title seems bogus. The question text indicates you know what the difference is in behaviour between the comma operator and the comma separator; but you're asking a syntactic question about in which cases does the comma symbol indicate a comma operator. – M.M Jun 15 '14 at 22:10
  • Your question is unclear when you ask about *multiple statements in one line*. In the example shown, are you talking about initialization, or assignment? The behavior is [very different](http://coliru.stacked-crooked.com/a/98b96a945ad4972f) in the two cases. – Praetorian Jun 15 '14 at 22:34
  • @Praetorian Please forgive the lack of expressiveness, due to the fact I'm not a native speaker, I'm referring to both cases. I would very much appreciate an answer with examples like the one you posted. Where the future reader could see in which example the comma acts as an operator and in which as a separator. Also a justification for left to right order of the comma operator and the same for the separator. – 101010 Jun 15 '14 at 22:52

2 Answers2

4

When a separator is appropriate -- in arguments to a function call or macro, or separating values in an initializer list (thanks for the reminder, @haccks) -- comma will be taken as a separator. In other expressions, it is taken as an operator. For example,

my_function(a,b,c,d);

is a call passing four arguments to a function, whereas

result=(a,b,c,d);

will be understood as the comma operator. It is possible, through ugly, to intermix the two by writing something like

my_function(a,(b,c),d);

The comma operator is normally evaluated left-to-right.

The original use of this operation in C was to allow a macro to perform several operations before returning a value. Since a macro instantiation looks like a function call, users generally expect it to be usable anywhere a function call could be; having the macro expand to multiple statements would defeat that. Hence, C introduced the , operator to permit chaining several expressions together into a single expression while discarding the results of all but the last.

As @haccks pointed out, the exact rules for how the compiler determines which meaning of , was intended come out of the language grammar, and have previously been discussed at How does the compiler know that the comma in a function call is not a comma operator?

Community
  • 1
  • 1
keshlam
  • 7,931
  • 2
  • 19
  • 33
3

You cannot use comma to separate statements. The , in a = 1, b = 2; is the comma operator, whose arguments are two assignment expressions. The order of evaluation of the arguments of the comma operator is left-to-right, so it's clear what the evaluation order is in that case.

In the context of the arguments to a function-call, those arguments cannot be comma-expressions, so the top-level commas must be syntactic (i.e. separating arguments). In that case, the evaluation order is not specified. (Of course, the arguments might be parenthesized expressions, and the parenthesized expression might be a comma expression.)

This is expressed clearly in the grammar in the C++ standard. The relevant productions are expression, which can be:

assignment-expression

or

expression , assignment-expression

and expression-list, which is the same as an initializer-list, which is a ,-separated list of initializer-clause, where an initializer-clause is either:

assignment-expression

or

braced-init-list

The , in the second expression production is the comma-operator.

rici
  • 234,347
  • 28
  • 237
  • 341