[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
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