c++ - Why is `i = ++i + 1` unspecified behavior? -


Consider the following C ++ standard ISO / IEC 14882: 2003 (E) quote (section 5, paragraph 4):

Where it has been said, the order of evaluation of sub-reactions of different operators and individual expressions and order in the order of adverse effects is specified in an unspecified manner. 53) It will have a one time modified value by evaluating expression near a scalar object between the previous and next sequence point. In addition, the first value will be used only to determine the value of the archiving. The requirements of this article will be completed for each acceptable order of sub-reactions of full expression; Otherwise the behavior is undefined. [Example:

  i = v [i ++]; // The behavior is unspecified i = 7, i ++, i ++; // I 9 i = ++ i + 1 becomes; // The behavior is unspecified i = i + 1; // I has increased the value  

-add example]

I was surprised that i = ++ i + 1 i Does any one compiler detect the implementation which does not give 2 for the following case?

  int i = 0; I = ++ I + 1; Std :: cout & lt; & Lt; I & lt; & Lt; Std :: endl;  

The thing is that operator = has two arguments first one is always the i reference is the order of evaluation in this case Does not matter. I do not see any problem except the C ++ criteria.

Please , do not do consider such cases where the order of reasoning is important for evaluation. For example, ++ i + i is clearly undetermined, please consider only my case i = ++ i + 1 .

Why does C ++ standard stop such expressions?

You form the two-argument function of operator = Make a mistake in thinking , where the argument should be thoroughly evaluated before the start of the side effects function. If this is the case, then there will be several sequence points in the expression i = + i + 1 and before the assignment starts ++ i will be fully evaluated. This is not the case, though. What is being evaluated in internal assignment operator is not a user-defined operator, that expression has only one sequence point.

> Assignment (and before the additional operator) of is evaluated, but side effects Not necessarily immediately implemented. The result of ++ i + 1 is always the same as i + 2 , so this value, which is assigned as part of i The result of the assignment operator ++ i is always i + 1 , so that the amount assigned to i as part of the payroll operator

There is no sequence of point in violation of this rule.

Because the code is violating the rule because "the value of a scalar object between the previous and next sequence points will be most often revised, evaluation of an expression," the behavior is undefined. Practically , however, it is possible that either i + 1 or i + 2 will be assigned first, then the second value will be assigned , And finally the program will continue normally - no nose monsters or explosive toilets, and no one i + 3 no.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -