C++ 常量引用生存期(容器适配器)
我有这样的代码:
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:最重要的常量"的候选者),在这种情况下它显然无济于事.
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 将支持 Rvalue 引用,该版本计划在一周左右发布(如果我没记错的话,将在 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.
相关文章