Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost-users][bind] binding std::for_each
From: Alexander (gutenev_at_[hidden])
Date: 2009-03-04 02:10:54


"Igor R" <boost.lists_at_[hidden]> wrote in message
news:cfe0a3cf0903031205u428d6a7at944b4fcd89001a64_at_mail.gmail.com...
> Hello,
>
> I'd like to bind std::for_each with some other functor created on the
> fly, like this:
>
> struct A
> {
> void f(int, int);
> };
>
> vector<shared_ptr<A> > vec;
>
> void f(function<void(void)>);
>
> // I want to call f() with a functor that would call A::f for every
> element in vec
>
> f(bind(&for_each, vec.begin(), vec.end(), bind(&A::f, _1, 1))); //
> doesn't compile
>
> f(bind(&for_each, vec.begin(), vec.end(), protect(bind(&A::f, _1,
> 1)))); // also doesn't compile
>
>
>
> Thank you.

As for_each is a template, to take its address you must pass template
parameters. As bind return type is unspecified, you would have to wrap inner
bind with function.

Like this (didn't test):

typedef function<void(A*)> Inner;
 f(bind(&for_each<Vec::interator, Inner>, vec.begin(), vec.end(),
Inner(bind(&A::f, _1, 1))));

And note that you should not write such way even if it works if you are
going to make it portable between various STL implementetion, because
for_each may take additional defaulted parameters.

It is better just to express outer bind as an own class with operator(), not
as bind.


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