|
Boost : |
From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-08-24 13:05:34
Jan Langer wrote:
> Daniel Frey wrote:
>
>> The point is, that Jan proposed to add something which has a hidden
>> overhead and I'm not sure it's a good idea. Instead of nested
>> if-else-cascades, the user could also write:
>>
>> bool operator<( const person& lhs, const person& rhs )
>> {
>> return
>> lhs.lastname != rhs.lastname ? lhs.lastname < rhs.lastname :
>> lhs.firstname != rhs.firstname ? lhs.firstname < rhs.firstname :
>> lhs.age < rhs.age;
>> }
>>
>> This scheme is easy to remember, (IMHO) easy to read, doesn't have
>> nested if's and it has short-circuiting. It's only cost which may be
>> removed by Jan's proposal is that it removes the redundancy in typing.
>> A per line-macro could do that without further problems, but I won't use
>
> could you write this macro? i dont see a way to end the recursion.
I guess you are thinking about something different than I was. I just
wanted to write a macro for each of the above lines. Like this:
#define FOO(a,b) ((a)!=(b))?((a)<(b)):
bool operator<( const persion& lhs, const person& rhs )
{
return
FOO( lhs.lastname, rhs.lastname )
FOO( lhs.firstname, rhs.firstname )
lhs.age < rhs.age;
}
But wait a minute. Thinking about my old proposed macros once more, I
think the following might also work:
#define B1(a,b) ((a)!=(b))?((a)<(b)):B2
#define B2(a,b) ((a)!=(b))?((a)<(b)):B1
#define BOOST_LEXICOGRAPHIC B1
const bool B1 = false, B2 = false;
Now your syntax is:
bool operator<( const person& lhs, const person& rhs )
{
return BOOST_LEXICOGRAPHIC
( lhs.lastname, rhs.lastname )
( lhs.firstname, rhs.firstname )
( lhs.age, rhs.age );
}
Without playing dirty tricks on true/false, with short-circuiting but
without comparators. (Yes, B1 and B2 should be much longer names)
> on the other hand i should also add a section to the documentation to
> explain the drawbacks of using this class.
Definitely. It's OK if people know about a problem and choose to ignore
it. It's not OK if they inherit a problem and they don't know why.
Regards, Daniel
-- Daniel Frey aixigo AG - financial training, research and technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk