8

It is possible to combine boolean expressions with a comma separator. I have seen it in a code and i am not sure what this resolves to. I wrote some sample code.

int BoolStatement(void)
{
    using std::cout;
    using std::endl;

    cout << "(0, 0) => " << (0, 0) << endl;
    cout << "(0, 1) => " << (0, 1) << endl;
    cout << "(1, 0) => " << (1, 0) << endl;
    cout << "(1, 1) => " << (1, 1) << endl;
    cout << "(0, 0) => " << (0, 0) << endl;
    cout << "(0, 3) => " << (0, 3) << endl;
    cout << "(5, 0) => " << (5, 0) << endl;
    cout << "(7, 1) => " << (7, 1) << endl;

    cout << endl;
    return 0;
}

The output of this is:

(0, 0) => 0
(0, 1) => 1
(1, 0) => 0
(1, 1) => 1
(0, 0) => 0
(0, 3) => 3
(5, 0) => 0
(7, 1) => 1

I am not sure if that is only true for my system and if this call is actually the same as a boolean combination of statements.

What is the output, is it the same on all systems? Why is that statement possible and is there documentation on it?

2
  • 4
    What boolean expressions are you combining? 0, 1, 3, etc. are integers, not booleans.
    – Barmar
    Feb 4, 2015 at 16:13
  • @Barmar I imply the use of integers as booleans where 0 is false and !0 is true. But looking at the answers you are right - this has nothing to do with booleans.
    – Johannes
    Feb 4, 2015 at 16:17

5 Answers 5

10

The comma operator returns the righthand side.

Wikipedia:

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

9

What is the output, is it the same on all systems?

The output is as you describe: the second of the two values. It's well defined.

Why is that statement possible?

Because the comma operator allows you to evaluate more than one thing in a single expression.

is there documentation on it?

It's documented in the C++ standard, [expr.comma]. In C++11, that's 5.18.

To summarise, the comma operator:

  • evaluates the expression before the comma;
  • discards that value;
  • evaluates the expression after the comma;
  • yields that value as the overall expression value.

You can see that from your output: in each case, the output is the value after the comma.

It's completely pointless in this case; but is useful if the first expression has side-effects which you want to sequence before the second, in a situation that only allows a single expression. For example:

for (int i = 0, j = n; i < j; ++i, --j)

The operator allows the final expression to do two things, even though you can only put one expression there.

7

An expression that looks like this

(a, b)

is a comma expression, its value is its result is its rightmost operand, i.e.

(a, b, ..., z)

would produce the value of z.

Note that all expressions in the chain get evaluated, including these with side effects. Moreover, comma acts as a sequence point, meaning that the side effects are applied before evaluating the next operand.

1
  • Perhaps worth pointing out that while they look the same, a comma expression and a function call are not the same thing. In particular, order of evaluation of function arguments is entirely undefined.
    – BoBTFish
    Feb 4, 2015 at 16:20
4

This is because it returns the last value of your expression sequence, in your examples:

    cout << "(0, 1) => " << (0, 1) << endl;
1-------------------------------^
    cout << "(1, 0) => " << (1, 0) << endl;
0-------------------------------^
    cout << "(1, 1) => " << (1, 1) << endl;
1-------------------------------^
    cout << "(0, 0) => " << (0, 0) << endl;
0-------------------------------^
    cout << "(0, 3) => " << (0, 3) << endl;
3-------------------------------^
    cout << "(5, 0) => " << (5, 0) << endl;
0-------------------------------^
    cout << "(7, 1) => " << (7, 1)
1-------------------------------^
2

In C++, a comma operator gives the compiler the order of evaluation. Usually the compiler is free to evaluate any expression the way it likes. But the comma operator evaluates its left hand operand before the right hand operand and returns the value of the right hand operand.

In a chain of operands linked by the comma operator, the returned value is the rightmost operand.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.