The code below demonstrates this difference:
#include <iostream>
#include <string>
int main()
{
char s[] = "ABCD";
std::string str(s);
char *p = s;
while(*p) {
*p++ = tolower(*p); // <-- incr after assignment
}
std::cout << s << std::endl;
std::string::iterator it = str.begin(), end = str.end();
while(it != end) {
*it++ = tolower(*it); // <-- incr before assignment ?
}
std::cout << str << std::endl;
return 0;
}
it produces output:
abcd
bcd
if we separate assignment operation and increment operator:
while(it != end) {
*it = tolower(*it); // <-- incr before assignment ?
it++;
}
the output will be as expected.
What’s wrong with the original code?
$ g++ --version
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
The problem is that the order of evaluation of arguments of
operator=is unspecified. This is according to C++ Standard 5.2.2/8. Consider the following:is equal to
Now
*it++could be computed beforetolower(*it)and vice versa.