c++rvaluelvaluec++23

Cannot return a named rvalue reference in a function with return type of lvalue reference?


In C++, a named rvalue reference is lvalue. Why the below code yields "error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'"?

int & unmove(int && t)
{
    return t;
}

And this code works:

int & unmove(int && t)
{
    int & r = t;
    return r;
}

Solution

  • Since C++23,

    xvalue expressions include now move-eligible expressions.

    So in return t;, t is now a xvalue expression,
    but int& r = t;, t is still a lvalue expression.