Using an object's member function as the action passed to for_each

B

Belebele

Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
....
for_each(0, 5, /*i want to call a.foo for each element in the range*/);




What is a good elegant way to do that?

What I did was to wrap the a object into a unary_function-derived
object and then pass the unary_function to the for_each method.

Any other ideas?

Thanks
 
T

Thomas J. Gritzan

Belebele said:
Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the range*/);

What is a good elegant way to do that?

#include <algorithm>
#include <boost/bind.hpp>

class A {
public:
void foo(int i) { };
};

int main()
{
A a;

int ints[5] = { 1, 2, 3, 4, 5};

std::for_each(ints, ints+5, boost::bind(&A::foo, a, _1));
}

There may be a way using std::bind* functions.
 
P

Pierre Barbier de Reuille

Belebele said:
Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the range*/);




What is a good elegant way to do that?

What I did was to wrap the a object into a unary_function-derived
object and then pass the unary_function to the for_each method.

Any other ideas?

Thanks

The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :

for_each(0, 5, std::bind1st(std::mem_fun(A::foo), a));

Another option is to use the boost::bind library, in which case it would
look like:

for_each(0, 5, bind(A::foo, a, _1));

Pierre
 
V

Victor Bazarov

Thomas said:
Belebele said:
Suppose I want to use some object's member function as the action
passed to a for_each call:

class A {
...
public:
void foo(int );
};

A a;
int ints[5];
...
for_each(0, 5, /*i want to call a.foo for each element in the
range*/);

What is a good elegant way to do that?

#include <algorithm>
#include <boost/bind.hpp>

class A {
public:
void foo(int i) { };
};

int main()
{
A a;

int ints[5] = { 1, 2, 3, 4, 5};

std::for_each(ints, ints+5, boost::bind(&A::foo, a, _1));
}

There may be a way using std::bind* functions.

std::for_each(ints, ints+5,
std::bind1st(std::mem_fun1(&A::foo), &a));

(include <functional> instead of <boost..>)

V
 
V

Victor Bazarov

Pierre said:
[..]
The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :

for_each(0, 5, std::bind1st(std::mem_fun(A::foo), a));

Try it before recommending it. It won't work. Binders are tricky.

V
 
B

Belebele

class A {
...
public:
void foo(int );
};

The best is to use std::mem_fun ... it already does what you want !
However, you need to combine it with std::bind1st like that :

for_each(0, 5, std::bind1st(std::mem_fun(A::foo), a));

So, bind1st correctly binds the implicit "this" parameter ... I was
simply confused about that point.

Thanks!
 
P

Pierre Barbier de Reuille

Belebele said:
So, bind1st correctly binds the implicit "this" parameter ... I was
simply confused about that point.

Thanks!

bind1st doesn't do anything like that. It just binds the first argument
of the function. It is std::mem_fun that convert a member function into
an object whose first argument is the object needed for the member
function. Also, as suggested higher, the for_each won't work like that,
you need an iterator (sorry, I copied that a bit quickly), so it should be:

int ints[6] = {0,1,2,3,4,5};
for_each(ints, ints+6, std::bind1st(std::mem_fun(&A::foo), &a));

(I also forgot the references ...)

Pierre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top