The February 2002 change to the Boost smart pointers introduced a number of changes. Since the previous version of the smart pointers was in use for a long time, it's useful to have a detailed list of what changed from a library user's point of view.
Note that 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.
Features Requiring Code Changes to Take Advantage
- The smart pointer class templates now each have their own header file. For compatibility, the <boost/smart_ptr.hpp> header now includes the headers for the four classic smart pointer class templates.
- The
weak_ptr
template was added. - The new
shared_ptr
andshared_array
relax the requirement that the pointed-to object's destructor must be visible when instantiating theshared_ptr
destructor. This makes it easier to haveshared_ptr
members in classes without explicit destructors. - A custom deallocator can be passed in when creating a
shared_ptr
orshared_array
. shared_static_cast
andshared_dynamic_cast
function templates are provided which work forshared_ptr
andweak_ptr
asstatic_cast
anddynamic_cast
do for pointers.- The self-assignment misfeature has been removed from
shared_ptr::reset
, although it is still present inscoped_ptr
, and instd::auto_ptr
. Callingreset
with a pointer to the object that's already owned by theshared_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. shared_ptr<void>
is now allowed.
Features That Improve Robustness
- The manipulation of use counts is now thread safe on Windows, Linux, and platforms that support pthreads. See the <boost/detail/atomic_count.hpp> file for details
- The new
shared_ptr
will always delete the object using the pointer it was originally constructed with. This prevents subtle problems that could happen if the lastshared_ptr
was a pointer to a sub-object of a class that did not have a virtual destructor.
Implementation Details
- Some bugs in the assignment operator implementations and in
reset
have been fixed by using the "copy and swap" idiom. - Assertions have been added to check preconditions of various functions; however, since these use the new <boost/assert.hpp> header, the assertions are disabled by default.
- The partial specialization of
std::less
has been replaced byoperator<
overloads which accomplish the same thing without relying on undefined behavior. - The incorrect overload of
std::swap
has been replaced byboost::swap
, which has many of the same advantages for generic programming but does not violate the C++ standard.
Revised 1 February 2002
Copyright 2002 Darin Adler. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.