title: construct two shared_ptr objects from the same pointer
body: <p>I have a problem from "The C++ Standard Library Extensions":</p>
  
  <blockquote>
    <p>Exercise 6<br>
    I said in Section 2.4.2
    that you shouldn't construct two
    shared_ptr objects from the same
    pointer. The danger is that both
    shared_ptr objects or their progeny
    will eventually try to delete the
    resource, and that usually leads to
    trouble. In fact, you can do this if
    you're careful. It's not particularly
    useful, but write a program that
    constructs two
    shared_ptr objects from
    the same pointer and deletes the
    resource only once.</p>
  </blockquote>
  
  <p>below is my answer:</p>
  
  <pre><code>template &lt;typename T&gt;
  void nonsence(T*){}
  struct SX {
       int data;
       SX(int i = 0) :
                data(i) {
                cout &lt;&lt; "SX" &lt;&lt; endl;
       }
       ~SX() {
                cout &lt;&lt; "~SX" &lt;&lt; endl;
       }
  };
  int main(int argc, char **argv) {
      SX* psx=new SX;
      shared_ptr&lt;SX&gt; sp1(psx),sp2(psx,nonsence&lt;SX&gt;);
      cout&lt;&lt;sp1.use_count()&lt;&lt;endl;
      return 0;
  }
  </code></pre>
  
  <p>but I don't think it is a good solution--because i don't want solving it by use constructor. can anyone give me a better one?
  thx, forgive my bad english.</p>