The unique_ptr
destructor needs to know the full definition of AImpl
, because it deletes it. So the question is, where does the unique_ptr
destructor sit? It's a template, so the question is about the instantiation point.
The destructor is instantiated when it is first used. Both the constructor and the destructor of the containing class use it (the constructor needs it if its body throws an exception). So the unique_ptr
destructor is instantiated where A
's constructor or destructor are placed, whichever comes first.
If you default those special members, they are generated immediately after the class body, i.e. in the header, where the size of AImpl
is not known.
If you instead declare them in the class and then put definitions (you may =default
those definitions) in the .cpp
, after the full definition of AImpl
, then the unique_ptr
destructor is instantiated there.