Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 562
zwhconst Avatar asked Nov 03 '25 08:11

zwhconst


1 Answers

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.

like image 169
Jarod42 Avatar answered Nov 04 '25 21:11

Jarod42