1

Recently I came across this piece of code. I don't know why I never saw this kind of syntax in all my "coding life".

int main()
{
    int b;
    int a = (b=5, b + 5);

    std::cout << a << std::endl;
}

a has value of 10. What exactly is this way of initialization called? How does it work?

FoY
  • 398
  • 1
  • 4
  • 17

5 Answers5

6

This statement:

int a = (b=5, b + 5);

Makes use of the comma operator. Per Paragraph 5.18/1 of the C++11 Standard:

[...] A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded value expression (Clause 5).83 Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. The type and value of the result are the type and value of the right operand; the result is of the same value category as its right operand, and is a bit-field if its right operand is a glvalue and a bit-field. If the value of the right operand is a temporary (12.2), the result is that temporary.

Therefore, your statement is equivalent to:

b = 5;
int a = b + 5;

Personally, I do not see a reason for using the comma operator here. Just initialize your variable the easily readable way, unless you have a good reason for doing otherwise.

Community
  • 1
  • 1
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 1
    +1 for your last paragraph, this is just obfuscation IMHO and should be avoided whenever possible (which is, pretty much always). Out of the few mlocs I maintain along with my colleagues I don't think there is a single place where the comma operator is used, and I'd shamelessly do bodily harm to anyone daring to perpetrate that. Readability comes first. – syam Mar 16 '13 at 00:08
  • @FoY: Oh, I see then. You won't find much of this code in real world applications anyway. And if you do, consider changing it (or suggesting to change it) into something more readable ;) – Andy Prowl Mar 16 '13 at 12:23
4

operator , evaluates arguments one after another and return the last value

It may be used not only in initialization

RiaD
  • 46,822
  • 11
  • 79
  • 123
3

The comma , operator allows you to separate expressions. The compount statement made by

exp1, exp2, ..., expn 

evaluates to expn.

So what happens is that first b is set to 5 and then a is set to b + 5.

A side note: since , has the lowest precedence in the table of operators the semantics of

int a = b = 5, b+5;

is different from

int a = (b = 5, b+5);

because the first is parsed as (int a = b = 5), b + 5

Jack
  • 131,802
  • 30
  • 241
  • 343
2

When used in an expression the comma operator will evaluate all of its operands (left-to-right) and return the last.

David G
  • 94,763
  • 41
  • 167
  • 253
1

The initialization is called copy initialization. If you ignore the complex expression on the right, it's the same as in:

int a = 10;

This is to be contrasted with direct initialization, which looks like this:

int a(10);

(It's possible that you were separately confused about how to evalue a comma expression. Please indicate if that's the case.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I actually meant the complex expression :) But it's all good, it's already answered. – FoY Mar 16 '13 at 12:17