First things first: Comma is actually not an operator, for the compiler it is just a token which gets a meaning in context with other tokens.
What does this mean and why bother?
Example 1:
To understand the difference between the meaning of the same token in a different context we take a look at this example:
class Example {
Foo<int, char*> ContentA;
}
Usually a C++ beginner would think that this expression could/would compare things but it is absolutly wrong, the meaning of the <
, >
and ,
tokens depent on the context of use.
The correct interpretation of the example above is of course that it is an instatiation of a template.
Example 2:
When we write a typically for loop with more than one initialisation variable and/or more than one expressions that should be done after each iteration of the loop we use comma too:
for(a=5,b=0;a<42;a++,b--)
...
The meaning of the comma depends on the context of use, here it is the context of the for
construction.
What does a comma in context actually mean?
To complicate it even more (as always in C++) the comma operator can itself be overloaded (thanks to Konrad Rudolph for pointing that out).
To come back to the question, the Code
a = b, c;
means for the compiler something like
(a = b), c;
because the priority of the =
token/operator is higher than the priority of the ,
token.
and this is interpreted in context like
a = b;
c;
(note that the interpretation depend on context, here it it neither a function/method call or a template instatiation.)
a = (b, c);
. – Unpena = b, c = d;
actually does perform the same as the intendeda = b; c = d;
? – Propaneb
andd
are function evaluations that use (and modify) a common state, the execution order is not defined untilC++17
. – Andromeda