Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that the compiler can't figure out what to use for B. In order to determine that type it wants to use the function&lt;> you pass in for <code>f</code>, but you don't pass an std::function&lt;> directly. You pass in something you expect to be used to construct a function&lt;>. And in order to do that implicit construction it needs to know the type of argument. So you've got this circular dependency where the type of argument depends on what you pass in, but what gets passed in depends on the type of argument.</p> <p>You can break this circular dependency by specifying the template parameters, such as <code>map_&lt;int,int&gt;(list, [](int x) -&gt; char { return x + 1; });</code></p> <p>(although I see the functor actually returns a char, not an int, so if the type deduction worked for you here you'd be getting back a <code>vector&lt;char&gt;</code> which cannot be converted to a <code>vector&lt;int&gt;</code> when you assign the result to <code>transformed</code>) </p> <p>However as has been pointed out, generally templates take functors as just a plain template type:</p> <pre><code>template&lt;typename A,typename Func&gt; auto map_(const std::vector&lt;A&gt;&amp; orig, Func f) -&gt; std::vector&lt;decltype(f(A()))&gt; { std::vector&lt;decltype(f(A()))&gt; rv; /*...*/ } </code></pre> <p>(we use the trailing return type because we need to use the expression <code>f</code> in the return type, which isn't available unless the return type comes afterwards.)</p> <p>This allows the template to deduce the functor type directly and avoids any type conversions and best allows for optimization.</p> <p>It's also customary to use iterators as arguments on these sorts of functions, in which case your function is just a wrapper around std::transform, so you can just use that directly. I'm not sure there's a whole lot of value in a special version that deals with vectors specifically.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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