2

I recently wanted to get a better feeling for the return values of the ++i and the i++ operator. So I created this small program here:

#include <iostream>
using namespace std;
int main()
{
    int i = 0;
    int j = 0;
    cout << "i++ " << i++ << " i " << i << endl;
    cout << "++j " << ++j << " j " << j << endl;
}

Now looking at the code I would expect that when I execute it I get the following result:

i++ 0 i 1
++j 1 j 1

Compiling the above snippet with llvm/clang 3.5 (Apple LLVM version 6.0 (clang-600.0.51)) yields the following warning:

main.cpp:8:23: warning: unsequenced modification and access to 'j'
      [-Wunsequenced]
    cout << "++j " << ++j << " j " << j << endl;
                      ^               ~
1 warning generated.

What does that mean in this context? The only blog post I found was not so helpful since I can't imagine a reordering of the output somehow. What would the output of such a reordering look like?

There's also a couple of similar questions here. The only good question on the subject I found was this one. But I don't understand how this would create the compiler warning above.

Compiling the code from above with gcc 4.9 (the flag -std=c++11 was enabled) didn't show this warning at all.

Community
  • 1
  • 1
Pascal
  • 831
  • 2
  • 11
  • 24
  • Please make it stop, [make it stop](http://www.youtube.com/watch?v=XwRpss4yrec) – EdChum Oct 15 '14 at 08:23
  • 1
    The order of the output is set in stone, but the order in which the values to be output are computed isn't. – molbdnilo Oct 15 '14 at 08:26
  • Thanks for the pointer @user657267 ! It's going to take me a while to get it. I feel that it has something to do in the way that streams are handled: Do you have a pointer for me in this direction? Btw: I can understand your frustration... As soon as Visual Studio C++11/17 support becomes more common there's going to be an increased number of similar questions... – Pascal Oct 15 '14 at 08:45

0 Answers0