What is the result of "cout << (a, b)" and why?

You can combine boolean expressions with a comma delimiter. I saw this in code, and I'm not sure if this solves. I wrote some code examples.

 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 result of this:

 (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 this is true only for my system, and if this call actually matches a logical combination of operators.

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

+7
source share
5 answers

What is the output signal, is it the same in all systems?

It is displayed as you describe: the second of two values. It is clearly defined.

Why is this statement possible?

Since the comma operator allows you to evaluate multiple objects in a single expression.

is there any documentation on it?

It is documented in the C ++ standard, [expr.comma]. In C ++ 11, this is 5.18.

To summarize, the comma operator:

  • evaluates the expression before the comma;
  • discards this value;
  • evaluates the expression after the decimal point;
  • gives this meaning as the general meaning of the expression.

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

It is completely pointless in this case; but it is useful if the first expression has side effects that you want to perform before the second, in a situation that allows only one expression. For example:

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

The operator allows the final expression to do two things, even if you can only place one expression.

+7
source

The comma operator returns the right side.

Wikipedia :

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

+10
source

Expression that looks like

 (a, b) 

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

 (a, b, ..., z) 

will create a z value.

Please note that all expressions in the thread are evaluated, including those with side effects. In addition, the comma acts as a sequence point, which means that side effects are applied before evaluating the next operand.

+7
source

This is because your examples return the last value of your sequence of expressions:

  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-------------------------------^ 
+4
source

In C ++, the comma operator gives the compiler an evaluation order. Typically, the compiler can evaluate any expression as it pleases. But the comma operator calculates the left operand before the right operand and returns the value of the right operand.

In a chain of operands connected by a comma operator, the return value is the rightmost operand.

+2
source

All Articles