[英]Temporary lifetime extension
The 12.2.5 section of standard says: 标准的12.2.5部分说:
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.
在函数调用(5.2.2)中与引用参数的临时绑定将持续存在,直到包含该调用的完整表达式完成为止。 A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits.
函数返回语句(6.6.3)中返回值的临时绑定将一直存在,直到函数退出。 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
. 在
#1
行,创建一个临时对象并绑定到foo
fooRef
参数。 fooRef
is destroyed on line #0
. fooRef
在#0
行被销毁。 So I thought the temporary should be destroyed here since lifetime-extension is not transitive. 所以我认为临时应该在这里销毁,因为生命延长不是传递性的。
Questions: 问题:
What does until the function exits
mean? until the function exits
意味着什么? Does it mean untill it finished executing
? 这是否意味着
untill it finished executing
?
Why do I get a 5
output. 为什么我得到
5
输出。 Does a temporary object still exist on line #2
? #2
行还存在临时对象吗?
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!
谢谢!
PS An accepted answer here also told the the code is broken
and I do not get, why I get such output of program. PS 这里接受的答案也告诉我代码被
broken
,我不知道,为什么我得到这样的程序输出。
What does until the function exits mean?
直到函数退出意味着什么? Does it mean untill it finished executing?
这是否意味着直到它完成执行?
Yes. 是。
Why do I get a 5 output.
为什么我得到5输出。 Does a temporary object still exist on line #2?
第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). 取消引用未绑定到活动对象的引用是未定义的行为 ,因此您可以获得
5
以及42
以及其他任何内容(包括崩溃)。 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. 就像你已经做的那样。临时绑定到函数参数
fooRef
,它从函数返回时被销毁。 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.
稍后,您将取消引用悬空引用,它会为您提供UB。
It means until the closing brace, ie }
. 这意味着直到结束括号,即
}
。
You invoked UB, you have a dangling reference. 你调用了UB,你有一个悬空引用。
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. 它可能会打印
6
因为这是堆栈中的最后一个。 Or try passing a std::string
instead, you might get a crash. 或者尝试传递
std::string
,你可能会崩溃。
int main (void)
{
const int& numberRef = foo(5);
foo(6);
std::cout << numberRef;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.