title: How to test whether a shared_ptr is empty or owns nothing
body: <p>A C++ <code>std::shared_ptr&lt;..&gt;</code> may be <strong>empty</strong> and it may also be <strong>null</strong>. Both of these concepts exist and they are <em>not</em> equivalent. Additionally, neither implication is always true between these cases.</p>
  
  <p>The latter case is trivial to detect because <code>operator bool</code> provides precisely that test. According to the docs, it "checks if <code>*this</code> stores a non-null pointer, i.e. whether <code>get() != nullptr</code>."</p>
  
  <p><strong>Is there a test for the former case, the case where the thing is <em>empty</em>?</strong></p>
  
  <p>My use for this is quite simple. I have a class that has a static factory method. Inside the static factory method is a static local <code>shared_ptr</code> to an instance of the class, initialised to <code>nullptr</code>. The first call to this factory method constructs an instance of the class and initialises the static local <code>shared_ptr</code> before returning a copy of it - this is guarded by a <code>mutex</code>. That <code>shared_ptr</code> may be held by anything, copied and passed about, further copies may be acquired by additional calls to the static factory and, finally, when all the copies are destructed, the <code>shared_ptr</code>'s deleter destructs the instance.</p>
  
  <p>The instance itself is created and destructed with a legacy C API, wrapped by my class, and, although these instances are intended to be shared as singletons, they also need to be cleaned up when they are no longer needed - unlike singletons.</p>
  
  <p>At the moment, I am using a null-check to decide whether the static local <code>shared_ptr</code> should be initialised or merely copied. I fear that this test will not work to detect the case where re-initialisation is required - for example, if something tries to acquire an instance at some time after all previous users gave up their references and the shared instance was deleted.</p>
  
  <p>Or is it true that <code>shared_ptr</code> is reset to <code>nullptr</code> when the reference count falls to zero and the deleter is invoked? Also for custom deleters?</p>
  
  <p>Also relevant: <a href="https://stackoverflow.com/questions/25920681/what-is-the-difference-between-an-empty-and-a-null-stdshared-ptr-in-c">What is the difference between an empty and a null std::shared_ptr in C++?</a></p>