我知道这一点:相抵触的未定义行为auto_ptr的转发声明
#include <memory>
class A;
class B
{
public:
B(A* a) : a_(a) {}
private:
std::auto_ptr<A> a_;
};
运行,除非你有一个彻头彻尾的B::~B()
线定义的;
在一个点,用gcc这样说:
blah/auto_ptr.h: In destructor 'std::auto_ptr<_Tp>::~auto_ptr() [with _Tp = B]': test.hh:6: instantiated from here
blah/auto_ptr.h:173: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
,我们可以检测和修复代码什么不好的事情发生之前。有时这停止发生。是否有任何编译器选项将其打开(-Wall -Wextra -Wpedantic似乎不会将其切换)
注意:由于各种原因,迁移到C++ 11和unique_ptr
不是一个选项,正如我读到的,unique_ptr存在同样的问题。
'std :: auto_ptr'的设计基本上是有缺陷的。即使你不能移动到C++ 11或更新的版本,你应该考虑抛弃'auto_ptr'来支持替代解决方案。它很有缺陷,这种类型将从C++ 17中的标准库中完全删除。 –