Boost logo

Boost :

From: Daryle Walker (dwalker07_at_[hidden])
Date: 2003-08-24 00:09:24


On Saturday, August 23, 2003, at 11:21 AM, Daniel Frey wrote:

> Jan Langer wrote:
>> what is needed for the lexicographic class to be included into boost?
>
> I don't know, but I think there are several ideas which haven't been
> addressed and that we should at least have a look at:
>
> a) Short-circuiting
> b) Unnamed functions
>
>> bool operator < (person const &p1, person const &p2)
>> {
>> return boost::lexicographic
>> (p1.lastname, p2.lastname, cmp_lower)
>> (p1.firstname, p2.firstname, cmp_lower)
>> (p2.age, p1.age);
>> }
>
> The two points listed above are both visible here: Even if there are
> no persons with the same name, age is accessed. Even worse, it may be
> a costly method age() and the unaware user creates much worse code
> than using nested if-else-structures.

Short-circuiting can only be done via compiler magic with the built-in
operator&& and operator||, so there's no way it can be added to
user-defined routines. Note that each step of the lexicographic
comparison has a result, and that later steps are only evaluated if the
previous step didn't give a result of equivalence (well, the comparison
is skipped; each argument still has to be evaluated). You can also
manually check each intermediate result and code out other checks (with
"if" statements), which would skip unnecessary evaluations of later
components.

> Also, you might not want to define operator<. Sometimes, you just need
> it in one place, e.g. std::sort( v.begin(), v.end(), /*HERE*/ );

Maybe Boost.Lamda or Boost.MPL can be used?

> I don't know how to solve these problems, though. The only idea I have
> removes the comparator and uses macros. It does provide
> short-circuiting, but I fear I might get stoned now. Anyway:
>
> #define false(a,b) (!((a)==(b)))?((a)<(b)):!true
> #define true(a,b) ((a)==(b))?((a)<(b)):false
> #define BOOST_LEXICOGRAPHIC false

I don't think you can (re#)define a keyword.

> bool operator<( const person& p1, const person& p2 )
> {
> return BOOST_LEXICOGRAPHIC
> ( p1.lastname, p2.lastname )
> ( p1.firstname, p2.firstname )
> ( p1.age, p2.age );
> }

Daryle


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk