On Sat, Aug 23, 2003 at 11:21:03AM +0200, Daniel Frey wrote: > a) Short-circuiting > b) Unnamed functions > > Jan Langer wrote: > >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. > > 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*/ );
I have imagined another syntax which addresses the issues somewhat: // use boost::lamdba boost::lexicographic<person> ( _1.first, cmp_lower ) /* see below */ ( _1.last, cmp_lower ) ( bind(&T::age,_1) /* use default cmp */ ) ( p1, p2 ) The basic idea is you say lexicographic<T> to start creating a comparator for T objects, and then use continual argument lists (as in the original example), but where each argument list is either ( lambda(T){ return T.component }, component_comparator ) or ( lambda(T){ return T.component } ) // uses default comparator Finally, when you see an argument list with arguments of type ( T, T ) you know you are done and it is time to compare now. Of course, if you leave off this final argument list, then you get back a function object which can be passed, e.g., to std::sort() as in the example above. The problem (cited "see below") is that since operator "." isn't overloadable, the boost::lambda expressions like "_1.first" don't actually exist. Offhand I dunno if C++ in general or Boost in particular offers a nice way to lambda-ify such a member-variable expression. Anyway, just wanted to share some ideas which I think may be a good direction to go to address the issues Daniel raised. -- -Brian McNamara ([EMAIL PROTECTED]) _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost