Consider the below code:
#include <iostream>
int func(){
int a = 0;
return a;
}
int main(){
int result = func();
}
According to the cpp standard, some rules about the return statement 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
So, the invocation for int result = func();, as if it could be translate to:
//a fiction code
func(){
int a = 0;
int result = a; #1
}
Because a is a glvalue, it should be converted to prvalue for prvalue evaluation (initialize an object). So my question is, while invocating int result = func(); in the body of func, does the glvalue a which as the operand of return, need to be converted to a prvalue?
return statementrules,copy-initialize an object from the operand ofreturnand according to thecopy-initializerules,hence gvalue need to be converted to prvalue for initialization purpose. As the answers @M.M has given - xmh0511