title: Binding functions with unique_ptr arguments to std::function<void()>
body: <p>I'm trying to make the following code work:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;functional&gt;
  #include &lt;string&gt;
  #include &lt;memory&gt;
  
  using namespace std;
  
  class Foo {
  public:
      Foo(): m_str("foo") { }
  
      void f1(string s1, string s2, unique_ptr&lt;Foo&gt; p)
          {
              printf("1: %s %s %s\n", s1.c_str(), s2.c_str(), p-&gt;str());
          }
  
      void f2(string s1, string s2, Foo* p)
          {
              printf("2: %s %s %s\n", s1.c_str(), s2.c_str(), p-&gt;str());
          }
  
      const char* str() const { return m_str.c_str(); }
  
  private:
      string m_str;
  };
  
  int main()
  {
      string arg1 = "arg1";
      string arg2 = "arg2";
      Foo s;
      unique_ptr&lt;Foo&gt; ptr(new Foo);
  
  
      //function&lt;void()&gt; f(bind(&amp;Foo::f1, &amp;s, arg1, arg2, std::move(ptr)));
      function&lt;void()&gt; f(bind(&amp;Foo::f2, &amp;s, arg1, arg2, ptr.release()));
  
      f();
  }
  </code></pre>
  
  <p>Calling <code>f()</code> bound to <code>Foo::f2</code> (last parameter is a raw pointer) works fine, but binding it to <code>Foo::f1</code> causes compilation error:</p>
  
  <pre><code>test.cpp: In function ‘int main()’:
  test.cpp:36:70: error: no matching function for call to ‘std::function&lt;void()&gt;::function(std::_Bind_helper&lt;false, void (Foo::*)(std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;), Foo*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::unique_ptr&lt;Foo, std::default_delete&lt;Foo&gt; &gt; &gt;::type)’
       function&lt;void()&gt; f(bind(&amp;Foo::f1, &amp;s, arg1, arg2, std::move(ptr)));
                                                                        ^
  test.cpp:36:70: note: candidates are:
  In file included from test.cpp:2:0:
  /usr/include/c++/4.8.2/functional:2251:2: note: template&lt;class _Functor, class&gt; std::function&lt;_Res(_ArgTypes ...)&gt;::function(_Functor)
    function(_Functor);
    ^
  /usr/include/c++/4.8.2/functional:2251:2: note:   template argument deduction/substitution failed:
  /usr/include/c++/4.8.2/functional:2226:7: note: std::function&lt;_Res(_ArgTypes ...)&gt;::function(std::function&lt;_Res(_ArgTypes ...)&gt;&amp;&amp;) [with _Res = void; _ArgTypes = {}]
         function(function&amp;&amp; __x) : _Function_base()
         ^
  /usr/include/c++/4.8.2/functional:2226:7: note:   no known conversion for argument 1 from ‘std::_Bind_helper&lt;false, void (Foo::*)(std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;), Foo*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::unique_ptr&lt;Foo, std::default_delete&lt;Foo&gt; &gt; &gt;::type {aka std::_Bind&lt;std::_Mem_fn&lt;void (Foo::*)(std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;)&gt;(Foo*, std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;)&gt;}’ to ‘std::function&lt;void()&gt;&amp;&amp;’
  /usr/include/c++/4.8.2/functional:2429:5: note: std::function&lt;_Res(_ArgTypes ...)&gt;::function(const std::function&lt;_Res(_ArgTypes ...)&gt;&amp;) [with _Res = void; _ArgTypes = {}]
       function&lt;_Res(_ArgTypes...)&gt;::
       ^
  /usr/include/c++/4.8.2/functional:2429:5: note:   no known conversion for argument 1 from ‘std::_Bind_helper&lt;false, void (Foo::*)(std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;), Foo*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::unique_ptr&lt;Foo, std::default_delete&lt;Foo&gt; &gt; &gt;::type {aka std::_Bind&lt;std::_Mem_fn&lt;void (Foo::*)(std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;)&gt;(Foo*, std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;)&gt;}’ to ‘const std::function&lt;void()&gt;&amp;’
  /usr/include/c++/4.8.2/functional:2206:7: note: std::function&lt;_Res(_ArgTypes ...)&gt;::function(std::nullptr_t) [with _Res = void; _ArgTypes = {}; std::nullptr_t = std::nullptr_t]
         function(nullptr_t) noexcept
         ^
  /usr/include/c++/4.8.2/functional:2206:7: note:   no known conversion for argument 1 from ‘std::_Bind_helper&lt;false, void (Foo::*)(std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;), Foo*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, std::unique_ptr&lt;Foo, std::default_delete&lt;Foo&gt; &gt; &gt;::type {aka std::_Bind&lt;std::_Mem_fn&lt;void (Foo::*)(std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;)&gt;(Foo*, std::basic_string&lt;char&gt;, std::basic_string&lt;char&gt;, std::unique_ptr&lt;Foo&gt;)&gt;}’ to ‘std::nullptr_t’
  /usr/include/c++/4.8.2/functional:2199:7: note: std::function&lt;_Res(_ArgTypes ...)&gt;::function() [with _Res = void; _ArgTypes = {}]
         function() noexcept
         ^
  /usr/include/c++/4.8.2/functional:2199:7: note:   candidate expects 0 arguments, 1 provided
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>I'm using gcc 4.8.2 and <code>-std=c++0x</code> (<code>-std=c++11</code> fails too) flags.</p>