[29] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
| 2 | <html> |
---|
| 3 | <head> |
---|
| 4 | <title>Smart Pointer Changes</title> |
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
| 6 | </head> |
---|
| 7 | <body bgcolor="#ffffff" text="#000000"> |
---|
| 8 | <h1><A href="../../index.htm"><img src="../../boost.png" alt="boost.png (6897 bytes)" align="middle" width="277" height="86" |
---|
| 9 | border="0"></A>Smart Pointer Changes</h1> |
---|
| 10 | <p>The February 2002 change to the Boost smart pointers introduced a number of |
---|
| 11 | changes. Since the previous version of the smart pointers was in use for a long |
---|
| 12 | time, it's useful to have a detailed list of what changed from a library user's |
---|
| 13 | point of view.</p> |
---|
| 14 | <p>Note that for compilers that don't support member templates well enough, a |
---|
| 15 | separate implementation is used that lacks many of the new features and is more |
---|
| 16 | like the old version.</p> |
---|
| 17 | <h2>Features Requiring Code Changes to Take Advantage</h2> |
---|
| 18 | <ul> |
---|
| 19 | <li> |
---|
| 20 | The smart pointer class templates now each have their own header file. For |
---|
| 21 | compatibility, the <a href="../../boost/smart_ptr.hpp"><boost/smart_ptr.hpp></a> |
---|
| 22 | header now includes the headers for the four classic smart pointer class |
---|
| 23 | templates. |
---|
| 24 | <li> |
---|
| 25 | The <b>weak_ptr</b> |
---|
| 26 | template was added. |
---|
| 27 | <li> |
---|
| 28 | The new <b>shared_ptr</b> and <b>shared_array</b> relax the requirement that |
---|
| 29 | the pointed-to object's destructor must be visible when instantiating the <b>shared_ptr</b> |
---|
| 30 | destructor. This makes it easier to have shared_ptr members in classes without |
---|
| 31 | explicit destructors. |
---|
| 32 | <li> |
---|
| 33 | A custom deallocator can be passed in when creating a <b>shared_ptr</b> or <b>shared_array</b>. |
---|
| 34 | <li> |
---|
| 35 | <b>shared_static_cast</b> and <b>shared_dynamic_cast</b> function templates are |
---|
| 36 | provided which work for <b>shared_ptr</b> and <b>weak_ptr</b> as <b>static_cast</b> |
---|
| 37 | and <b>dynamic_cast</b> |
---|
| 38 | do for pointers. |
---|
| 39 | <li> |
---|
| 40 | The self-assignment misfeature has been removed from <b>shared_ptr::reset</b>, |
---|
| 41 | although it is still present in <b>scoped_ptr</b>, and in <b>std::auto_ptr</b>. |
---|
| 42 | Calling <b>reset</b> with a pointer to the object that's already owned by the <b>shared_ptr</b> |
---|
| 43 | results in undefined behavior (an assertion, or eventually a double-delete if |
---|
| 44 | assertions are off). |
---|
| 45 | <li> |
---|
| 46 | The <b>BOOST_SMART_PTR_CONVERSION</b> |
---|
| 47 | feature has been removed. |
---|
| 48 | <li> |
---|
| 49 | <b>shared_ptr<void></b> is now allowed.</li> |
---|
| 50 | </ul> |
---|
| 51 | <h2>Features That Improve Robustness</h2> |
---|
| 52 | <ul> |
---|
| 53 | <li> |
---|
| 54 | The manipulation of use counts is now <a name="threadsafe">thread safe</a> on |
---|
| 55 | Windows, Linux, and platforms that support pthreads. See the <a href="../../boost/detail/atomic_count.hpp"> |
---|
| 56 | <boost/detail/atomic_count.hpp></a> |
---|
| 57 | file for details |
---|
| 58 | <li> |
---|
| 59 | The new shared_ptr will always delete the object using the pointer it was |
---|
| 60 | originally constructed with. This prevents subtle problems that could happen if |
---|
| 61 | the last <b>shared_ptr</b> was a pointer to a sub-object of a class that did |
---|
| 62 | not have a virtual destructor.</li> |
---|
| 63 | </ul> |
---|
| 64 | <h2>Implementation Details</h2> |
---|
| 65 | <ul> |
---|
| 66 | <li> |
---|
| 67 | Some bugs in the assignment operator implementations and in <b>reset</b> |
---|
| 68 | have been fixed by using the "copy and swap" idiom. |
---|
| 69 | <li> |
---|
| 70 | Assertions have been added to check preconditions of various functions; |
---|
| 71 | however, since these use the new <a href="../../boost/assert.hpp"><boost/assert.hpp></a> |
---|
| 72 | header, the assertions are disabled by default. |
---|
| 73 | <li> |
---|
| 74 | The partial specialization of <b>std::less</b> has been replaced by <b>operator<</b> |
---|
| 75 | overloads which accomplish the same thing without relying on undefined |
---|
| 76 | behavior. |
---|
| 77 | <li> |
---|
| 78 | The incorrect overload of <b>std::swap</b> has been replaced by <b>boost::swap</b>, |
---|
| 79 | which has many of the same advantages for generic programming but does not |
---|
| 80 | violate the C++ standard.</li> |
---|
| 81 | </ul> |
---|
| 82 | <hr> |
---|
| 83 | <p>Revised 1 February 2002</p> |
---|
| 84 | <p>Copyright 2002 Darin Adler. Permission to copy, use, modify, sell and distribute |
---|
| 85 | this document is granted provided this copyright notice appears in all copies. |
---|
| 86 | This document is provided "as is" without express or implied warranty, and with |
---|
| 87 | no claim as to its suitability for any purpose.</p> |
---|
| 88 | </body> |
---|
| 89 | </html> |
---|