Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2002-01-24 14:38:42


Peter Dimov's revision to the Boost smart_ptr class templates is now in
Boost CVS. What follows is a summary of differences between the old
smart_ptr and the new smart_ptr. For brevity, I usually mention only
shared_ptr or scoped_ptr, but the features of shared_array and scoped_array
are almost identical.

================

Features:

The four smart_ptr class templates are now available in separate headers
<boost/scoped_ptr.hpp>, <boost/scoped_array.hpp>, <boost/shared_ptr.hpp>,
<boost/shared_array.hpp>.

The new shared_ptr relaxes the requirement that the pointed-to object's
destructor must be visible when instantiating the shared_ptr destructor. Now
the pointed-to object's destructor need only be visible when the shared_ptr
is constructed. This makes it easier to have shared_ptr members in classes
without explicit destructors.

The new shared_ptr can be constructed with a custom deallocator. This
often-requested feature allows things like a shared_ptr<std::FILE>.

The new shared_ptr will always delete the object using the pointer it was
originally constructed with. This prevents subtle problems that could happen
with the old shared_ptr if the last shared_ptr was a pointer to a sub-object
of a class that did not have a virtual destructor.

A new weak_ptr class template is available in <boost/weak_ptr.hpp>, which
can be constructed from a shared_ptr and has a similar interface. The value
of the weak_ptr becomes 0 when the last shared_ptr to that object is
destroyed or reset.

shared_static_cast and shared_dynamic_cast function templates are provided
which work for shared_ptr and weak_ptr as static_cast and dynamic_cast do
for pointers.

Assertions have been added to check preconditions of various functions;
since these use the new <boost/assert.hpp> header, the assertions are
disabled by default.

The self-assignment misfeature has been removed from shared_ptr::reset,
although it is still present in scoped_ptr. Calling reset with a pointer to
the object that's already owned by the shared_ptr results in undefined
behavior (an assertion, or eventually a double-delete if assertions are
off).

The BOOST_SMART_PTR_CONVERSION feature has been removed.

The new class templates fix several anomalies with operator=() and reset()
by using a simpler implementation. This fixes a number of reported bugs with
the old shared_ptr.

shared_ptr<void> is now allowed.

The partial specialization of std::less has been replaced by operator<
overloads which accomplish the same thing without relying on undefined
behavior.

The incorrect overload of std::swap has been replaced by a boost::swap,
which has many of the same advantages but does not violate the C++ standard.

shared_ptr can now be used more-flexibly with boost::mem_fn and hence
boost::bind because of the addition of a get_pointer function overload.

Dependencies on <boost/utility.hpp> and <boost/static_assert.hpp> have been
removed to reduce the coupling between Boost libraries.

The smart pointer classes are now thread-safe on many platforms, using the
new <boost/detail/atomic_count.hpp>, without significant performance
degradation.

shared_ptr, shared_array, and weak_ptr share most of their implementation,
with details found in <boost/detail/shared_count.hpp>.

================

Caveats:

As a transitional measure, <boost/smart_ptr.hpp> still gives the old
version. Since the old and new version use the same name, you can't mix old
with new. Note that this means you can't mix the new smart pointers with the
Boost.Python or Boost.Regex library until this is changed. We'll have to
resolve this one way or another before the next release.

The asserts in the new smart pointer class templates use a new
<boost/assert.hpp> BOOST_ASSERT macro, which defaults off. Other asserts in
Boost use the standard C <assert.h> assert macro, which defaults on. Still
other places in Boost use the word "assert" to refer to code that throws
exceptions. More refinement in this area is probably needed, including
perhaps formal review of <boost/assert.hpp>.

The total memory required by each shared_ptr is slightly higher than for the
old shared_ptr. This should not have a significant impact on most programs.

The documentation and test programs have not yet been updated for the new
smart_ptr. We probably need to deal with this before the next release.

For compilers that don't support member templates well enough, a separate
implementation is used that lacks many of the new features and is more like
the old version.

The self-assignment misfeature has been removed from shared_ptr, although it
is still present in scoped_ptr (see above).

The BOOST_SMART_PTR_CONVERSION feature has been removed.

================

Further suggestions or comments about the smart pointer class templates can
go either to the Boost mailing list or to Peter <pdimov_at_[hidden]> and me
<darin_at_[hidden]>.

    -- Darin


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