[boost-users][bind] binding std::for_each

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

"Igor R"
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
vec; void f(function
); // 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

2009/3/3 Igor R
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
vec; void f(function
); // 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
That's what lambda algorithms are for.
#include <vector>
#include
participants (3)
-
Alexander
-
Igor R
-
Roman Perepelitsa