问题
In order to use an incomplete type with a smart pointer such as boost::scoped_ptr
, one must explicitly define an empty destructor for the parent class in the corresponding CPP file. Example:
// H file
class Foo
{
public:
~Foo();
private:
class Pimpl;
boost::scoped_ptr<Pimpl> pimpl_;
};
// CPP file
class Foo::Pimpl {};
Foo::~Foo() {}
Where exactly does the compiler place the instantiation of boost::scoped_ptr
's destructor? I'm trying to visually imagine where it would be in either of these source files, as if I had explicitly defined scoped_ptr's destructor myself. Is it reasonable to imagine it this way?
I know that template methods and classes aren't instantiated for a type until it is used with that type, but I'm trying to think about it structurally now, and where the compiler would place it if it actually hand-wrote it. This will help me get a better understanding of how such techniques (like the one above) are able to be used.
Also I'm not sure if the entire definition of a template class is instantiated or only the relevant parts of it, as they are used. In other words, is it possible for only part of boost::scoped_ptr
's entire definition to exist?
回答1:
I feel like I'm missing something from your question.
There's only one body (".cpp") file being used in this example, so it goes there. More precisely, it goes into the same object file (".o", ".obj", etc) as that destructor. The language standard doesn't specify this, but that's how all the implementations I'm familiar with do it.
来源:https://stackoverflow.com/questions/13571986/where-are-template-methods-instantiated