Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is two things in your code that will not work.</p> <ul> <li>First, when passing a lambda function as argument, I suggest using <code>Template</code>. The standard library on Microsoft seems to use this method for <code>std::for_each</code> for example.</li> <li><p>And :</p> <blockquote> <p>When function template has a return type, which cannot be deduced from arguments, or when function template doesn't have any argument, the type cannot be deduced by the compiler. This function will require <em>template type argument specification</em>.</p> </blockquote></li> </ul> <p>Take a look at this example :</p> <pre><code>template&lt;class Tout, class Tin, class Fun&gt; // ^^^^^^^^^^^ // Note that I changed the order of the types std::vector&lt;Tout&gt; map( const std::vector&lt;Tin&gt;&amp; in, Fun mapper ) { // ^^^^^^^^^^ std::vector&lt;Tout&gt; ret; for( auto elem : in ) { ret.push_back( mapper( elem ) ); } return ret; } int main() { std::vector&lt;int&gt; bars /* = ... */; std::vector&lt;float&gt; foos = map&lt;float&gt;( bars, []( int ) { return 1.0f; } ); // ^^^^^^^ Specify the type Tout system( "pause" ); return 0; } </code></pre> <p><strong>EDIT :</strong></p> <p>Like it is said in the comment, we can use <code>decltype</code> and <code>std::decay</code> to not have to explicitly specify the result of the function :</p> <pre><code> template&lt;class Tin, class Fun&gt; // no Tout // ^^^^^^^^^^^ auto map( const std::vector&lt;Tin&gt;&amp; in, Fun mapper ) //^^^^ ^^^^^^^^^^ -&gt; std::vector&lt;typename std::decay&lt; decltype( mapper( in.front() ) )&gt;::type &gt; { std::vector&lt;typename std::decay&lt; decltype( mapper( in.front() ) )&gt;::type &gt; ret; for( auto elem : in ) { ret.push_back( mapper( elem ) ); } return ret; } int main() { std::vector&lt;int&gt; bars /* = ... */; std::vector&lt;float&gt; foos = map( bars, []( int ) { return 1.0f; } ); // No specification system( "pause" ); return 0; } </code></pre> <p>Let's explain a little bit.</p> <p>First we will use the <em>late-specified return type</em> syntax. It will allow us to use the parameter names in the return type specification. We start the line with <code>auto</code> and put the return type specification after the parameters using <code>-&gt;</code>.</p> <p>We will use <code>decltype</code> because the decltype type specifier yields the type of a specified expression. It will be very useful in our case. For example to get the type of the function we passed in parameters, it is just <code>decltype( f( someArg ) )</code>.</p> <p>Let's state what do we want : The return type of the function should be a vector of the return type of the function passed in argument right ? So we can return <code>std::vector&lt; decltype( mapper( in.front() ) )&gt;</code> and that's it ! (Why the <code>in.front()</code> ? We have to pass a parameter to the function to have a valid expression.)</p> <p>But here again, we have a problem : <code>std::vector</code> does not allow references. To be certain that it will not be a problem for us, we will use the <code>std::decay</code> meta-function who <em>applies lvalue-to-rvalue, array-to-pointer, and function-to-pointer implicit conversions to the type T, <strong>removes cv-qualifiers</strong>, <strong>remove references</strong>, and defines the resulting type as the member typedef type.</em>. That is, if the function returns something like <code>const Foo&amp;</code> it will end in <code>Foo</code>.</p> <p>The result of all of that : <code>std::vector&lt; typename std::decay&lt; decltype( mapper( in.front() ) )&gt;::type &gt;</code>.</p> <p>You have to repeat this expression again at the beginning of the function to declare the variable you will return.</p> <p>Some usefull references about that :</p> <ul> <li><a href="http://en.cppreference.com/w/cpp/types/decay" rel="nofollow">http://en.cppreference.com/w/cpp/types/decay</a></li> <li><a href="http://en.wikipedia.org/wiki/Decltype" rel="nofollow">http://en.wikipedia.org/wiki/Decltype</a></li> <li><a href="http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html" rel="nofollow">http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/dd537655.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd537655.aspx</a></li> </ul> <p>It is not easy to explain, I hope my explanations are understandable.</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.
    3. 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