title: How can I use unique_ptr with a more generic deleter?
body: <p>Consider some function:</p>
  
  <pre><code>template&lt;typename F&gt;
  void foo(F f) {
    std::unique_ptr&lt;int&gt; p = f();
    // do some stuff with p
  }
  </code></pre>
  
  <p>Because <code>unique_ptr</code> decrees a default template argument, <code>default_delete</code>, for <code>D</code>, any function object passed to <code>foo</code> that returns a <code>unique_ptr</code> with a non-default deleter fails to compile.  For example,</p>
  
  <pre><code>int x = 3;
  foo([&amp;x](){
      // use empty deleter
      return std::unique_ptr&lt;int&gt;(&amp;x, [](int*){});
  });
  </code></pre>
  
  <p>However, I could see this being potentially useful, and I don't see a direct reason why it shouldn't be possible.  Is there a common approach for addressing this?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>The easy fix would be to define <code>foo</code> instead to use the following:</p>
  
  <pre><code>  std::unique_ptr&lt;int, std::function&lt;void(int*)&gt;&gt; p = f();
  </code></pre>
  
  <p>But I'm wondering why this couldn't have been incorporated into the interface for <code>unique_ptr</code>?  Is there a reason the class interface couldn't provide this generic attribute?  Are there approaches for "wrapping" this kind of thing into a new definition?</p>
  
  <p>For example,</p>
  
  <pre><code>template&lt;typename T&gt;
  using Generic_unique_ptr =
    std::unique_ptr&lt;
      T,
      std::function&lt; void(typename std::unique_ptr&lt;T&gt;::element_type*) &gt;
    &gt;;
  </code></pre>
  
  <p>But this seems dangerous because it exposes the potential to do something like the follwing,</p>
  
  <pre><code>Generic_unique_ptr&lt;int&gt; p(new int());
  </code></pre>
  
  <p>which would leave the deleter uninitialized and exhibit undefined behavior.  Perhaps some way to provide <strong><em>an instance of</em></strong> <code>std::default_delete&lt;T&gt;</code> as the default deleter?</p>