unique_ptr实现中可能存在的错误

pdo*_*iak 6 c++ unique-ptr c++11

我试图使用带有前向声明的unique_ptr类成员.众多消息来源称例如使用unique_ptr进行前向声明?声明非内联析构函数应该足够了,但在VS2013和GCC 5.3.1中似乎不是这种情况.我没有测试其他编译器.

例:

#include <memory>

class B;

class A { 
public:
    //A();
    ~A();
private:
    std::unique_ptr<B> b;
};

//class B { };

int main() {
    A a;
}
Run Code Online (Sandbox Code Playgroud)

我只能在取消注释ctor声明或类B声明后才能编译此代码.否则在VS2013上我收到错误

error C2338: can't delete an incomplete type
Run Code Online (Sandbox Code Playgroud)

关于GCC错误:

In file included from /usr/local/include/c++/5.3.0/memory:81:0,
                 from main.cpp:1:
/usr/local/include/c++/5.3.0/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = B]':
/usr/local/include/c++/5.3.0/bits/unique_ptr.h:236:17:   required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = B; _Dp = std::default_delete<B>]'
main.cpp:5:7:   required from here
/usr/local/include/c++/5.3.0/bits/unique_ptr.h:74:22: error: invalid application of 'sizeof' to incomplete type 'B'
  static_assert(sizeof(_Tp)>0,
                      ^
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

kni*_*vil 2

类 A 的析构函数必须知道类 B 的定义。只要 A 的构造函数/析构函数的实现文件知道类 B 的定义,类 B 的前向声明就可以。如果您的实现(隐式)在头文件,那么您需要在头文件中定义 B 。你可以向 Herb Sutter学习Pimpl 。