Is an lvalue reference returned from a function actually an rvalue (from the perspective of the caller)?

joh*_*ell 1 c++ reference rvalue lvalue c++11

How is it possible to have an lvalue reference refer to an lvalue reference returned from a function? Isn't the lvalue reference returned from that function actually an rvalue from the perspective of the caller? For example:

class Obj {};

Obj& refToRef(Obj& o) {
    return o;
}

int main() {
    Obj o;
    Obj& o2 = refToRef(o);
}
Run Code Online (Sandbox Code Playgroud)

How is o2 (an lvalue reference) able to refer to what appears to be an rvalue?

Mic*_*zel 8

从调用者的角度来看,从该函数返回的左值引用实际上不是右值吗?

不,因为函数调用的结果并不总是右值。

[expr.call] / 14(重点是我的):

如果结果类型是左值引用类型或对函数类型的右值引用,则函数调用为左值;如果结果类型是对对象类型的右值引用,则函数调用为xvalue;否则为prvalue。

我敢肯定,这条规则是专门为避免您的问题所提出的问题而存在的。