Aha! I had to add:
typedef void result_type;
to the body of the avg object. Then your example:
for_each(values.begin(), values.end(),
bind(avg, bind(&value_wrapper_type::getValue, _1))
);
Worked perfectly! Thank you very much for the help Mathias, it is
greatly appreciated.
Cheers!
Andrew Hundt
On Thu, Mar 1, 2012 at 2:44 PM, Mathias Gaunard
On 03/01/2012 05:52 PM, Andrew Hundt wrote:
I have a class with an accessor member function that I want to call and apply the result to a functor using std::for_each. I have a working version below that uses a for loop and for_each, but the for_each version is cryptic and cumbersome. Is there a way I can make the for_each version more concise?
#if 0 // for loop version: for(value_vector_type::iterator it = values.begin(); it!=values.end(); it++){ avg(it->getValue()); // I want to put this in a for_each loop } #else // bind version: std::for_each(values.begin(), values.end(), // iterate over all values boost::bind( boost::mem_fn(&average_type::operator()), // attach the averaging functor to the output of the getvalue call &avg, boost::bind( boost::mem_fn(&value_wrapper_type::getValue), // bind the getValue call to each element in values _1 ) ) ); #endif
for_each(values.begin(), values.end(), bind(avg, bind(&value_wrapper_type::getValue, _1)) );
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users