对匿名RVALUE的引用被损坏
Reference to anonymous rvalue is corrupted
为什么以下代码...
#include <iostream>
#include <map>
template< typename T, typename U >
class Map
{
public:
Map( const T& t, const U& u ) { map_[ t ] = u; }
Map< T, U >& operator() ( const T& t, const U& u )
{
map_[ t ] = u;
return *this;
}
U& operator[] ( const T& t) { return map_[ t ]; }
private:
std::map< T, U > map_;
};
Map< int, std::string >& g_map = Map< int, std::string> ( 1, "lorem" )
( 3, "ipsum" )
( 5, "dolor" );
int main( int argc, char* argv[] )
{
std::cout << g_map[3] << std::endl;
return 0;
}
...产生这种损坏的输出?...
>g++ -g main.cpp
>./a.out
ipsumÿÿÿÿlorem!h€Ap€AD€A!ˆ€A¼gì¿P€A€A,€A!p€A€AY
我最近了解到,分配对匿名rvalue的引用会延长rvalue对象的寿命。因此,我认为,由于匿名rvalue std::map
是由全球范围g_map
提到的,因此它的寿命将延长到全球范围变量的寿命,并且将使用g_map
作为其他任何全球变量(如果不是的话(是有效的供参考,匿名rvalue将在结束的半隆上死亡(。
有人可以解释终身扩展规则如何适用上述?
用GCC 4.9.2。
您本质上拥有这个:
class C {
public:
C& detemporize() { return *this; }
};
C& cr = C().detemporize();
创建了一个临时C
实例。然后在其上调用一种方法,该方法返回C&
参考。编译器不知道也不关心回报值是指同一临时性的;虽然知道,它很可能会返回对某些全球,长寿的对象的引用。
无论如何,cr
最终指的是那个临时性,然后立即死亡,而cr
悬挂。随后使用它的任何尝试都表现出不确定的行为。
在您的代码中,Map::operator()
扮演detemporize()
的角色,使g_map
悬挂参考。
我最近了解到,分配对匿名rvalue的参考会延长rvalue对象的寿命。
仅当您将临时对象直接分配给参考时才发生:
const obj &ref1 = obj(); // extends
const obj &ref = somefuncthatreturnsobj(); // extends
但是没有魔法,如果您调用一个函数,该函数以某种方式隐藏了引用它不再起作用:
class foo {
const foo &get() const { return *this; };
};
const foo &ref1 = foo(); // extends lifetime of temporary
const foo &ref2 = foo().get(); // no lifetime extention, dangling reference
相关文章:
- 为什么会发生堆损坏
- 将对象数组的引用传递给函数
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 对匿名RVALUE的引用被损坏
- 通过引用将变量传递给多个线程会损坏堆
- 引用可能已损坏的静态对象
- 为什么每次生成时,本地 .NET 程序集在引用列表中(几乎)都显示为损坏
- 从工厂函数返回右值引用时堆/内存损坏
- 移动所有者时,对所有者的C++复合引用已损坏
- 在Visual Studio调试模式下运行时,c++对象引用已损坏
- 重新定义损坏的引用
- C++对象引用损坏
- 删除引用时的双重自由或损坏