title: Suicide object implementation leveraging `std::weak_ptr`
body: <p>I'm considering using "suicide objects" to model entities in a game, that is, objects able to delete themselves. Now, the usual C++03 implementation (plain old <code>delete this</code>) does nothing for other objects potentially refering to the suicide object, which is why I'm using <code>std::shared_ptr</code> and <code>std::weak_ptr</code>.</p>
  
  <p>Now for the code dump :</p>
  
  <pre><code>#include &lt;memory&gt;
  #include &lt;iostream&gt;
  #include &lt;cassert&gt;
  
  struct SuObj {
      SuObj() { std::cout &lt;&lt; __func__ &lt;&lt; '\n'; }
      ~SuObj() { std::cout &lt;&lt; __func__ &lt;&lt; '\n'; }
  
      void die() {
          ptr.reset();
      }
  
      static std::weak_ptr&lt;SuObj&gt; create() {
          std::shared_ptr&lt;SuObj&gt; obj = std::make_shared&lt;SuObj&gt;();
          return (obj-&gt;ptr = std::move(obj));
      }
  
  private:
  
      std::shared_ptr&lt;SuObj&gt; ptr;
  };
  
  int main() {
      std::weak_ptr&lt;SuObj&gt; obj = SuObj::create();
  
      assert(!obj.expired());
      std::cout &lt;&lt; "Still alive\n";
  
      obj.lock()-&gt;die();
  
      assert(obj.expired());
      std::cout &lt;&lt; "Deleted\n";
  
      return 0;
  }
  </code></pre>
  
  <h1>Question</h1>
  
  <p>This code appears to work fine. However, I'd like to have someone else's eye to gauge it. Does this code make sense ? Did I blindly sail into undefined lands ? Should I drop my keyboard and begin art studies right now ?</p>
  
  <p>I hope this question is sufficiently narrowed down for SO. Seemed a bit tiny and low-level for CR.</p>
  
  <h2>Minor precision</h2>
  
  <p>I do not intend to use this in multithreaded code. If the need ever arises, I'll be sure to reconsider the whole thing.</p>