Re: Asymmetric Functor predicates
Stephen Howe wrote:
Is it possible to use asymmetric functor predicates?
In practice, yes. Just write several overloads of operator()
vector <point2D> v2D;
sort (v2D.begin(), v2D.end(), CCompare2D());
Now if you had
bool DoesXExist(int x)
{
return binary_search(v2D.begin(), v2D.end(), x, SomeFunctor());
}
I can easily provide some conversion constructor that turns x into a =
point2D
And then I can provide another functor that only processes just the x =
portion of point2D
But what I would like to do is get away from having conversion =
constructors at all.
I presume I would have some functor that is like so
class SomeFunctor
{
bool operator()(const point2D &e1, int x) const
{
return (e1.x < x);
}
bool operator()(int x, const point &e1) const
{
return (x < e1.x);
}
};
This is not well defined in the current standard. See
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#270
With most implementations (including the MSVC one), it would work if you =
provide three overloads - the two you show and the third taking two =
point2D's.
With C++0x, you would only need one overload of operator(), the one =
taking point2D and int. binary_search and related algorithms no longer =
requre that the sequence be sorted, but only that it be partitioned with =
respect to the search key (that is, that e < x be true for some initial =
subsequence, and false for everything else).
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not =
necessarily a good idea. It is hard to be sure where they are going to =
land, and it could be dangerous sitting under them as they fly overhead. =
-- RFC 1925