What standard clause mandates this lvalue-to-rvalue conversion?

后端 未结 5 2017
独厮守ぢ
独厮守ぢ 2020-12-03 16:00

Given:

int main() {
   int x = 0;
   int y = x; // <---
}

Could someone please tell me which clause of the standard (2003 preferred) man

5条回答
  •  情深已故
    2020-12-03 16:15

    I do believe that this is intuitive to some degree (what others already said - the value is needed, so there is an obvious need to convert the object designator to the value contained therein). The best I could come up with, by 4p3:

    An expression e can be implicitly converted to a type T if and only if the declaration "T t=e;" is well-formed, for some invented temporary variable t (8.5). The effect of the implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The result is an lvalue if T is a reference type (8.3.2), and an rvalue otherwise. The expression e is used as an lvalue if and only if the initialization uses it as an lvalue.

    Note the "if and only if" at the end - the initializer therefor is used as an rvalue, because the initialization uses it as an rvalue (result of the conversion). So by 3.10p7

    Whenever an lvalue appears in a context where an rvalue is expected, the lvalue is converted to an rvalue; see 4.1, 4.2, and 4.3.


    EDIT: The paragraph for entering 4p3 can be found at 8.5p16, last bullet:

    Otherwise, the initial value of the object being initialized is the (possibly converted) value of the initializer expression.

    Also note the comments below.

提交回复
热议问题