A class holds a std::unique_ptr<Impl> member and defines its destructor only in the header as = default. Why does this fail to compile?
Correct answer
A. The defaulted destructor needs Impl complete to invoke the deleter on it
21 views
A. The defaulted destructor needs Impl complete to invoke the deleter on it
21 views
std::unique_ptr may be declared with an incomplete type, but its destructor instantiates default_delete, which needs the complete type in order to call delete. That is why the pimpl idiom declares ~Widget(); in the header and defines it in the source file after Impl is complete. std::shared_ptr avoids the problem because the deleter is captured and type-erased at construction.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.