我知道這一點:相抵觸的未定義行爲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中的標準庫中完全刪除。 –