在这种情况下,unique_ptr的行为应该是什么?

What should the behavior of unique_ptr be in this situation?

本文关键字:是什么 这种情况下 unique ptr      更新时间:2023-10-16

假设我有以下内容:

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()