[Lambda] Question about calling a member function inside a lambda construct

Hello, I'm rather stucked in using lambda expressions for a presumably simple task. Suppose I have objects with a member function bool is_empty() const; stored in a vector: I want to count the number of objects for which their member functions returns false, which is simple, but not elegant to achieve by size_t sum = 0; for( Foo::const_iterator it = vec.begin(); it != vec.end(); ++it ) if ( it->is_empty() == false ) sum++; I want to exchange this for a lambda expression, and came up with the following after some thinking. for_each( begin(), end(), if_then( !(_1 ->* &GridCell::is_empty)(), var(sum)++ ) ); This is still wrong and leads to two error messages: error C2784: 'const boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lamb da::logical_action<Action>,boost::tuples::tuple<boost::lambda::lambda_functo r<T>>>> boost::lambda::operator !(const boost::lambda::lambda_functor<T> &)' : could not deduce template argument for 'const boost::lambda::lambda_functor<T> &' from 'boost::tuples::null_type' error C2675: unary '!' : 'boost::tuples::null_type' does not define this operator or a conversion to a type acceptable to the predefined operator Could anyone tell how to get this working? Regards Hendrik -- Hendrik Belitz ISD Software und Systeme GmbH Hauert 4 D - 44227 Dortmund Germany Fon: +49 (0)231/97 93-0 Fax: +49 (0)231/97 93-101 Mail: <mailto:hBelitz@isdcad.de> hBelitz@isdcad.de Internet: <http://www.isdcad.de/> www.isdcad.de Geschäftsführung: Günter Flassig (Vors.), Dr. Jörg Ruhwedel Sitz Dortmund, Amtsgericht Dortmund HRB 4601

Hendrik Belitz wrote:
Hello,
I'm rather stucked in using lambda expressions for a presumably simple task. Suppose I have objects with a member function bool is_empty() const; stored in a vector: I want to count the number of objects for which their member functions returns false, which is simple, but not elegant to achieve by size_t sum = 0; for( Foo::const_iterator it = vec.begin(); it != vec.end(); ++it ) if ( it->is_empty() == false ) sum++;
size_t sum = std::count_if( vec.begin(), vec.end(), !bind( &GridCell::is_empty, _1 ) );
I want to exchange this for a lambda expression, and came up with the following after some thinking.
for_each( begin(), end(), if_then( !(_1 ->* &GridCell::is_empty)(), var(sum)++ ) );
Maybe another pair of parentheses will help? !((_1 ->* &GridCell::is_empty)())

Thanks Peter,
size_t sum = std::count_if( vec.begin(), vec.end(), !bind( &GridCell::is_empty, _1 ) );
Well, for some strange reason I do not see the wood for the trees in this case. I do not even thought about using std::count_if. ;)
Maybe another pair of parentheses will help? !((_1 ->* &GridCell::is_empty)())
Doesn't work either, that still produces the same errors. Nevertheless, the above solution seems much more readable to me anyway, although I still would like to know if this could be also achieved by using boost::lambda in some way. Regards Hendrik _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Hendrik Belitz
-
Peter Dimov