Boost logo

Boost Users :

From: Alexander Borghgraef (alexander.borghgraef.rma_at_[hidden])
Date: 2005-08-18 03:26:19


 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 list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net