std::for_each with member function as reference

Started by
10 comments, last by pointer 13 years, 7 months ago
Here's a simplified version of my code.

class ObjectA{  ...}typedef std:vector<ObjectA*> ObjectAVector;class ObjectB{  void Apply(Object& A)  {    ...  }  void Apply(ObjectAVector& av)  {    for(ObjectAVector::iterator it = av.begin();                                it != av.end(); ++it)    {      Apply(**it);    }  }}


I am wondering, is there a way to convert that iterator loop with a std::for_each algorithm. Using boost is possible as well as anything available within the C++ standard in VS2008.
Advertisement
This would be my favourite solution in c++:
// possibly in the precompiled header...#include <boost/foreach.hpp>#define BOOST_FOREACH foreachvoid Apply(ObjectAVector& av){    foreach(ObjectA* obj, av)    {        Apply(*obj);    }}
You could do something like this:

#include <functional>...std::for_each(av.begin(), av.end(), std::bind1st(std::mem_fun(&ObjectB::Apply), this));


but you'd have to change ObjectB::Apply(ObjectA&) to ObjectB::Apply(ObjectA*), I didn't manage to make it work with references. Maybe someone else can help us out here?

[Edited by - pointer on September 10, 2010 7:44:55 AM]
If the argument to Apply() was ObjectA* and not ObjectA&, I think this should work:

std::for_each(av.begin(), av.end(), boost::bind(&ObjectB::Apply, this, _1));

But if the argument is of type ObjectA&, I'm not sure it's possible without defining a custom function object.
Quote:Original post by pointer
I didn't manage to make it work with references. Maybe someone else can help us out here?
boost::bind ought to work fine with references.

I am not familiar enough with the std::binders to figure it out there, but I *think* it ought to be possible as well.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

SiS-Shadowman's solution is also my favorite. Then comes using the loop. And I would never use std::for_each for this: It's ugly, unreadable and hard to modify.

Why are you using a vector of pointers? I prefer using a vector of objects, unless there is a good reason to use pointers (e.g., objects are HUGE and relocations would be too expensive, or I need polymorphism). If I really need to use pointers, I use a vector of shared_ptr or boost::ptr_vector.

Quote:Original post by alvaro
Why are you using a vector of pointers? I prefer using a vector of objects, unless there is a good reason to use pointers (e.g., objects are HUGE and relocations would be too expensive, or I need polymorphism). If I really need to use pointers, I use a vector of shared_ptr or boost::ptr_vector.


You are quite right. My example above was just a much simplified version of the actual code as a way to determine if my question could be done simply. I'am indeed using using a vector of smart pointers, which unfortunately brings their own little complication with something like this. :)

I like to thanks everyone for their answers though. I am still getting my head around boost and binds as I have heard that doing your own vector loops is inefficient compared to the std algorithm.
Quote:Original post by pointer
You could do something like this:

*** Source Snippet Removed ***

but you'd have to change ObjectB::Apply(ObjectA&) to ObjectB::Apply(ObjectA*), I didn't manage to make it work with references. Maybe someone else can help us out here?


Umm... mem_fun_ref? :)
Just for the sake of conversation:

std::for_each(av.begin(), av.end(), [=] (ObjectA* a) { Apply(a); } );

Quote:Original post by Chrominium
I am still getting my head around boost and binds as I have heard that doing your own vector loops is inefficient compared to the std algorithm.


Don't believe everything you hear.

This topic is closed to new replies.

Advertisement