2

In c++, arguments evaluation of order is not guaranteed, but is the order of left/right sub expression of assignment expression is guaranteed? For example

#include <iostream>
#include <map>

int main()
{
    int i = 2;
    std::map<int, int> map;
    map[i++] = i--;
    return 0;
}

Is left expression i++ guaranteed to be executed before right expression i--?

Jichao
  • 40,341
  • 47
  • 125
  • 198

1 Answers1

1

You asked:

Is left expression i++ guaranteed to be executed before right expression i--?

No, it is not.

The line

map[i++] = i--;

could end up being

map[2] = 3;

or

map[1] = 2;

depending on which side of the assignment operator gets evaluated first.

However, since the line invokes undefined behavior, it could also be, as pointed out in the comment by @juanchopanza, :

map[42] = -999;
R Sahu
  • 204,454
  • 14
  • 159
  • 270