Home > Backend Development > C++ > Why Did \'i = i 1;\' Become a Legitimate Operation in C 17?

Why Did \'i = i 1;\' Become a Legitimate Operation in C 17?

Susan Sarandon
Release: 2024-10-29 03:19:02
Original
1071 people have browsed it

Why Did

Why i = i 1; Became Legal in C 17

In C 11, the code snippet "i = i 1;" invoked undefined behavior. However, in C 17, this code became legally acceptable. This change stems from the enhanced sequencing guarantees introduced in C 17.

According to the C 11 standard [N3337 basic.exec], the sequencing of side effects is unsequenced unless explicitly specified. In the example code, the assignment operator (=) and the postfix increment operator ( ) both have side effects that can lead to undefined behavior if not correctly sequenced.

In C 17, a new sentence was added to [N4659 expr.ass] that clarifies the sequencing rules for the assignment operator:

The right operand is sequenced before the left operand.
Copy after login

This additional statement ensures that all side effects within the right operand are executed before any side effects within the left operand, including the assignment itself.

In the case of "i = i 1;", the right operand (i 1) consists of the postfix increment operator (i ) and the constant 1. The sequencing guarantees introduced in C 17 ensure that the postfix increment is performed and its side effect occurs before the assignment is executed. This eliminates the undefined behavior that was present in C 11.

Therefore, the enhanced sequencing guarantees in C 17 have made "i = i 1;" legal by ensuring that the side effects of the right and left operands are correctly sequenced, preventing undefined behavior.

The above is the detailed content of Why Did 'i = i 1;' Become a Legitimate Operation 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