问题描述
我的代码如下所示:
class T {};
class container {
const T &first, T &second;
container(const T&first, const T & second);
};
class adapter : T {};
container(adapter(), adapter());
我认为常量引用的生命周期就是容器的生命周期.但是,如果不是这样,适配器对象在容器创建后被销毁,留下悬空引用.
I thought lifetime of constant reference would be lifetime of container. However, it appears otherwise, adapter object is destroyed after container is created, leaving dangling reference.
什么是正确的生命周期?
What is the correct lifetime?
适配器临时对象的堆栈范围是容器对象的范围还是容器构造函数的范围?
is stack scope of adapter temporary object the scope of container object or of container constructor?
如何正确实现绑定临时对象到类成员引用?
how to correctly implement binding temporary object to class member reference?
谢谢
推荐答案
根据 C++03 标准,到引用的临时绑定具有不同的生命周期,具体取决于上下文.在您的示例中,我认为下面突出显示的部分适用(12.2/5临时对象"):
According to the C++03 standard, a temporary bound to a reference has differing lifetimes depending on the context. In your example, I think the highlighted portion below applies (12.2/5 "Temporary objects"):
引用绑定到的临时对象或作为临时对象绑定的子对象的完整对象的临时对象将在引用的生命周期内持续存在,除非下面指定.临时绑定到构造函数的 ctor-initializer (12.6.2) 中的引用成员,直到构造函数退出.在函数调用 (5.2.2) 中与引用参数的临时绑定一直存在,直到包含调用的完整表达式完成为止.
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) persists until the constructor exits. 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.
因此,虽然绑定临时对象是一种延长临时对象生命周期的高级技术(GotW #88: A Candidate For the "Most Important const"),在这种情况下它显然对你没有帮助.
So while binding a temporary is an advanced technique to extend the lifetime of the temporary object (GotW #88: A Candidate For the "Most Important const"), it apparently won't help you in this case.
另一方面,Eric Niebler 有一篇您可能感兴趣的文章,其中讨论了一种有趣的(如果令人费解的)技术,该技术可以让您的类的构造函数推断是否已将临时对象(实际上是一个右值)传递给它(因此必须被复制)或传递的非临时(左值)(因此可能安全地隐藏引用而不是复制):
On the other hand, Eric Niebler has an article that you may be interested in that discusses an interesting (if convoluted) technique that could let your class's constructors deduce whether a temporary object (actually an rvalue) has been passed to it (and therefore would have to be copied) or a non-temporary (lvalue) as been passed (and therefore could potentially safely have a reference stashed away instead of copying):
- 有条件的爱:FOREACH Redux
不过祝你好运 - 每次我阅读这篇文章时,我都必须像以前从未见过这些材料一样仔细研究所有内容.它只在我身边短暂停留......
Good luck with it though - every time I read the article, I have to work through everything as if I've never seen the material before. It only sticks with me for a fleeting moment...
我应该提到 C++0x 的右值引用应该使 Niebler 的技术变得不必要.计划在一周左右发布的 MSVC 2010 将支持右值引用(如果我没记错的话,是 2010 年 4 月 12 日).不知道 GCC 中右值引用的状态是什么.
And I should mention that C++0x's rvalue references should make Niebler's techniques unnecessary. Rvalue references will be supported by MSVC 2010 which is scheduled to be released in a week or so (on 12 April 2010 if I recall correctly). I don't know what the status of rvalue references is in GCC.
这篇关于C++ 常量引用生命周期(容器适配器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!