Temporary lifetime extension

≡放荡痞女 提交于 2019-12-21 03:55:06

问题


The 12.2.5 section of standard says:

A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full-expression in which they are created and in the reverse order of the completion of their construction.

The code I try 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;
}

On line #1 a temporary object is created and bound to fooRef parameter of foo. fooRef is destroyed on line #0. So I thought the temporary should be destroyed here since lifetime-extension is not transitive.

Questions:

  1. What does until the function exits mean? Does it mean untill it finished executing?

  2. Why do I get a 5 output. Does a temporary object still exist on line #2?

  3. How can I interpret the standard quote to figure out how this example works?

Step-by-step atomic walk-through with references to the standard would be greatly appreciated. Thank you!

P. S. An accepted answer here also told the the code is broken and I do not get, why I get such output of program.


回答1:


What does until the function exits mean? Does it mean untill it finished executing?

Yes.

Why do I get a 5 output. Does a temporary object still exist on line #2?

Dereferencing a reference which is not bound to a living object is undefined behavior, so you may get 5 as well as 42 as well as anything else (including a crash). You simply cannot have any expectation on a program that has undefined behavior.

How can I interpret the standard quote to figure out how this example works?

Pretty much like you did already.The temporary gets bound to the function parameter fooRef, which gets destroyed when returning from the function. Since that temporary is bound to the returned value, that object ceases to exist when the function returns. Later on, you are dereferencing a dangling reference, which gives you UB.




回答2:


  1. It means until the closing brace, i.e. }.

  2. You invoked UB, you have a dangling reference.

Try the following modification of your code and see what it prints. It probably will print 6 because that is what was last on the stack. Or try passing a std::string instead, you might get a crash.

int main (void)
{
    const int& numberRef = foo(5);  
    foo(6);
    std::cout << numberRef;
    return 0;
}


来源:https://stackoverflow.com/questions/17362673/temporary-lifetime-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!