Files
cppdraft_translate/cppdraft/conv/lval.md
2025-10-25 03:02:53 +03:00

4.5 KiB
Raw Blame History

[conv.lval]

7 Expressions [expr]

7.3 Standard conversions [conv]

7.3.2 Lvalue-to-rvalue conversion [conv.lval]

1

#

A glvalue of a non-function, non-array type T can be converted to a prvalue.43

If T is an incomplete type, a program that necessitates this conversion is ill-formed.

If T is a non-class type, the type of the prvalue is the cv-unqualified version of T.

Otherwise, the type of the prvalue is T.44

2

#

When an lvalue-to-rvalue conversion is applied to an expression E, and either

E is not potentially evaluated, or

the evaluation of E results in the evaluation of a member Ex of the set of potential results of E, and Ex names a variable x that is not odr-used by Ex ([basic.def.odr]),

the value contained in the referenced object is not accessed.

[Example 1: struct S { int n; };auto f() { S x { 1 };constexpr S y { 2 };return [&](bool b) { return (b ? y : x).n; };}auto g = f();int m = g(false); // undefined behavior: access of x.n outside its lifetimeint n = g(true); // OK, does not access y.n — end example]

3

#

The result of the conversion is determined according to the following rules:

  • (3.1)

    If T is cv std::nullptr_t, the result is a null pointer constant ([conv.ptr]). [Note 1: Since the conversion does not access the object to which the glvalue refers, there is no side effect even if T is volatile-qualified ([intro.execution]), and the glvalue can refer to an inactive member of a union ([class.union]). — end note]

  • (3.2)

    Otherwise, if T has a class type, the conversion copy-initializes the result object from the glvalue.

  • (3.3)

    Otherwise, if the object to which the glvalue refers contains an invalid pointer value ([basic.compound]), the behavior isimplementation-defined.

  • (3.4)

    Otherwise, if the bits in the value representation of the object to which the glvalue refers are not valid for the object's type, the behavior is undefined. [Example 2: bool f() {bool b = true; char c = 42; memcpy(&b, &c, 1); return b; // undefined behavior if 42 is not a valid value representation for bool} — end example]

  • (3.5)

    Otherwise, the object indicated by the glvalue is read ([defns.access]). Let V be the value contained in the object. If T is an integer type, the prvalue result is the value of type T congruent ([basic.fundamental]) to V, andV otherwise.

4

#

[Note 2:

See also [basic.lval].

— end note]

43)43)

For historical reasons, this conversion is called the “lvalue-to-rvalue” conversion, even though that name does not accurately reflect the taxonomy of expressions described in [basic.lval].

44)44)

In C++ class and array prvalues can have cv-qualified types.

This differs from C, in which non-lvalues never have cv-qualified types.