2

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?

void.pointer
  • 24,859
  • 31
  • 132
  • 243

1 Answers1

2

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.

Ti Strga
  • 1,353
  • 18
  • 40
  • My question has a lot to do with my thought process. I was trying to make sense out of it in terms of actual source files instead of translation units (as you are explaining it). If another translation unit includes the same Foo.h file (containing the code above) it will have no need for the scoped_ptr's destructor, since nothing in the header file will use it. So you helped me make sense out of this by thinking of it in terms of translation units instead :) – void.pointer Nov 26 '12 at 20:39