std::find_if, boost::mem_fn and boost::not1

Hi All,
I'm just getting into using boost and stl algorithms, and I'm getting
some indecipherable compiler errors that I'd like some help understanding.
Here's some sample code:
<------------------------SNIP------------------------>
#include

Pete Hodgson wrote:
Hi All,
I'm just getting into using boost and stl algorithms, and I'm getting some indecipherable compiler errors that I'd like some help understanding.
Here's some sample code:
[...]
typedef std::vector< boost::shared_ptr<CEgg> > tEggsVector;
//this function doesn't compile, //presumably because of the boost::not1 usage bool AllEggsHaveHatched() { tEggsVector basketOfEggs;
tEggsVector::iterator it = std::find_if( basketOfEggs.begin(), basketOfEggs.end(), boost::not1( boost::mem_fn( &CEgg::IsHatched ) ) );
not1 doesn't work well with boost::mem_fn applied to shared_ptr, but you may try !boost::bind( &CEgg::IsHatched, _1 ) instead.

Peter Dimov wrote: ...
not1 doesn't work well with boost::mem_fn applied to shared_ptr, but you may try
!boost::bind( &CEgg::IsHatched, _1 )
instead.
I tried the following:
bool SomeEggsHaveHatched()
{
tEggsVector basketOfEggs;
using boost::lambda::_1;
tEggsVector::const_iterator it = std::find_if(
basketOfEggs.begin(),
basketOfEggs.end(),
boost::bind( &CEgg::IsHatched, _1 ) );
bool bSomeEggsHaveHatched = ( it != basketOfEggs.end() );
return bSomeEggsHaveHatched;
}
but it won't compile, giving:
<------------------------------SNIP--------------------------------------->
c:\WORK\4DCapture_Soln\thirdparty\boost\boost\bind\mem_fn_template.hpp(90)
: error C2664: 'R boost::_mfi::cmf0

Pete Hodgson wrote:
Peter Dimov wrote: ...
not1 doesn't work well with boost::mem_fn applied to shared_ptr, but you may try
!boost::bind( &CEgg::IsHatched, _1 )
instead.
I tried the following:
bool SomeEggsHaveHatched() { tEggsVector basketOfEggs;
using boost::lambda::_1;
tEggsVector::const_iterator it = std::find_if( basketOfEggs.begin(), basketOfEggs.end(), boost::bind( &CEgg::IsHatched, _1 ) );
bool bSomeEggsHaveHatched = ( it != basketOfEggs.end() ); return bSomeEggsHaveHatched; }
but it won't compile, giving:
[...]
Any clues as to what's going on here?
You are using boost::bind with boost::lambda::_1. Remove the using boost::lambda::_1 line.

Pete Hodgson wrote:
using boost::lambda::_1;
tEggsVector::const_iterator it = std::find_if( basketOfEggs.begin(), basketOfEggs.end(), boost::bind( &CEgg::IsHatched, _1 ) );
You're using lambda placeholders for the bind library binders. You need to use the bind library placeholders or the lambda library binders. In other words, you must not mix the two. It's rather unpleasant, but at the moment they are not compatible. Sebastian Redl

Sebastian Redl wrote:
Pete Hodgson wrote:
using boost::lambda::_1;
tEggsVector::const_iterator it = std::find_if( basketOfEggs.begin(), basketOfEggs.end(), boost::bind( &CEgg::IsHatched, _1 ) );
You're using lambda placeholders for the bind library binders. You need to use the bind library placeholders or the lambda library binders. In other words, you must not mix the two. It's rather unpleasant, but at the moment they are not compatible.
Sebastian Redl
Ah, I didn't realize that there were two different _1 implementations. Thanks for the help guys.
participants (3)
-
Pete Hodgson
-
Peter Dimov
-
Sebastian Redl