Pimpl with unique_ptr:为什么我必须将接口构造函数的定义移动到".cpp"?

jav*_*ver 6 c++ pimpl-idiom unique-ptr

只要我不将构造函数(of B)的定义移动到头部,代码就会工作B.h.

BH

class Imp;  //<--- error here
class B{
    public:
    std::unique_ptr<Imp> imp;
    B();     //<--- move definition to here will compile error
    ~B();
    //// .... other functions ....
};
Run Code Online (Sandbox Code Playgroud)

B.cpp

#include "B.h"
#include "Imp.h"
B::B(){ }
~B::B(){ }
Run Code Online (Sandbox Code Playgroud)

Imp.h

class Imp{};
Run Code Online (Sandbox Code Playgroud)

Main.cpp (编译我)

#include "B.h"
Run Code Online (Sandbox Code Playgroud)

错误:删除指向不完整类型的指针
错误:使用未定义类型'Imp'C2027

我可以以某种方式理解析构函数必须被移动到.cpp,因为Imp可能会被称为析构: -

delete pointer-of-Imp;  //something like this
Run Code Online (Sandbox Code Playgroud)

但是,我不明白为什么规则也涵盖了构造函数(问题).

我读过了 :-

Pot*_*ter 8

在异常退出的情况下,构造函数需要销毁类成员.

我不认为制作构造函数noexcept会有所帮助,尽管可能应该这样做.