[Effective-cpp] Item 1: Uses and Abuses of vector

Terje Slettebø tslettebo at broadpark.no
Sat Oct 23 12:19:09 EDT 2004


>From: "Paul Grenyer" <paul at paulgrenyer.co.uk>


> >> However using the algos with the current facilities of C++ mey be not
too
> >> productive, and create obscure or even unreadable code.   While a for
> >> loop
> >> is easy to read for anyone I met.   That is sad, as I'd be a big fan of
> >> algos too, but that is the truth.
> >
> > Facilities such as Boost.Lambda may help here.
>
> Eh? Lambda doesn't improve _readability_ and certainly isn't intuitive.
:-)

I assumed that Balog referred to the kinds of expressions you may get if you
use a complicated expression e.g. as the function object for for_each, or
some predicate. Since this is the effective-cpp list (reuse is good. ;) ), I
refer to the kind of expressions that Scott Meyers have in "Effective STL"
Item 47: "Avoid producing write-only code", such as (to quote part of the
expression at page 206):

vector<int> v;
int x, y;
...
find_if(v.rbegin(), v.rend(), bind2nd(greater_equal<int>(), y));

This may be written using BLL as:

find_if(v.rbegin(), v.rend(), _1 >= y);

Do you still not find this clearer than the first example? BLL is
definitively about improving readability. It's also intuitive to use (at
least for relatively simple expressions), at least if you know how to use
it.

(I didn't provide an example in the previous posting, as I've mentioned BLL
a few times before (including the latest C Vu "Student Code Critique"), so I
didn't want to "nag" about it (for lack of a better expression). :) )

Regards,

Terje




More information about the Effective-cpp mailing list