Asymmetrical predicates in STL algorithms

I only stumbled to this problem recently, after happily using the patently unportable solution for years (with numerous versions of STL to boot). Let's say we have a sorted vector of complex objects with a fairly simple key, and want to do lower_bound() on just a key, not a specially constructed instance of the whole object. It turns out that: bool less( const Object&, const Key& ); lower_bound( v.begin(), v.end(), Key(something), ptr_fun(less) ); is expressly prohibited by the SGI STL specifications and not quite allowed, even though not quite prohibited either, by the ISO standard (unless my standardese grew rusty again). It also turns out that there are STLs where it won't work (e.g. the new Plauger's STL shipped with VStudio 2005 release, but not beta 2...). Now, this: struct less { bool operator()( const Object&, const Key& ) const; bool operator()( const Key&, const Object& ) const; }; lower_bound( v.begin(), v.end(), Key(something), less() ); should work everywhere, or so I think. Would be nice to be able to create such an adaptor for a pair of functions (or a pair of bind or lambda expressions for that matter). I blogged a simple solution (for functions only) here http://swsw.motovilov.net/weblogs/swsw/archives/2006/02/asymmetrical_pr.html but am wondering if perhaps I missed a generic solution in one of the Boost libraries, or there's one coming in the new release? Any comments/suggestions? Regards, ...Max...

"Max Motovilov"
Now, this:
struct less { bool operator()( const Object&, const Key& ) const; bool operator()( const Key&, const Object& ) const; };
lower_bound( v.begin(), v.end(), Key(something), less() );
should work everywhere, or so I think. Would be nice to be able to create such an adaptor for a pair of functions (or a pair of bind or lambda expressions for that matter). I blogged a simple solution (for functions only) here http://swsw.motovilov.net/weblogs/swsw/archives/2006/02/asymmetrical_pr.html but am wondering if perhaps I missed a generic solution in one of the Boost libraries, or there's one coming in the new release?
Well, standard libraries are now required to make it legal: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#270 And you can get an implementation where it works at http://www.boost-consulting.com/vault/index.php?&direction=0&order=&directory=Algorithms/binary_search HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com

Dave,
Thanks! Isn't there a problem with equal_range() where both the first
edition of the standard and the work paper you referred to explicitly
mention comp(e,value) and comp(value,e)? That, while consistent with the
behavior of the original STL, seems to still require a symmetrical
predicate...
Regards,
...Max...
"David Abrahams"
"Max Motovilov"
writes: Now, this:
struct less { bool operator()( const Object&, const Key& ) const; bool operator()( const Key&, const Object& ) const; };
lower_bound( v.begin(), v.end(), Key(something), less() );
should work everywhere, or so I think. Would be nice to be able to create such an adaptor for a pair of functions (or a pair of bind or lambda expressions for that matter). I blogged a simple solution (for functions only) here
http://swsw.motovilov.net/weblogs/swsw/archives/2006/02/asymmetrical_pr.html
but am wondering if perhaps I missed a generic solution in one of the Boost libraries, or there's one coming in the new release?
Well, standard libraries are now required to make it legal:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#270
And you can get an implementation where it works at
HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com

"Max Motovilov"
Dave,
Thanks! Isn't there a problem with equal_range() where both the first edition of the standard and the work paper you referred to explicitly mention comp(e,value) and comp(value,e)? That, while consistent with the behavior of the original STL, seems to still require a symmetrical predicate...
Yes, equal_range does require a symmetrical predicate in the sense that you can pass both e or value as either parameter. However, e and value don't have to be the same type. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
Max Motovilov