延长寿命 [英] Temporary lifetime extension

查看:97
本文介绍了延长寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准的12.2.5部分说:

The 12.2.5 section of standard says:

与函数调用中的参考参数的临时绑定(5.2.2) 一直持续到包含 称呼.在函数return中临时绑定到返回值 语句(6.6.3)一直保留到函数退出.在所有这些 情况下,在评估表达式时创建的临时对象 初始化引用,但临时引用除外 引用已绑定,在全表达式末尾被销毁 它们是按照创建的相反顺序完成的 他们的建筑.

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.

我试图理解的代码是:

#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;
}

在行#1上创建一个临时对象,并将其绑定到foofooRef参数. fooRef在行#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.

问题:

  1. until the function exits是什么意思?它是untill it finished executing的意思吗?

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

为什么会得到5输出. #2行上仍然存在临时对象吗?

Why do I get a 5 output. Does a temporary object still exist on line #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!

P. S. 此处的接受答案也告诉了代码是broken,我不明白,为什么我得到这样的程序输出.

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.

推荐答案

函数退出之前是什么意思?这是否意味着直到执行完毕?

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

是的

为什么我得到5输出.在第2行上还存在一个临时对象吗?

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

取消引用未绑定到活动对象的引用是未定义的行为,因此您可能会获得542以及其他任何内容(包括崩溃).对于具有未定义行为的程序,您根本无法抱有任何期望.

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?

就像您已经做过的一样.临时变量绑定到函数参数fooRef,该参数在从函数返回时被销毁.由于该临时对象已绑定到返回值,因此该对象在函数返回时将不复存在.稍后,您将取消引用悬空的引用,从而获得UB.

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.

这篇关于延长寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆