Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2002-05-09 12:16:02


On Thursday, May 9, 2002, at 03:55 AM, Peter Dimov wrote:

> From: "Gennadiy Rozental" <rogeeff_at_[hidden]>
>> Why don't you name you semantic: ptr_with_custom_deleter?
>
> This actually deserves a response. It seems that custom deleters are often
> misunderstood or overemphasized. They are _not_ a central point of
> shared_ptr's interface; there are other reasons for keeping a deleter along
> with the pointer. Custom deleter support falls out for free from the
> implementation, which is why we added the appropriate constructor.

To help dispel a bit of the mystery about those "other reasons":

One of the reasons for keeping a deleter along with the pointer is to allow passing around shared_ptr objects even when the complete type of the pointed-to object is not visible. One of the best features of the modern versions of shared_ptr is that the destructor must only be visible at the time an object is put into the shared_ptr. This makes things a lot better for objects with shared_ptr members than with many other smart pointer implementations. Among other things, you don't have to worry about where the destructor is instantiated, so you can often just let the compiler generate the destructor.

With older versions of shared_ptr, you had to ensure that the class definition was visible at the time the destructor was instantiated. This led to subtle problems where the compiler generated destructor would do something different than an empty constructor in a .cpp file; it was the main thing that inspired us to invent checked_delete. So if you had this:

b.h:

          #include <boost/shared_ptr.hpp>

        class A;

        class B {
          public:
              B();
          private:
              shared_ptr<A> p;
          };

You would have had a bug with the older shared_ptr. With the oldest versions, the object pointed to by p would just end up being deleted incorrectly! With later versions, you'd get a compile time error due to the checked delete idiom. With the latest shared_ptr, it just works.

     -- Darin


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk