延长临时人寿的理由是什么? [英] What is the rationale for extending the lifetime of temporaries?

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

问题描述

在C ++中,临时值的生命周期可以通过绑定到引用来扩展:

In C++, the lifetime of a temporary value can be extended by binding it to a reference:

Foo make_foo();

{
    Foo const & r1 = make_foo();
    Foo && r2 = make_foo();

    // ...
}             // both objects are destroyed here


$ b b

为什么允许这样做?这个问题解决了什么问题?

Why is this allowed? What problem does this solve?

我在设计和进化中找不到这样的解释(例如6.3.2:Temporaries )。我也没有找到任何先前的问题(这一个最接近)。

I couldn't find an explanation for this in Design and Evolution (e.g. 6.3.2: Lifetime of Temporaries). Nor could I find any previous questions about this (this one came closest).

此功能有点不直观,并有微妙的失败模式。例如:

This feature is somewhat unintuitive and has subtle failure modes. For example:

Foo const & id(Foo const & x) { return x; }  // looks like a fine function...

Foo const & r3 = id(make_foo());             // ... but causes a terrible error!

为什么可以这么容易和默默地滥用部分语言?

Why is something that can be so easily and silently abused part of the language?

更新:这一点可能微妙到需要澄清:我不反对使用规则引用绑定到临时。这是很好的,并且允许我们在绑定到引用时使用隐式con­ ver­ sions。我问的是为什么临时的生命周期受到影响。为了玩魔鬼的倡导者,我可以声明生存期直到完全表达式结束的现有规则已经涵盖了使用临时变元调用函数的常见用例。

Update: the point may be subtle enough to warrant some clarification: I do not dispute the use of the rule that "references bind to temporaries". That is all fine and well, and allows us to use implicit con­ver­sions when binding to references. What I am asking about is why the lifetime of the temporary is affected. To play the devil's advocate, I could claim that the existing rules of "lifetime until end of full expression" already cover the common use cases of calling functions with temporary arguments.

推荐答案

简单的答案是,你需要能够绑定一个临时与一个const引用,没有该功能将需要大量的代码重复,函数 const& 用于lvalue或value参数,或者by-value用于rvalue参数。
一旦你需要该语言需要定义一些语义,将保证临时的生命周期至少与引用的一样长。

The simple answer is that you need to be able to bind a temporary with a const reference, not having that feature would require a good amount of code duplication, with functions taking const& for lvalue or value arguments or by-value for rvalue arguments. Once you need that the language needs to define some semantics that will guarantee the lifetime of the temporary is at least as long as that of the reference.

一次您接受引用可以绑定到一个上下文中的右值,只是为了一致性,您可能想要扩展规则以允许在其他上下文中的相同绑定,并且语义真的是相同的。

Once you accept that a reference can bind to an rvalue in one context, just for consistency you may want to extend the rule to allow the same binding in other contexts, and the semantics are really the same. The temporary lifetime is extended until the reference goes away (be it a function parameter, or a local variable).

另一种方法是在某些上下文中允许绑定的规则(例如,一个函数参数或一个局部变量)函数调用),但不是所有(本地引用)或规则允许两者,并且在后一种情况下总是创建悬挂引用。

The alternative would be rules that allow binding in some contexts (function call) but not all (local reference) or rules that allow both and always create a dangling reference in the latter case.

从答案中删除引号,留在这里,以便注释仍然有意义:

Removed the quote from the answer, left here so that comments would still make sense:

如果你看看标准中的措辞有一些提示

If you look at the wording in the standard there are some hints as of this intended usage:


12.2 / 5 [段落中间]
[...]函数调用(5.2.2)中的引用参数将持续到包含调用的完整表达式完成为止。 [...]

这篇关于延长临时人寿的理由是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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