
"Igor R" <boost.lists@gmail.com> wrote in message news:cfe0a3cf0903031205u428d6a7at944b4fcd89001a64@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.