std::map<int, int> my_map;
my_map[0] = my_map.size();
Then, will my_map[0] be 0 or 1, or undefined?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no sequence point here, so the evaluation order is unspecified (or, in C++11 jargon, the two expressions are indeterminately sequenced).
The value of
my_map[0]is0or1depending on the implementation.my_map[0]will increase the size if its evaluated first, causingmy_map.size()to evaluate to1.my_map[0]will then be1.my_map.size()is evaluated first, then the value formy_map[0]will be0.Now, how to make the above behavior well-defined? You must introduce a sequence point, that is, force one expression to be sequenced before the other; for behavior like the first,
… or, for behavior like the second,
Make sure to upvote Oo Tiib for being the first to demonstrate how to introduce sequencing for the expressions 🙂