Home > Backend Development > C++ > Is `int x = x;` Undefined Behavior Due to Lvalue-to-Rvalue Conversion?

Is `int x = x;` Undefined Behavior Due to Lvalue-to-Rvalue Conversion?

Linda Hamilton
Release: 2024-12-17 00:45:24
Original
491 people have browsed it

Is `int x = x;` Undefined Behavior Due to Lvalue-to-Rvalue Conversion?

Lvalue-to-Rvalue Conversion and Undefined Behavior in int x = x;

In C , the question of whether assigning an uninitialized value to itself constitutes undefined behavior has been a topic of debate. The standard (3.3.2) declares that initializing a variable with itself, like in the example int x = x;, can lead to unexpected outcomes.

The crux of the issue lies in the potential requirement for lvalue-to-rvalue conversion in such assignments. Suppose the right-hand x is an uninitialized automatic variable with an indeterminate value. According to the standard (4.1), performing an lvalue-to-rvalue conversion on such a value results in undefined behavior.

Evidence for Lvalue-to-Rvalue Conversion

Despite the absence of explicit requirements in the standard, various implications suggest that the primary intention is for built-in operators to expect prvalues by default. For instance, the standard notes that built-in assignment operators entail lvalue-to-rvalue conversion for the right operand. Other provisions also indicate that lvalue-to-rvalue conversion is generally anticipated except when explicitly specified otherwise.

Extending the Conjecture to Initializers

While initialization is distinct from assignment, the standard's ambiguity regarding value categories could potentially extend to this area. Evidence suggests that the intended specification is to expect prvalues wherever a value is needed.

Consequences for int x = x;

Assuming the conjecture holds true, initializing x with itself would require lvalue-to-rvalue conversion on an uninitialized value, leading to undefined behavior.

Supporting Evidences

Further evidence arises from the consideration of code that assigns uninitialized variables with different types and different assignments. Inconsistent behavior, such as preventing lvalue-to-rvalue conversion on reference initialization but permitting it on object initialization, adds credence to the view that lvalue-to-rvalue conversion is expected for copy-initialization.

Additionally, a defect report aims to introduce explicit language redefining object values from "uninitialized" to "indeterminate value" in the context of undefined behavior during lvalue-to-rvalue conversion. This suggests the intent to establish undefined behavior in cases like int x = x;.

Conclusion

Based on the evidences presented, it is reasonable to conclude that lvalue-to-rvalue conversion is required in the initialization int x = x;. This, in turn, leads to undefined behavior due to the uninitialized nature of the right-hand x. The standard's inconsistencies and need for clarification highlight the importance of detailed specifications for value categories.

The above is the detailed content of Is `int x = x;` Undefined Behavior Due to Lvalue-to-Rvalue Conversion?. 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