#include <iostream>
int func0(){
int a = 0;
return a;
}
int&& func1(){
int a = 0;
return a;
}
int main(){
int&& result0 = func0();
int&& result1 = func1();
}
return statement
rules are:
- A function returns to its caller by the return statement.
- [...] the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand.
The rule about how to initialize the object of the function call is only #2.
We know the expression func0()
is a prvalue. The reference result0
need to bind an object, so temporary materialization conversion
shall covert the prvalue to an xvalue, So the temporary object as the prvalue result object is initialized from the operand of return
, then the reference reusult0
bind to the temporary object.
But we know the result1
is a reference and the return type of func1
is also reference. For this case, [stmt.return] does not clearly cover this case, because result1
is a reference rather than object
(neither glvalue reuslt object nor prvalue result object), so what the rules about this case? If I miss something, please correct me.