I read this answer by Howard Hinnant (Is std::unique_ptr<T> required to know the full definition of T?) and then this answer (How is a template instantiated?) and I was just thinking. If you have a class like so
class Something {
Something();
~Something();
class Impl;
std::unique_ptr<Impl> impl;
};
The unique_ptr
will be instantiated at that point when the class is compiled (as I could make out from the other answer above). Then why is it okay to not have the class Impl
defined later on? Won't the instantiation require the destructor of Impl
to be present?
Note The following is in an effort to clarify what I am asking above.
The way I am thinking about it, when the compiler goes over the definition of the class Something
. It will see the declaration of the nested class Impl
and then it will see the declaration of the unique_ptr<Impl>
, and at that point. It will instantiate the template unique_ptr
with Impl
. And that instantiated code will contain a call to the destructor of Impl
. Since at this point we have code that includes a call to the destructor of an incomplete class, how is the code above safe?