Note that there are some explanatory texts on larger screens.

plurals
  1. POMap function with c++11 constructs
    primarykey
    data
    text
    <p>Both to teach myself about implementing more advanced template constructions than simply basic ones, and becouse they are useful in many circumstances, I'm trying to implement map, filter and similar functions common in functional programming using c++11 constructs like decltype.</p> <p>I'm having trouble creating a function prototype that the compiler I use can handle, so I have to ask you how you would create something like this:</p> <pre><code>// // Takes an iterable, applies a function to every element, and returns a vector of the results // template &lt;typename T, typename Func&gt; auto map(const T&amp; iterable, Func func) -&gt; std::vector&lt; decltype( func( *iterable.cbegin() ) ) &gt; { // body snipped } </code></pre> <p>That is, this function should take any iterable and a function that takes the iterables value type as an argument and returns some sort of value. The result of the function call will be a vector, regardless of the type of iterable passed in, of the type that the passed function returns. </p> <p>The map function should accept any function with a valid prototype as argument, whether it is a functionpointer, a functor or lambda expression.</p> <p>Using the function above with this test code:</p> <pre><code>std::vector&lt;int&gt; intVector; intVector.push_back(1); intVector.push_back(2); map(intVector, [](int&amp; value) { return value + 1; }); </code></pre> <p>makes visual studio spit out a C2893 ("Failed to specialize function template") error and I'm not sure what's wrong.</p> <p><strong>Update:</strong> Applied changes suggested in comments and answers so far to question, new prototype tested but the same error remains.</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.
 

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