Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::bind a bound function
    primarykey
    data
    text
    <p>I'm having trouble in detecting why the heck is this not compiling. I've got some lambda function that returns a <code>std::function</code> based on some argument. </p> <p>I've narrowed down my problem to this snippet(which doesn't use lambdas, but reproduces my error perfectly):</p> <pre><code>#include &lt;functional&gt; #include &lt;iostream&gt; struct foo { template&lt;class T&gt; void bar(T data) { std::cout &lt;&lt; data &lt;&lt; "\n"; } }; void some_fun(const std::function&lt;void(int)&gt; &amp;f) { f(12); } int main() { foo x; auto f = std::bind(&amp;foo::bar&lt;int&gt;, x, std::placeholders::_1); auto w = std::bind(some_fun, f); w(); } </code></pre> <p>The call to <code>w()</code> produces one of those lovely gcc error outputs in which I can't figure out what's going wrong. This is the error echoed by gcc 4.6.1:</p> <pre><code>g++ -std=c++0x test.cpp -o test test.cpp: In function ‘int main()’: test.cpp:20:7: error: no match for call to ‘(std::_Bind&lt;void (*(std::_Bind&lt;std::_Mem_fn&lt;void (foo::*)(int)&gt;(foo, std::_Placeholder&lt;1&gt;)&gt;))(const std::function&lt;void(int)&gt;&amp;)&gt;) ()’ /usr/include/c++/4.6/functional:1130:11: note: candidates are: /usr/include/c++/4.6/functional:1201:2: note: template&lt;class ... _Args, class _Result&gt; _Result std::_Bind&lt;_Functor(_Bound_args ...)&gt;::operator()(_Args&amp;&amp; ...) [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function&lt;void(int)&gt;&amp;), _Bound_args = {std::_Bind&lt;std::_Mem_fn&lt;void (foo::*)(int)&gt;(foo, std::_Placeholder&lt;1&gt;)&gt;}] /usr/include/c++/4.6/functional:1215:2: note: template&lt;class ... _Args, class _Result&gt; _Result std::_Bind&lt;_Functor(_Bound_args ...)&gt;::operator()(_Args&amp;&amp; ...) const [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function&lt;void(int)&gt;&amp;), _Bound_args = {std::_Bind&lt;std::_Mem_fn&lt;void (foo::*)(int)&gt;(foo, std::_Placeholder&lt;1&gt;)&gt;}] /usr/include/c++/4.6/functional:1229:2: note: template&lt;class ... _Args, class _Result&gt; _Result std::_Bind&lt;_Functor(_Bound_args ...)&gt;::operator()(_Args&amp;&amp; ...) volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function&lt;void(int)&gt;&amp;), _Bound_args = {std::_Bind&lt;std::_Mem_fn&lt;void (foo::*)(int)&gt;(foo, std::_Placeholder&lt;1&gt;)&gt;}] /usr/include/c++/4.6/functional:1243:2: note: template&lt;class ... _Args, class _Result&gt; _Result std::_Bind&lt;_Functor(_Bound_args ...)&gt;::operator()(_Args&amp;&amp; ...) const volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function&lt;void(int)&gt;&amp;), _Bound_args = {std::_Bind&lt;std::_Mem_fn&lt;void (foo::*)(int)&gt;(foo, std::_Placeholder&lt;1&gt;)&gt;}] </code></pre> <p>Here, <code>f</code> should be some callable object which takes an int as argument and calls <code>x.bar(int)</code> using it. On the other hand, <code>w</code> is just a callable object which calls <code>some_fun(f)</code>, being <code>f</code> the callable object mentioned above, which has the signature expected by <code>some_fun</code>'s parameter.</p> <p>Am I missing something? I probably don't know how to actually mix <code>std::bind</code> and <code>std::function</code>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload