Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The issue is that <code>std::bind</code> treats "bind expression" (like your <code>applyWithFoo0</code>) differently from other types. Instead of calling foo1 with <code>applyWithFoo0</code> as parameter it tries to invoke <code>applyWithFoo0</code> and pass its return value to foo1. But <code>applyWithFoo0</code> doesn't return anything that is convertible to <code>std::function&lt;void(int)&gt;</code>. The intention of handling "bind expressions" like this is to make them easily composable. In most cases you probably don't want bind expression to be passed as function parameters but only their results. If you explicitly wrap the bind expression into a <code>function&lt;&gt;</code> object, the <code>function&lt;&gt;</code> object will simply be passed to foo1 directly since it is <em>not</em> a "bind expression" and therefore not handled specially by <code>std::bind</code>.</p> <p>Consider the following example:</p> <pre><code>#include &lt;iostream&gt; #include &lt;functional&gt; int twice(int x) { return x*2; } int main() { using namespace std; using namespace std::placeholders; auto mul_by_2 = bind(twice,_1); auto mul_by_4 = bind(twice,mul_by_2); // #2 auto mul_by_8 = bind(twice,mul_by_4); // #3 cout &lt;&lt; mul_by_8(1) &lt;&lt; endl; } </code></pre> <p>This actually compiles and works because instead of passing a functor to twice like you might expect from the bind expressions #2 and #3, bind actually evaluates the passed bind expressions and <strong>uses its result</strong> as function parameter for twice. Here, it is intentional. But in your case, you tripped over this behaviour by accident because you actually want bind to pass the functor itself to the function instead of its evaluated value. Wrapping the functor into a function&lt;> object is obviously a work-around for that.</p> <p>In my opinion this design decision is a bit awkward because it introduces an irregularity people have to know about to be able to use bind correctly. Maybe, we'll get another more satisfying work around in the future like</p> <pre><code>auto applyFoo1 = bind( foo1, _1, noeval(applyWithFoo0) ); </code></pre> <p>where <code>noeval</code> tells bind not to evaluate the expression but to pass it directoy to the function. But maybe the other way around -- explicitly telling bind to pass the result of a functor to the function -- would have been a better design:</p> <pre><code>auto mul_by_8 = bind( twice, eval(mul_by_4) ); </code></pre> <p>But I guess, now it's too late for that ...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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