I want to trim a string in C++ with this code:
std::string str(" Trim test ");
str.erase( /* 1 */
0, /* 2 */
str.find_first_not_of(" ") /* 3 */
) /* 4 */
.erase( /* 5 */
str.find_last_not_of(" ") + 1, /* 6 */
std::string::npos /* 7 */
); /* 8 */
Does the standard allow line #6 to be calculated before line #1 is executed, so that when #5 is finally called, the argument might no longer be valid?
Yes, the standard allow line #6 to be calculated before line #1 is executed.
From the Wikipedia page on sequence points:
You should rewrite it as