Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2006-04-07 11:23:17


On 4/7/06, Imre <imre42_at_[hidden]> wrote:
>
> Hi
>
> I'm new to the boost::lambda lib, and have a problem.
> I have a list that stores struct objects. One field of the struct is a
> function pointer. I'd like to call the function pointer stored in each
> element of the list, but I can't get it right.
> First, I tried something like this:
>
> struct S
> {
> typedef void (*Func)();
> Func f;
> ...
> };
> ...
> std::list<S> l;
> ...
> for_each(l.begin(), l.end(), bind(&S::f, _1));
>
> I think this doesn't work because the bind yields a function object that
> returns f itself, but doesn't call it. Instead, I'd need to build a
> function object that returns the result of calling the f stored in the
> current list node. Unfortunately, I don't know how to do it.
> Thanks for any help.
>
> Imre

Try this:

   for_each(l.begin(), l.end(), bind(bind(&S::f, _1)));

Tested & works under gcc 4.0.1, OS X 10.4

Stuart Dootson


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