[LAMBDA] calling a function pointer stored in a struct

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[LAMBDA] calling a function pointer stored in a struct

Imre-3

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

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [LAMBDA] calling a function pointer stored in a struct

Stuart Dootson
On 4/7/06, Imre <[hidden email]> 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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users