title: Unit testing, mocking and unique_ptr
body: <p>There's a class under test which currently accepts a <code>unique_ptr&lt; Interface &gt;&amp;&amp;</code> in it's constructor,  to express that it wants to take single ownership of an Interface implementation. Problems arise when wanting to test this class using a mocked <code>Interface</code> though: the mocking framework (HippoMocks) gives me only <code>Interface*</code> which I do not own, hence cannot delete.</p>
  
  <p>I had the same problem before when testing classes taking <code>const shared_ptr&lt; Interface &gt;&amp;</code> as arguments, but fixed that by providing a custom no-op deleter:</p>
  
  <pre><code>template&lt; class T &gt;
  void NoDelete( T* )
  {
  }
  
    //create a shared_ptr without effective deleter
  template&lt; class T &gt;
  std::shared_ptr&lt; T &gt; mock_shared( T* t )
  {
    return std::shared_ptr&lt; T &gt;( t, NoDelete&lt; T &gt; );
  }
  
  Interface* iface = mocks.GetMeAMock&lt; Interface &gt;();
  DoStuffWithSharedPtrOfInterface( mock_shared&lt; Interface &gt;( iface ) );
  </code></pre>
  
  <p>A similar fix for unique_ptr doesn't really work out because the deleter is a template argument:</p>
  
  <pre><code>template&lt; class T &gt;
  struct NoDelete
  {
    void operator ()( T* )
    {
    }
  };
  
    //oops this is totally useless since std::unique_ptr&lt; T, NoDelete&lt; T &gt; &gt;
    //is not quite the same type as std::unique_ptr&lt; T &gt;
  template&lt; class T &gt;
  std::unique_ptr&lt; T, NoDelete&lt; T &gt; &gt; mock_unique( T* t )
  {
    return std::unique_ptr&lt; T, NoDelete&lt; T &gt; &gt;( t, NoDelete&lt; T &gt;() );
  }
  </code></pre>
  
  <p>Is there a workaround for this? Or should I not be using unique_ptr here in the first place?</p>
  
  <p><strong>update</strong>
  I gave this a go; should work but sizeof( ptr ) is now 8, hard to tell what impact that has.</p>
  
  <pre><code>  //use CustomUniquePtr::type instead of uniqe_ptr
  template&lt; class T &gt;
  struct CustomUniquePtr
  {
    typedef typename std::unique_ptr&lt; T, void (*) ( T* ) &gt; type;
  }
  
    //use everywhere
  template&lt; class T &gt;
  CustomUniquePtr&lt; T &gt;::type make_unique( T* p )
  {
    return CustomUniquePtr&lt; T &gt;::type( p, Delete&lt; T &gt; );
  }
  
    //use when mocking, doe not delete p!
  template&lt; class T &gt;
  CustomUniquePtr&lt; T &gt;::type mock_unique( T* p )
  {
    return CustomUniquePtr&lt; T &gt;::type( p, NoDelete&lt; T &gt; );
  }
  </code></pre>