This is a common pattern I use to index tokens as they come in: check if the token is in a map, and if not, add it to the map, assigning the map's size.
When doing this in C++, it unexpectedly increments the map's size before the assignment is made:
#include <cstdio>
#include <map>
using namespace std;
int main() {
map<char, int> m;
printf("Size before adding: %d\n", m.size());
m['A'] = m.size();
printf("Size after adding: %d\n", m.size());
printf("What was added: %d\n", m['A']);
return 0;
}
This prints out:
Size before adding: 0
Size after adding: 1
What was added: 1
The way I understand it, it should evaluate the right hand side, which is zero, then pass it to a function that puts 'A' and the zero into the map. But it seems to be evaluating it after it started assigning, which doesn't make sense.
Shouldn't the right-hand side be evaluated before the assignment operation?