title: Passing a non-copyable closure object to std::function parameter
body: <p>In C++14, a lambda expression can capture variables by moving from them using capture initializers. However, this makes the resulting closure object non-copyable. If I have an existing function that takes a <code>std::function</code> argument (that I cannot change), I cannot pass the closure object, because <code>std::function</code>'s constructor requires the given functor to be <code>CopyConstructible</code>.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  void doit(std::function&lt;void()&gt; f) {
      f();
  }
  
  int main()
  {
      std::unique_ptr&lt;int&gt; p(new int(5));
      doit([p = std::move(p)] () { std::cout &lt;&lt; *p &lt;&lt; std::endl; });
  }
  </code></pre>
  
  <p>This gives the following errors:</p>
  
  <pre><code>/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:1911:10: error: 
        call to implicitly-deleted copy constructor of '&lt;lambda at test.cpp:10:7&gt;'
              new _Functor(*__source._M_access&lt;_Functor*&gt;());
                  ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:1946:8: note: in
        instantiation of member function 'std::_Function_base::_Base_manager&lt;&lt;lambda at test.cpp:10:7&gt;
        &gt;::_M_clone' requested here
                _M_clone(__dest, __source, _Local_storage());
                ^
  /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:2457:33: note: in
        instantiation of member function 'std::_Function_base::_Base_manager&lt;&lt;lambda at test.cpp:10:7&gt;
        &gt;::_M_manager' requested here
              _M_manager = &amp;_My_handler::_M_manager;
                                         ^
  test.cpp:10:7: note: in instantiation of function template specialization 'std::function&lt;void
        ()&gt;::function&lt;&lt;lambda at test.cpp:10:7&gt;, void&gt;' requested here
          doit([p = std::move(p)] () { std::cout &lt;&lt; *p &lt;&lt; std::endl; });
               ^
  test.cpp:10:8: note: copy constructor of '' is implicitly deleted because field '' has a deleted
        copy constructor
          doit([p = std::move(p)] () { std::cout &lt;&lt; *p &lt;&lt; std::endl; });
                ^
  /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/unique_ptr.h:273:7: note: 
        'unique_ptr' has been explicitly marked deleted here
        unique_ptr(const unique_ptr&amp;) = delete;
        ^
  </code></pre>
  
  <p>Is there a reasonable workaround?</p>
  
  <p>Testing with Ubuntu clang version 3.5-1~exp1 (trunk)</p>