Home >Backend Development >C++ >Why is `i = i 1;` now legal in C 17?
C 17 Legalizes i = i 1; Due to Revised Operator Evaluation Order
In C 11, the assignment i = i 1; was considered undefined behavior. However, in C 17, it became a valid expression. To understand this change, we need to delve into the revised rules for operator evaluation.
Pre-C 17 Undefined Behavior
In C 11, the sequencing of operand evaluations for assignment operators was defined as follows:
In i = i 1;, the post-increment i exhibits side effects (modifying the value of i), which could occur interleaved with the assignment operación. This potential for unsequenced side effects led to undefined behavior.
C 17's Revised Sequencing Rule
C 17 introduced a new sequencing rule for assignment operators:
This revised rule applies to both the value computations and side effects of the right operand.
Legalization of i = i 1;
In i = i 1;, the right operand is i 1. The evaluation of this expression involves the postfix increment of i within the parentheses, followed by the addition of 1. According to the new rule, this entire evaluation is sequenced and completed before the assignment to i.
Therefore, in C 17, the act of modifying i (side effect) is completed before the actual assignment, eliminating the potential for unsequenced side effects. This revised sequencing makes the i = i 1; expression legal in C 17.
The above is the detailed content of Why is `i = i 1;` now legal in C 17?. For more information, please follow other related articles on the PHP Chinese website!