String concatenation, why does this compile?

c++

Solution

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.

Problem

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; } ```

Original source

Related problems