Re:Iterators: Signs of Weakness in Object-Oriented Languages
by Figs on Tuesday May 04, 2010 @08:30PM
(#1319910)
Of course, C++ already has std::accumulate, which is basically the same thing as a general fold or a summation, depending on which version you use. So, there isn't really much of a reason to write sum manually at all. :p
Re:Iterators: Signs of Weakness in Object-Oriented Languages
by scott_s on Tuesday May 04, 2010 @05:34PM
(#1319415)
Indeed. I actually use this technique in my own code, and I feel like I'm laying down pipe all over the place to make sure the captured variable gets routed to the function object correctly. Once you do it a few times, it is obviously boiler-plate code and obviously a hindrance to momentum.
The verbosity of the fragment above is greatly reduced. You had to write 'sum' or 's' lots of extra times ('Summation(sum)', 'sum(s)', 'int& sum', and 'int& s'), and you won't have to do that anymore -- according to wikipedia your function will change in C++0x to:
Which is semantically much less to think about. It also still has the feature / annoyance (?) of having to specifically identify which variables to capture from the environment ('sum', as a reference; it seems you can capture variables by value too).Fortunately, it seems the C++ people even thought of that annoyance, and allow you to write:
capturing references to all variables in the enclosing scope. Neat!