Standard Section 12.2.5 states:
The temporal reference to the reference parameter in the function call (5.2.2) is maintained until the completion of the full expression containing the call. The temporary binding to the return value in the return function (6.6.3) is preserved until the function exits. In all these cases, temporary, created during the evaluation of the expression, the initialization of the link, with the exception of the temporary link is connected, are destroyed at the end of the full expression in which they are created, and in the opposite order of completion.
The code I'm trying to understand is:
#include <iostream> const int& foo(const int& fooRef) { return fooRef; } // #0 int main (void) { const int& numberRef = foo(5); // #1 std::cout << numberRef; // #2 return 0; }
Line #1 creates a temporary object and is bound to the fooRef parameter foo . fooRef destroyed on line #0 . Therefore, I thought that temporary destruction should be destroyed here, since life extension is not transitive.
Questions:
What does until the function exits ? Does this mean untill it finished executing ?
Why am I getting pin 5 . Does the object temporarily exist on line #2 ?
How can I interpret a standard quote to figure out how this example works?
It is understood that phased atomic passage with reference to the standard. Thanks!
R. S. The accepted answer here also said the code is broken , and I do not understand why I get this output from the program.
source share