unique_ptr实现中可能存在的错误

Possible bug in unique_ptr implementation

本文关键字:存在 错误 ptr 实现 unique      更新时间:2023-10-16

我试图使用带有前向声明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;
}

只有在取消注释 ctor 声明或类 B 声明后,我才能使此代码编译。否则在VS2013上出现错误

error C2338: can't delete an incomplete type

关于 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,
                      ^

这是为什么呢?

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