3

I am trying to grok the comma operator. The reference says:

In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded (...), and its side effects are completed before evaluation of the expression E2 begins (...).

I am unable to grok the clause - "... the expression E1 is evaluated, its result is discarded (...), and its side effects are completed before...". Specifically, what is retained and what is discarded?

For e.g., in the example from this SO answer:

int x = 0;
int y = some_number;
for(; x < y; ++x, --y)
{
    // Do something which uses a converging x and y
}

When we do a ++x, what is the 'result' (that is discarded) and what is the 'side-effect' (that is 'completed' and perhaps 'retained')? Shouldn't the result of ++x be discarded and the value of x remain unchanged? Working example showing incrementation of x is here.

In other words, given a certain expression, how do I deduce if it will be evaluated and its results would be discarded, or if it is a side-effect and its results would perhaps be computed and retained?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Why would it be discarded? x has been declared above so it will be incremented as long as the condition is true. I think you might be confused with the scope of the variable. – Adrisui3 Sep 13 '20 at 20:50
  • Well, the quote included above says "the expression E1 is evaluated, its result is **discarded**" and `++x` is E1. So, IMHO `++x` would result in `1`, which should have been discarded and x should have remained `0`. –  Sep 13 '20 at 20:50
  • 2
    @UmedhSinghBundela The result of evaluating `++x` is the value `x + 1` and that's discarded. The side effect is incrementing `x` and that's retained. – dxiv Sep 13 '20 at 20:52
  • For instance a "side effect" of `a=1` is the assignment to a but the expression evaluates to 1. So the expression `a=1,b=2` evaluates as 2 but also causes a and b to be set to 1 and 2 respectively. Does that clarify "side effect" – doug Sep 13 '20 at 20:52
  • 2
    With the expression `++x`, the incrementation of `x` is the side-effect that is retained. However, the result of the expression `++x` is discarded. But this is not a good example, because the new value of `x` and the result of the expression (that is discarded) is the same value, so it is confusing. – Andreas Wenzel Sep 13 '20 at 20:53
  • @dxiv, could you please elaborate? Note that `cout` in the linked Ideone code shows that value of `x` is incremented to `1`. –  Sep 13 '20 at 20:54
  • @UmedhSinghBundela That's what I wrote "*side effect is incrementing `x` and that's retained*". – dxiv Sep 13 '20 at 20:55
  • Both `++x` and `x++` *will alter X*. The only difference is the return value which you can ignore or use, but the outcome doesn't change. – tadman Sep 13 '20 at 20:55

1 Answers1

5

In C++ an expression can result in a value and can cause side effects. In the expression ++x, --y you have two sub expressions forming the whole expression. The left, ++x returns x after increment, and the right returns y after decrement. The expression will return the right side of the comma (y) rather than the left side x.

The side effects of the left hand side are preserved, so x is still incremented.

This might make more sense if you were looking to perform assignment.

For example

int x = 1;
int y = 1;
int& z = (++x, --y);
std::cout << z << std::endl;

z becomes a reference to y and thus we will print 0

AndyG
  • 39,700
  • 8
  • 109
  • 143
  • Yes, it makes sense. Could you elaborate a bit more on the term side-effect? Or perhaps guide me to some resource to understand 'side-effects' of expressions? For e.g., if I have say, `x=5, y=3` in the above example (instead of `++x`, `y--`); within the loop, will I have `x=5`? –  Sep 13 '20 at 21:01
  • 1
    @UmedhSinghBundela: Think of side effect as "state change". When x is incremented, its state changes. Sounds obvious right? It would not be so obvious if you worked in a functional language where once x is assigned to 1, it will always be 1. A "pure" function has no side effects, it just computes a result and returns a value. Think `x+y`. Neither `x` nor `y` change, but the expression returns a result that is their sum, which can be used to initialize another variable. When a function doesn't actually change anyone's state, it's called "pure". – AndyG Sep 13 '20 at 21:05