title: Is this an error in "More Effective C++" in Item28?
body: <p>I encountered a question when I was reading the item28 in <em>More Effective C++</em>.  In this item, the author shows to us that we can use member template in <code>SmartPtr</code> such that the <code>SmartPtr&lt;Cassette&gt;</code> can be converted to <code>SmartPtr&lt;MusicProduct&gt;</code>.<br>
  The following code is not the same as in the book, but has the same effect.  </p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Base {};
  class Derived : public Base {};
  
  template&lt;typename T&gt;
  class smart {
  public:
      smart(T* ptr)
          : ptr(ptr)
      {}
  
      template&lt;typename U&gt;
      operator smart&lt;U&gt;()
      {
          return smart&lt;U&gt;(ptr);
      }
  
      ~smart()
      {
          delete ptr;
      }
  private:
      T* ptr;
  };
  
  void test(const smart&lt;Base&gt;&amp; ) {}
  
  int main()
  {
      smart&lt;Derived&gt; sd(new Derived);
      test(sd);
      return 0;
  }
  </code></pre>
  
  <p>It indeed can be compiled without compilation error. But when I ran the executable file, I got a core dump. I think that's because the member function of the conversion operator makes a temporary smart, which has a pointer to the same ptr in <code>sd</code> (its type is <code>smart&lt;Derived&gt;</code>). So the delete directive operates twice. What's more, after calling test, we can never use <code>sd</code> any more, since <code>ptr</code> in <code>sd</code> has already been delete.<br>
  Now my questions are :  </p>
  
  <ul>
  <li>Is my thought right? Or my code is not the same as the original code in the book?</li>
  <li>If my thought is right, is there any method to do this?</li>
  </ul>
  
  <p>Thanks very much for your help.</p>