int a = 20;
int& fun(){
return a;
}
int main(){
fun();
return 0;
}
Is the reference returned by func rvalue by itself?
No. If a function returns an lvalue reference, then calling it gives you an lvalue.
If it returns an rvalue reference, you get an xvalue, and if it returns by value, you get a prvalue.
Also, strictly saying the result of calling the function is not a reference. It's an lvalue expression of type int
.
In general, you can determine value category of any expression by applying decltype
to it. It will add reference-ness to the type of the expression, indicating its category: &
for lvalues, &&
for xvalues, and nothing for prvalues.
Expressions themselves can't have reference types, so this added reference-ness will not conflict with their type.
For example, decltype(fun())
is int &
, proving that it's an lvalue.
Even though doesn't apply in this case, note that decltype
handles variable names differently from any other expressions (giving you the type of the variable, rather than expression type + value category). The workaround is to add a second set of (...)
around the variable name, which makes decltype
treat it as any other expression.