C++ Reference ยท Operators
The comma operator
Reference for the C++ comma operator: evaluating two expressions in order, its use in for loops, how it differs from argument separators, and overloading.
The comma operator a, b evaluates a, discards its value, then evaluates b and yields b (with its type and value category). Everything a does, including side effects, is finished before b starts, so the comma is one of the few places where C++ guarantees left-to-right order.
It has the lowest precedence of all operators. Most commas in a program are not the operator at all: they separate function arguments, declarators (int a, b;), initializer-list elements and template arguments. Only a comma inside an expression, where an expression is expected, is the operator.
int x = (f(), g()); // calls f, throws away its result, then calls g
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
expression1 , expression2
| Name | Description |
|---|---|
expression1 |
Evaluated first; its value is discarded. All side effects are complete before expression2 begins. |
expression2 |
Evaluated second; it provides the result. |
Returns: the value of expression2. If expression2 is an lvalue, so is the whole expression, so (a, b) = 3 assigns to b.
Errors / undefined behavior: none of its own. The built-in operator introduces a sequence point, unlike the commas separating function arguments, whose evaluation order is unspecified.
Usage notes
- Where it is useful: several updates in one
forincrement (++i, --j), and in macros orconstexprhelper expressions where only an expression is allowed. - Parenthesize it when you need the operator inside an argument list or initializer:
f((a, b))passes one argument,f(a, b)passes two. - Discarding with
(void):(void)x, ysuppresses "unused value" warnings from the left operand and also stops an overloaded comma from being selected. - Overloading
operator,is legal but strongly discouraged: an overloaded comma loses the sequencing guarantee for class operands before C++17 and surprises readers. Library code defends against it with(void)casts. - Common mistake:
int a = 1, 2;is a syntax error (it declaresaand then tries to declare2); writeint a = (1, 2);if you really mean it, which is rarely wise. - Common mistake:
e = 7, 8;assigns 7. Use parentheses when the value of the whole comma expression is what you want. - Warnings: compilers warn about a left operand with no effect, such as
(a, b)whereais a plain variable. - Alternatives: write two statements. Readability usually beats cleverness here. A fold expression such as
(f(args), ...)in a variadic template is the modern place where the comma operator earns its keep.