Re: STL binary_function in VS 2005
"Sytse" <no@nowhere.xx> wrote in message
news:459faaac$0$323$e4fe514c@news.xs4all.nl
Below is a code snippet I created for testing one of the problems that
is starting to give me a headache. For some reason mixing types in the
binary_function template goes horribly wrong in the lower_bound
function (for the example I just used a pointer as the other type,
but in the legacy code this is in fact a completely different class).
//Used for finding elements
class TestFind : public std::binary_function<Test,Test*,bool>
{
public:
//Swapping the parameters does not solve the error
bool operator()(const Test& l, const Test* r)const
{
return l.Get() < r->Get();
}
/*Adding this function does not solve the problem
bool operator()(const Test* l, const Test& r)const
{
return l->Get() < r.Get();
}*/
};
First, binary_function is a red herring. You can remove the derivation
from binary_function, and nothing will change, either under VC7 or VC8.
What you see is a known problem. See
http://groups.google.com/group/microsoft.public.vc.stl/browse_frm/thread/726a858aa900cbe4
http://msdn2.microsoft.com/en-us/library/aa985982(VS.80).aspx
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#270
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2001/n1313.html
Have your predicate provide three different overloads:
operator(const Test&, const Test&);
operator(const Test&, const Test*);
operator(const Test*, const Test&);
With all three in TestFind, your sample works. Alternatively, you can
turn off iterator debugging as shown in the second article I quote.
--
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