I have read other stackoverflow questions on the subject, yet I am really confused with the incomplete type and this C++ specification paragraph §5.3.5/5 :
If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined.
Given an example, .h :
template<class T> class my_scoped_ptr
{
private:
T *t;
public:
my_scoped_ptr(T * _t) : t(_t) {}
~my_scoped_ptr() {
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete t;
}
};
class Holder
{
public:
Holder();
~Holder();
private:
class Impl;
my_scoped_ptr<Impl> _mptr;
};
.cpp
class Holder::Impl {};
Holder::Holder() : _mptr(new Impl) {}
Holder::~Holder() {}
How does the non-inline destructor of class Holder suddenly make Impl complete? Why is the default destructor not sufficient to make the class complete? Why shared_ptr works perfectly well without the need of the destructor?