Re: standard vs. hand crafted loops
In article <4d33o4F1876p5U1@news.dfncis.de>,
Thomas Richter <thor@mersenne.math.TU-Berlin.DE> wrote:
Hi,
In article <1147512350.709207.31700@y43g2000cwc.googlegroups.com>,
"Andrei Polushin" <polushin@gmail.com> wrote:
It seems to me that when Mr. Richter said, "My major concern on the
STL
algorithms is that the job they perform is so trivial that you can
really write them up yourself..." he was saying that the job the
algorithm performs is trivial, not that the way it was used was too
complex. In other words, you are establishing a different argument.
The point is that algorithms like "std::find_if" require you to write
up a predicate for the algorithm to look at, and writing this predicate
requires the creation of a helper class that provides an operator().
Yes. I understand that was your point, Andrei didn't. OO and generic
programming provide structure, you specialize that structure by
providing some details. With OO you override virtual functions, with
generic programming you provide types and functors.
Do you also object to overriding virtual functions?
Now, looking at the final code, my judgement is that a three-line loop
that contains one single "if" clause is an idiom every C/C++ programmer
understands, it is easy to read, understand and maintain, whereas the
same solution using std::find_if requires you to read thru the helper
class that is somewhere declared outside of the function body.
With a well named functor, this isn't an issue. You don't need to read
through the helper class, the name tells you what it does.
I don't see that the solution is any clearer, except possibly if I can
reuse the predicate,
With find_if, it isn't the predicate you are reusing (usually) its the
algorithm itself. Why write 30 different find loops in your code when
you can just reuse the one you got?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]