Is it valid to bind non-const lvalue-references to rvalues in C++ 11?(modified)

前端 未结 2 1551
星月不相逢
星月不相逢 2020-11-29 11:56

I know in c++03, an an non-const reference cannot be bound to rvalues.

T& t = getT(); is invalid, and in c++11, we can do this: T&& t

相关标签:
2条回答
  • 2020-11-29 12:10

    should that work in c++11?

    No, it should not.

    Foo is a custom class, I don't understand why the first two line compiles

    It compiles only with MSVC. MSVC has an (arguably useful) compiler extension that allows binding lvalues of user-defined types to rvalues, but the Standard itself forbids this. See, for instance, this live example where GCC 4.7.2 refuses to compile your code.

    Does the standard say anything about this?

    Indeed it does. Per paragraph 8.5.3/5 of the C++11 Standard:

    A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:

    — If the reference is an lvalue reference and the initializer expression

    — is an lvalue (but is not a bit-field), and “cv1 T1” is reference-compatible with “cv2 T2,” or

    — has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be implicitly converted to an lvalue of type “cv3 T3,” where “cv1 T1” is reference-compatible with “cv3 T3” [...],

    then the reference is bound to the initializer expression lvalue in the first case and to the lvalue result of the conversion in the second case (or, in either case, to the appropriate base class subobject of the object). [...]

    [ ...]

    Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference. [ Example:

    double& rd2 = 2.0; // error: not an lvalue and reference not const
    int i = 2;
    double& rd3 = i; // error: type mismatch and reference not const
    

    —end example ]

    0 讨论(0)
  • 2020-11-29 12:25

    No you can't bind a temporary to a non-const lvalue reference.

    T f();
    
    T& t1 = f(); // won't compile
    const T& t2 = f(); // OK
    T&& t3 = f(); // OK
    

    This is a safety feature. Mutating a temporary by an lvalue that is about to die anyway is most likely a logic error, so it isn't allowed by the language.

    Note that due to the RVO than in practice:

    T&& t3 = f();
    

    and

    T t3 = f();
    

    are equivilent.

    0 讨论(0)
提交回复
热议问题