Home > Backend Development > C++ > Why is `i = i 1;` now legal in C 17?

Why is `i = i 1;` now legal in C 17?

Barbara Streisand
Release: 2024-11-01 06:38:01
Original
714 people have browsed it

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:

  • The value computations of the operands were sequenced before the assignment operation.
  • Side effects on a scalar variable were unsequenced relative to both other side effects and value computations.

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:

  • The right operand is sequenced before the left operand.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template