Note that there are some explanatory texts on larger screens.

plurals
  1. POSet the generic return type
    primarykey
    data
    text
    <p>I've a function that works well for me:</p> <pre><code>template &lt;typename __container, typename __callback = std::function &lt; void (typename __container::value_type, typename __container::value_type)&gt; &gt; int reduce(__container &amp;&amp;container, __callback &amp;&amp;callback) { auto current = container.begin(); for (auto value : container) *current = callback(*current, value); return *current; }; int result = functional::reduce(vector, [](const int current, const int next) -&gt; int { return current + next; }); std::cout &lt;&lt; result; // 10 </code></pre> <p>And now, I'm going to unify the one:</p> <pre><code>template &lt;typename __container, typename __callback = std::function &lt; void (typename __container::value_type, typename __container::value_type)&gt; &gt; auto reduce(__container &amp;&amp;container, __callback &amp;&amp;callback) -&gt; decltype(__container::value_type); </code></pre> <p>However I am getting the following error:</p> <pre><code>reduce.cpp:61:9: error: no matching function for call to 'reduce' int i = reduce(vector, [](const int current, const int next) -&gt; int { ^~~~~~~~~~~~~~~~~~ reduce.hpp:69:7: note: candidate template ignored: substitution failure [with __container = std::__1::list&lt;int, std::__1::allocator&lt;int&gt;&gt; &amp;, __callback = &lt;lambda at nott.cpp:61:36&gt;] auto reduce(__container &amp;&amp;container, __callback &amp;&amp;callback) -&gt; decltype(__container::value_type) ^ 1 error generated. make: *** [build] Error 1 </code></pre> <p>How can I set the generic return type?</p> <p><strong>UPDATE:</strong></p> <pre><code>template &lt;typename __container, typename __callback&gt; auto reducef(const __container&amp; input, __callback callback) -&gt; decltype(callback(std::declval&lt;typename __container::value_type&gt;(), std::declval&lt;typename __container::value_type&gt;())) { decltype( callback( std::declval&lt;typename __container::value_type&gt;(), std::declval&lt;typename __container::value_type&gt;() ) ) result{}; return std::accumulate(input.begin(), input.end(), result, callback); }; </code></pre>
    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. 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