使用const引用延长临时对象的寿命 [英] Prolonging life of a temporary object using const reference

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

问题描述

我需要对const引用进行一些说明. 来自此链接:

I need a few clarification regarding const reference. From this link:

const Foo &myFoo = FuncBar();

const引用延长了本地对象的寿命.但是,当我检查此链接时,尽管它们使用了const引用

const reference extended the life of the local object. But when I checked this link although they used a const reference

Sandbox(const string& n) : member(n) {}

字符串四"的生存期没有增加.

the lifetime of string "four" did not increase.

Sandbox sandbox(string("four"));

他们使用了句子

仅本地const引用会延长寿命.

Only local const references prolong the lifespan.

然后第二个链接中的字符串不是主函数本地的字符串"four",并且const引用n是否不应该延长其寿命?
那么为什么第二个链接中的寿命没有延长呢?

Then in the second link isn't the string "four" local to the main function and shouldn't the const reference n prolong its life?
So why isn't the lifetime getting prolonged in the second link?

推荐答案

您所引用的两个链接在某种意义上是不同的,一个链接显示使用本地const引用,另一个显示使用类成员const引用.

The two links which you have referred are different in the sense that one shows the use of a local const reference and other shows the use of a class member const reference.

当我们创建本地const引用并引用一个临时对象时,在此编译器中,它将延长临时对象的寿命,直到本地const引用的范围为止.

When we create local const references and refer to a temporary object then in this compiler extends the life of the temporary till the scope of local const reference.

指向临时成员的类成员const引用将导致意外的结果,因为临时对象的寿命不会超出为初始化类成员引用而调用的构造函数的范围.正如其中一个答案中所解释的那样,临时对象将一直存在直到构造函数完成为止.

Class member const reference pointing to temporary will lead to unexpected results as the life of the temporary object will not be extended beyond the constructor invoked to initialize class member reference. As explained in one of the answers the temporary will only survive till completion of the constructor.

引用以下答案: const引用是否可以延长临时文件的寿命?

生存期扩展不能通过函数参数传递. §12.2/5 [class.temporary]:

The lifetime extension is not transitive through a function argument. §12.2/5 [class.temporary]:

第二个上下文是将引用绑定到临时项时.引用所绑定的临时对象或作为与该临时对象所绑定的子对象的完整对象的临时对象在引用的生存期内一直存在,除非以下指定.在构造函数的ctor-initializer(第12.6.2节[class.base.init])中,绑定到引用成员的临时绑定将一直存在,直到构造函数退出为止.在函数调用(第5.2.2节[expr.call])中与参考参数的临时绑定将一直持续到包含该调用的完整表达式完成为止.

The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (§12.6.2 [class.base.init]) persists until the constructor exits. A temporary bound to a reference parameter in a function call (§5.2.2 [expr.call]) persists until the completion of the full expression containing the call.

如果正确分析它,您将意识到在两种情况下,临时生命的寿命都会延长,直到初始化引用的范围有效为止.一旦范围从引用超出范围,该临时范围即变为无效.

If you analyze it correctly you will realize that in both cases the life of temporary is extended till the scope from where the references are initialized is valid. As soon as the scope from where reference goes out of scope the temporary becomes invalid.

对于本地const引用,作用域位于函数内部(从该函数将其初始化为临时变量). 对于类成员const引用,scope是将其初始化为临时文件的构造函数.

For local const reference, scope is inside a function from where it is being initialized to a temp. For class member const reference, scope is constructor where it is being initialized to a temp.

您还应该阅读这篇GOTW文章: https://herbsutter .com/2008/01/01/gotw-88-a-candidate-for-the-最重要的const/

You should also read this GOTW article: https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/

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

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