Home > Backend Development > C++ > Does C Initialization Require Lvalue-to-Rvalue Conversion?

Does C Initialization Require Lvalue-to-Rvalue Conversion?

Susan Sarandon
Release: 2024-12-15 12:33:08
Original
810 people have browsed it

Does C   Initialization Require Lvalue-to-Rvalue Conversion?

Does Initialization Entail Lvalue-to-Rvalue Conversion?

The C 11 Standard remains ambiguous on whether initialization requires lvalue-to-rvalue conversion. However, evidence suggests that rvalues are expected as operands in built-in operators, unless specified otherwise. This approach has been extended to the context of initializers.

Conjecture: Prvalues Expected for Values

The assumed specification is that prvalues are expected wherever a value is needed, including during initialization. This implies that an lvalue-to-rvalue conversion is required when initializing an object.

Consequences of the Conjecture

If initialization expects prvalues, the following occur:

  • int x = x; undergoes lvalue-to-rvalue conversion, leading to undefined behavior because x is uninitialized.
  • Ill-formedness for code like int y = 0; int x = y;, where y is an lvalue.
  • Inconsistent behavior, e.g. z = y being undefined behavior but z = x not being so, assuming x == y.

Further Evidence

Additional evidence supporting this conjecture includes:

  • Paragraph 8.5/16 of the Standard implies the need for "standard conversions" but does not mention category conversions.
  • Paragraph 4.1 mentions undefined behavior when applying lvalue-to-rvalue conversion to an uninitialized object.
  • A defect report proposes rewording Paragraph 4.1 to include objects with indeterminate values, including uninitialized objects.

Conclusion

Based on the provided evidence, the most likely interpretation is that initialization expects prvalues, resulting in undefined behavior when using uninitialized lvalues as initializers. This interpretation aligns with the broader approach of expecting rvalues in built-in operators unless specified otherwise.

The above is the detailed content of Does C Initialization Require 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