Using lambda to count elements in list of lists

Hello! I'm trying to use Boost.Lambda to count the total number of elements in a list of lists. This example shows what I'm trying to do: #include <boost/lambda/lambda.hpp> #include <algorithm> #include <list> using namespace std; using namespace boost::lambda; int main() { int count = 0; list<list<int> > ll; for_each(ll.begin(), ll.end(), var(count)+=(_1).size()); } This will not compile: (13): error C2039: 'size': is not a member of 'boost::lambda::lambda_functor<T>' Is this possible to do? Thanks in advance! Hälsningar, Daniel

Daniel Lidström wrote:
Hello!
I'm trying to use Boost.Lambda to count the total number of elements in a list of lists. This example shows what I'm trying to do:
#include <boost/lambda/lambda.hpp> #include <algorithm> #include <list>
using namespace std; using namespace boost::lambda;
int main() { int count = 0; list<list<int> > ll; for_each(ll.begin(), ll.end(), var(count)+=(_1).size()); }
This will not compile: (13): error C2039: 'size': is not a member of 'boost::lambda::lambda_functor<T>' Is this possible to do?
AFAICT, no. Not unless the type of _1 implements all possible member functions in the world :-) This is the single biggest problem of a non-langauage solution to lambdas. -Thorsten

Thorsten Ottosen wrote:
Daniel Lidström wrote:
int main() { int count = 0; list<list<int> > ll; for_each(ll.begin(), ll.end(), var(count)+=(_1).size()); }
This will not compile: (13): error C2039: 'size': is not a member of 'boost::lambda::lambda_functor<T>' Is this possible to do?
AFAICT, no. Not unless the type of _1 implements all possible member functions in the world :-)
for_each( ll.begin(), ll.end(), count += bind( &list<int>::size, _1 ) ); works for me.

On 2/1/06, Daniel Lidström <daniel.lidstrom@sbg.se> wrote:
Hello!
I'm trying to use Boost.Lambda to count the total number of elements in a list of lists. This example shows what I'm trying to do:
#include <boost/lambda/lambda.hpp> #include <algorithm> #include <list>
using namespace std; using namespace boost::lambda;
int main() { int count = 0; list<list<int> > ll; for_each(ll.begin(), ll.end(), var(count)+=(_1).size()); }
This will not compile: (13): error C2039: 'size': is not a member of 'boost::lambda::lambda_functor<T>' Is this possible to do? Thanks in advance!
Hälsningar, Daniel
You'll need to use lambda's bind to access the size member function: for_each(ll.begin(), ll.end(), var(count)+=bind(&list<int>::size, _1)); You might might find std::accumulate a better match for your requirements: count = accumulate(ll.begin(), ll.end(), 0, _1 + bind(&list<int>::size, _2)); Stuart Dootson

Daniel Lidström wrote:
for_each(ll.begin(), ll.end(), var(count)+=(_1).size());
This will not compile: (13): error C2039: 'size': is not a member of 'boost::lambda::lambda_functor<T>' Is this possible to do? Thanks in advance!
Yes, but you need binding syntax: typedef list< list< int > > lli; for_each(ll.begin(), ll.end(), var(count) += bind(&lli::size, _1)); Sebastian Redl
participants (5)
-
Daniel Lidström
-
Peter Dimov
-
Sebastian Redl
-
Stuart Dootson
-
Thorsten Ottosen