Can't use std::unique_ptr with T being a forward declaration

前端 未结 2 933
梦毁少年i
梦毁少年i 2021-01-07 18:46

Now first, I am aware of the general issues with unique_ptr<> and forward declarations as in Forward declaration with unique_ptr? .

Consider these three files:

2条回答
  •  有刺的猬
    2021-01-07 19:22

    The implicitly defined special member functions are inline, leading to issues with incomplete types. As the link from Chris's answer shows, all non-delegating constructors could potentially invoke the destructor. This includes copy(deleted in this case) and move constructors as well. So, when you are dealing with non-static members involving incomplete types, explicitly default the definitions in the source file, thus ensuring that they aren't defined inline.

    In header:

    A();
    ~A();
    A(const A&);
    A(A&&);
    

    In source:

    A::A() = default;
    A::~A() = default;
    A::A(const A&) = default;
    A::A(A&&) = default;
    

提交回复
热议问题