
It's all about types. In the stand alone expression, the bind() result is evaluated by passing it x, yielding an int which is compared to the int y, so operator== is called on two ints. In the find_if call, the bind() result is *not* evaluated so operator== is called on the bind type and an int. The bottom line is that you can only use functions and functors with bind, not operators like "==". If you want to do that kind of thing, you should look in to the Boost.lambda library. As a side note, is there a standard/Boost library with the functor equivalent of std::equal_to for the unary operators & and * ? Or do I need to write those myself? At 03:26 AM 8/18/2005, you wrote:
Ok, I've just discovered boost::bind, and it is a marvelous discovery :-) Now, I do seem to have a problem: the operator overloading for logical operators seems to work on its own, but not when I apply it within standard algorithms such as find_if. Still, the bind documentation states that this should be possible, and gives examples such as: std::remove_if( first, last, !bind( &X::visible, _1 ) ); std::find_if( first, last, bind( &X::name, _1 ) == "peter" );
If we look at some simple test code I wrote:
int add3( int i ){ return i + 3; };
int main() { int x = 2; int y = 5; bind( add3, _1)(x) == y; // Compiles find_if( l.begin(), l.end(), bind<int>( equal_to<int>(), bind(add3, _1), y) ); // Compiles find_if( l.begin(), l.end(), bind(add3, _1) == y ); // Doesn't compile }
From the last line I get the following error message: error: no match for 'operator==' in 'boost::bind [with R = int, B1 = int, A1 = boost::arg<1>](add3, (<unnamed>::_1, boost::arg<1>())) == y'
I'm puzzled why the standalone bind expression containing == does compile, whereas the one inside find_if doesn't. Of course, I do have a functional equivalent I can work with in the first find_if line, but it does seem neat to be able to write more readable code, as in the examples. Anyone care to explain this to me? Ok, standard disclaimers: I use the g++ 3.4.4 compiler on Fedora, I did include all necessary boost:: and std:: namespace stuff in my code, but left it out in this post, and I have no clue whatsoever which version of boost I have installed ( Can I find it in the code somewhere? Otherwise I'll ask the sysadmin which version he installed when I see him.).
-- Alex Borghgraef
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users