私はこれを知っている:あなたはB::~B()
のラインの定義のうちを持っていない限り、未定義の動作の抵触auto_ptrを前方宣言
#include <memory>
class A;
class B
{
public:
B(A* a) : a_(a) {}
private:
std::auto_ptr<A> a_;
};
ランを。
そして、ある時点で、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はそれをカットしていないようです)
A注:C++ 11への移行は、さまざまな理由からオプションではありません。私はそれを読んでも、同じ問題がunique_ptrに存在します。クラスBのために生成されたデストラクタが正しくPメンバーを破壊した結果
struct A;
struct B {
std::unique_ptr<A> p;
};
struct A {
~A() {
}
};
{
B b;
b.p = std::unique_ptr<A>(new A()); // here is you bind default_deletor of already completed type
}
:あなたがunique_ptrをオブジェクトを構築するときに削除者をバインドするため
'std :: auto_ptr'のデザインに根本的な欠陥があります。 C++ 11以降に移行することができない場合でも、代替ソリューションを優先して 'auto_ptr'を削除することを検討する必要があります。 C++の標準ライブラリから型が完全に削除されるというのは、あまりにも欠陥があります。 –