I trying to convert some loops in my code to use the for_each functionality of the STL. Currently, I calculate and accumulate two separate values over the same set of data, requiring me to loop over the data twice. In the interest of speed, I want to loop once and accumulate both values. Using for_each was suggested as it apparently can be worked into a multithreaded or multiprocessor implementation fairly easily (I haven’t learned how to do that yet.)
Creating a function that only loops over the data once and calculates both values is easy, but I need to return both. To use with for_each, I need to return both calculated values at each iteration so STL can sum them. From my understanding, this isn’t possible as for_each expects a single value returned.
The goal with using for_each, besides cleaner code (arguably?) is to eventually move to a multithreaded or multiprocessor implementation so that the loop over the data can be done in parallel so things run faster.
It was suggested to me that I look at using a functor instead of a function. However, that raises two issues.
- How will using a functor instead allow the return accumulation of two values?
- I have two methods of applying this algorithm. The current code has a virtual base class and then two classes that inherit and implement the actual working code. I can’t figure out how to have a “virtual functor” so that each method class can implement its own version.
Thanks!
Here is an example of using a functor to perform two accumulations in parallel.
[Note that you could also consider using
std::accumulate(), with an appropriate overload foroperator+.]As for virtual functors, you cannot do these directly, as STL functions take functors by value, not by reference (so you’d get a slicing problem). You’d need to implement a sort of “proxy” functor that in turn contains a reference to your virtual functor.* Along the lines of:
* Scott Meyers gives a good example of this technique in Item 38 of his excellent Effective STL.