在这种情况下,unique_ptr的行为应该是什么?
What should the behavior of unique_ptr be in this situation?
假设我有以下内容:
std::unique_ptr<A> pA;
pA(new A);
在这个复杂的例子中,pA(new A);
的行为应该是什么?
据我所知,在MSVC2010中,default_delete中的void operator()(T*) const;
在new
返回并立即删除指针后被调用。而g++(4.7.0)给了我no match for call (std::unique_ptr<A>)(A*)
错误
代码不能编译。std::unique_ptr
不重载operator()
。
Visual c++ 2011 Developer Preview正确地拒绝了这些代码。Visual c++ 2010只接受std::unique_ptr
实现中的一个bug。
MSVC对unique_ptr
使用了无状态的删除器优化,即它利用了空基类优化,只是继承了删除器。不幸的是,继承是public
,这就是为什么您可以访问default_delete
函子的重载operator()
。
相关文章:
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 在这种情况下,如何传递成员函数而不是函数?
- 在这种情况下,"typename..."意味着什么?
- 在这种情况下是私有的吗?试图使操作员<<过载
- 在这种情况下,int**在C中是什么意思
- 在这种情况下,括号有什么好处?
- 在这种情况下,#define 的作用是什么?
- 在这种情况下,我们可以使用静态而不是朋友吗,还有其他解决方案是什么
- 在这种情况下,删除指针数组期间会发生什么?
- 在这种情况下,有什么正确的方法可以实现锁定吗?
- 在这种情况下,UTF-16 和 UTF-32 的 BOM 是强制性的
- 为什么在这种情况下它不是一个 constexpr?
- 在这种情况下,* 有什么作用?:
- 在这种情况下,从左到右的结合性是什么意思
- 什么是错误C2039,在这种情况下它意味着什么
- 在这种情况下,>=是什么意思
- 在这种情况下,溢出是什么意思
- 警告C4309 -它是什么意思,在这种情况下可以忽略它
- 在这种情况下,unique_ptr的行为应该是什么?
- cin是在这种情况下使用的正确函数吗