10

I found this in my code, was a typo on my part, but it still compiled. Anyone know why? I have no idea.

#include <string>
#include <iostream>

int main()
{
  std::string x;
  std::string b = "Bryan";
  x += '|' + b, x;
  std::cout << x << std::endl;
}
Prix
  • 19,417
  • 15
  • 73
  • 132
bryan sammon
  • 7,161
  • 15
  • 38
  • 48

4 Answers4

21
x += '|' + b, x;

Here , is basically an operator whose left operand is evaluated first, followed by right operand. It is that simple.

Since the precedence of += and + is higher than , operator, it becomes equivalent to this:

(x += '|' + b) ,  x;

Here:

left  operand => (x += '|' + b)
right operand =>  x

Try another example:

int f() { ... }
int g() { ... }

f(), g();

Here f() will be called first followed by g().

Hope that helps.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Why is `+=` treated differently than `=` here? – mohit Aug 09 '13 at 17:49
  • @mohit: I don't know what you mean by "differently". Still I guess you want to know this : `+=` and `=` have same precedence. It is just that `,` has lower precedence than both of them! – Nawaz Aug 09 '13 at 17:51
  • Got it. I was under wrong impression that `a = 4, 5;` would set `a` to `5`. Thanks. – mohit Aug 09 '13 at 17:55
  • 1
    @mohit: No, `a=4,5` would set `a` to `4`. If you want it to be `5`, then write `a=(4,5)` or simply `a=5`. – Nawaz Aug 09 '13 at 17:56
  • Is there any legitimate reason to do this? What's the purpose of this operator? – user606723 Aug 09 '13 at 20:42
3
x += '|' + b, x;

This compiles because the comma here is acting as an operator (instead of a separator) where the right-hand operand has no effect.

From 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).

...

The comma operator has the lowest precedence of any C operator...

In x += '|' + b, x;, operator += has a higher precedence than , and operator + has a higher precedence than +=, meaning that it's equivalent to (x += ('|' + b)), x;

Additionally, if you compile your code with warnings on, you will likely receive a warning similar to this:

warning: right-hand operand of comma has no effect
Community
  • 1
  • 1
nklauza
  • 1,501
  • 1
  • 12
  • 12
2
x += '|' + b, x;

This is a comma operator, which has the lowest precedence. It takes two operands, , evaluates them left to right. Both of its operands are perfectly legal in this example:

Note that comma operator has a value of the right operand, but it's usually useless, like in this example. So it's the same with:

x += '|' + b;
x;  // no effect
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2
x += '|' + b, x;

It's because the the expression before the , operator takes to effect. For example -

std::string y("Hello");
x += '|' + b, y;

The above would still give the result |Bryan in your program.

Mahesh
  • 34,573
  • 20
  • 89
  • 115