Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Shared ownership:</strong><br> The <code>shared_ptr</code> and <code>weak_ptr</code> the standard adopted are pretty much the same as their <a href="http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm" rel="noreferrer">Boost counterparts</a>. Use them when you need to share a resource and don't know which one will be the last to be alive. Use <code>weak_ptr</code> to observe the shared resource without influencing its lifetime, not to break cycles. Cycles with <code>shared_ptr</code> shouldn't normally happen - two resources can't own each other.</p> <p>Note that Boost additionally offers <a href="http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_array.htm" rel="noreferrer"><code>shared_array</code></a>, which might be a suitable alternative to <code>shared_ptr&lt;std::vector&lt;T&gt; const&gt;</code>.</p> <p>Next, Boost offers <a href="http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/intrusive_ptr.html" rel="noreferrer"><code>intrusive_ptr</code></a>, which are a lightweight solution if your resource offers reference-counted management already and you want to adopt it to the RAII principle. This one was not adopted by the standard.</p> <p><strong>Unique ownership:</strong><br> Boost also has a <a href="http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/scoped_ptr.htm" rel="noreferrer"><code>scoped_ptr</code></a>, which is not copyable and for which you can not specify a deleter. <code>std::unique_ptr</code> is <code>boost::scoped_ptr</code> on steroids and should be your <strong>default choice when you need a smart pointer</strong>. It allows you to specify a deleter in its template arguments and is <em>movable</em>, unlike <code>boost::scoped_ptr</code>. It is also fully usable in STL containers as long as you don't use operations that need copyable types (obviously).</p> <p>Note again, that Boost has an array version: <a href="http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/scoped_array.htm" rel="noreferrer"><code>scoped_array</code></a>, which the standard unified by requiring <code>std::unique_ptr&lt;T[]&gt;</code> partial specialization that will <code>delete[]</code> the pointer instead of <code>delete</code>ing it (with the <code>default_delete</code>r). <code>std::unique_ptr&lt;T[]&gt;</code> also offers <code>operator[]</code> instead of <code>operator*</code> and <code>operator-&gt;</code>.</p> <p>Note that <code>std::auto_ptr</code> is still in the standard, but it is <em>deprecated</em>. <code>§D.10 [depr.auto.ptr]</code></p> <blockquote> <p>The class template <code>auto_ptr</code> is deprecated. [ <em>Note:</em> The class template <code>unique_ptr</code> (20.7.1) provides a better solution. <em>—end note</em> ]</p> </blockquote> <p><strong>No ownership:</strong><br> Use dumb pointers (raw pointers) or references for <strong>non-owning references</strong> to resources and when you know that the <strong>resource will outlive</strong> the referencing object / scope. Prefer references and use raw pointers when you need either nullability or resettability.</p> <p>If you want a non-owning reference to a resource, but you don't know if the resource will outlive the object that references it, pack the resource in a <code>shared_ptr</code> and use a <code>weak_ptr</code> - you can test if the parent <code>shared_ptr</code> is alive with <code>lock</code>, which will return a <code>shared_ptr</code> that is non-null if the resource still exists. If want to test whether the resource is dead, use <code>expired</code>. The two may sound similar, but are very different in the face of concurrent execution, as <code>expired</code> only guarantees its return value for that single statement. A seemingly innocent test like</p> <pre><code>if(!wptr.expired()) something_assuming_the_resource_is_still_alive(); </code></pre> <p>is a potential race condition.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload