Search code examples
c++language-lawyerrvalue-referencelvaluelvalue-to-rvalue

Lvalue to rvalue conversion with integer pointer


If I read implicit conversions correctly:

Lvalue to rvalue conversion

A glvalue of any non-function, non-array type T can be implicitly converted to a prvalue of the same type. [..]

Unless encountered in unevaluated context (in an operand of sizeof, typeid, noexcept, or decltype), this conversion effectively copy-constructs a temporary object of type T using the original glvalue as the constructor argument, and that temporary object is returned as a prvalue.

Then why does this not work?

int* iptr = nullptr;
int*&& irr = iptr; // Cannot bind lvalue to rvalue reference

The type int* should have been implicitly converted to a prvalue of the same type via a temporary - thus binding to the rvalue reference without issues.


Solution

  • The standard states that this is ill-formed explicitly; lvalue-to-rvalue conversion won't (shouldn't) be considered.

    From [dcl.init.ref]/5.4.4:

    if the reference is an rvalue reference, the initializer expression shall not be an lvalue.

    [ Example:

    double d2 = 1.0;
    double&& rrd2 = d2;                 // error: initializer is lvalue of related type