CKTIS / C++
Comma operator (,)
The built-in comma operator evaluates its left operand before its right operand, discards the left result, and yields the right result while preserving its type and value category. Do not confuse it with commas separating arguments or declarators.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
(left_expression, right_expression)The built-in , operator joins two expressions. The left operand is evaluated before the right: its value computations and side effects are sequenced before evaluation of the right operand. The left result is discarded. The whole expression has the right operand's type, value, and value category; it also preserves its bit-field status. Operators in a, b, c group as (a, b), c.
Comma has the lowest precedence among expression operators. Use parentheses to make its scope clear: result = (a, b) assigns the result of b, whereas result = a, b means (result = a), b. Commas in f(a, b) and int a = 0, b = 1; are separators, not operators. Writing f((a, b)) passes one argument.
In the example, (x += 2, x * 4) first changes x to 3, then yields 12. The expression (x, y) is an lvalue referring to y, so (x, y) = 7 changes y. The expected output is 3 7 12 followed by a newline.
User-defined types can overload operator,; its result is then determined by the function rather than the right-operand rule. In C++23, operator notation still preserves left-before-right sequencing, but do not transfer that guarantee to an explicit call operator,(a, b). In C++23, use arr[(a, b)] for a comma operator inside a subscript: arr[a, b] calls a multi-argument operator[] or is ill-formed.
Separate statements are usually clearer. The supplied source is an evolving working draft, not a frozen C++23 text.
Examples
#include <iostream>
int main() {
int x = 1;
int y = 10;
int result = (x += 2, x * 4);
(x, y) = 7;
std::cout << x << ' ' << y << ' ' << result << '\n';
}
Result
3 7 12
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.