Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Credit goes to Mathias Gaunard on the boost.users mailing list for pointing me towards this solution:</p> <pre><code> std::for_each(values.begin(), values.end(), boost::bind(boost::ref(avg), boost::bind(&amp;value_wrapper_type::getValue, _1)) ); </code></pre> <p>Wrapping <code>avg</code> with <code>boost::ref</code> is required because otherwise a copy of <code>avg</code> is filled out with the results of <code>getValue()</code>, rather than <code>avg</code> itself.</p> <p>Here is the full compiled and tested solution:</p> <pre><code>#include &lt;stdexcept&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;boost/bind.hpp&gt; #include &lt;boost/bind/mem_fn.hpp&gt; // A value wrapper template&lt;typename T&gt; struct Value { Value(){} Value(const T&amp; value, bool valid = true):m_value(value),m_valid(valid){} T getValue(){ return m_value; } bool getValid(){ return m_valid; } void setValue(const T&amp; value){ m_value = value; } void setValid(const T&amp; valid){ m_valid = valid; } private: T m_value; bool m_valid; }; // Class that calculates the average piecewise template&lt;typename T&gt; struct Average { private: T m_numPoints; T m_ChannelSum; public: typedef void result_type; Average() : m_numPoints(0), m_ChannelSum(0.0){} result_type operator()(T value){ m_numPoints++; m_ChannelSum+=value; } double getAverage(){ if (m_ChannelSum==0) { throw std::logic_error("Cannot get average of zero values"); } return m_ChannelSum/m_numPoints; } T getCount(){ return m_numPoints; } T getSum(){ return m_ChannelSum; } }; // Run the average computation on several values int main(int argc, char** argv){ typedef int value_type; typedef Value&lt;value_type&gt; value_wrapper_type; typedef std::vector&lt;value_wrapper_type&gt; value_vector_type; value_vector_type values; values.push_back(value_wrapper_type(5)); values.push_back(value_wrapper_type(7)); values.push_back(value_wrapper_type(3)); values.push_back(value_wrapper_type(1)); values.push_back(value_wrapper_type(2)); typedef Average&lt;value_type&gt; average_type; average_type avg; #if 0 // for loop version: for(value_vector_type::iterator it = values.begin(); it!=values.end(); it++){ avg(it-&gt;getValue()); // I want to put this in a for_each loop } #else // bind version: std::for_each(values.begin(), values.end(), boost::bind(boost::ref(avg), boost::bind(&amp;value_wrapper_type::getValue, _1)) ); #endif std::cout &lt;&lt; "Average: " &lt;&lt; avg.getAverage() &lt;&lt; " Count: " &lt;&lt; avg.getCount() &lt;&lt; " Sum: " &lt;&lt; avg.getSum() &lt;&lt; std::endl; } </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