title: Minimal C++98 unique_ptr implementation with move copy
body: <p>I am trying to implement a minimal unique_ptr in c++98(gcc 4.4.7)(Don't have C++11 enabled compiler).</p>
  
  <p>My effort till now is as following:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;typeinfo&gt;
  #include &lt;cassert&gt;
  
  struct default_deleter
  {
      void operator () (void *p) { free(p); }
      operator bool() { return true;}
  };
  
  struct do_not_delete
  {
      void operator () (void const *){}
      operator bool() { return true;}
  };
  
  template &lt;typename T,typename D = default_deleter&gt;
  class unique_ptr
  {
  public:
      typedef T element_type;
      typedef D deleter_type;
      typedef T * pointer;
  
      explicit unique_ptr(pointer p = 0)
          : ptr_(p)
          , deleter_(default_deleter())
      {
      }
  
      unique_ptr(pointer p,deleter_type d)
          : ptr_(p)
          , deleter_(d)
      {
      }
  
      ~unique_ptr()
      {
          reset();
      }
  
      pointer get() const
      {
          return ptr_;
      }
  
      pointer release()
      {
          pointer tmp = ptr_;
          ptr_ = 0;
          return tmp;
      }
  
      void reset(pointer p = 0)
      {
          if (ptr_ != p){
              if (ptr_ )
                  deleter_(ptr_);
              ptr_ = p;
          }
      }
  
      void swap(unique_ptr&amp; u)
      {
          std::swap(ptr_,u.ptr_);   
      }
  
      operator bool() const
      {
          return (ptr_ != 0);
      }
  #if 0
      unique_ptr&amp; operator=(unique_ptr&lt;pointer,deleter_type&gt; &amp;&amp; other)
      {
          this-&gt;reset(other.release());
          deleter() = other.deleter();
          return *this;
      }
  #endif
     private:
      unique_ptr(const unique_ptr&amp;);
      unique_ptr&amp; operator=(const unique_ptr&amp;);
  
      pointer ptr_;
      deleter_type deleter_;
  };
  
  void testf(unique_ptr&lt;int const,do_not_delete&gt; ptr)
  {
      //
  }
  
  
  int main()
  {
      {
           unique_ptr&lt;int&gt; a((int *)malloc(sizeof(int)));
           unique_ptr&lt;int,do_not_delete&gt; c(a.get(),do_not_delete());
           unique_ptr&lt;int&gt; p(a.release());
      }
      {
          int const s = 10;
          testf(unique_ptr&lt;int const,do_not_delete&gt;(&amp;s,do_not_delete()));
      }
      //unique_ptr&lt;int,void(*)(void *)&gt; a((int *)malloc(sizeof(int)),0);
  }
  </code></pre>
  
  <p>But I am stuck, when I need to pass a unique_ptr to a function or return from a function.
  What all I can do fix this problem?</p>
  
  <p>I was encouraged to ask this question because of <a href="http://www.boost.org/doc/libs/1_41_0/doc/html/boost/interprocess/unique_ptr.html" rel="nofollow">unique_ptr reference</a>  for <a href="http://www.boost.org/users/history/version_1_41_0.html" rel="nofollow">boost 1.41.0 release </a>.</p>