tags:

views:

63

answers:

2

I have a list of Thing and a Controller that I want to notify() with each of the things. The code below works:

#include <algorithm>
#include <iostream>
#include <tr1/functional>
#include <list>
using namespace std;

class Thing { public: int x; };

class Controller
{
public:
    void notify(Thing& t) { cerr << t.x << endl; }
};

class Notifier
{
public:
    Notifier(Controller* c) { _c = c; }
    void operator()(Thing& t) { _c->notify(t); }
private:
    Controller* _c;
};

int main()
{
    list<Thing> things;
    Controller c;

    // ... add some things ...
    Thing t;
    t.x = 1; things.push_back(t);
    t.x = 2; things.push_back(t);
    t.x = 3; things.push_back(t);

    // This doesn't work:
    //for_each(things.begin(), things.end(),
    //         tr1::mem_fn(&Controller::notify));

    for_each(things.begin(), things.end(), Notifier(&c));
    return 0;
}

My question is: can I get rid of the Notifier class by using some version of the "This doesn't work" line? Seems like I should be able to make something work, but can't quite get the right combination. (I've fumbled around with a number of different combinations.)

Without using boost? (I would if I could.) I'm using g++ 4.1.2, yes I know it is old...

+4  A: 

You can accomplish this using bind, which is originally from Boost but is included in TR1 and C++0x:

using std::tr1::placeholders::_1;
std::for_each(things.begin(), things.end(),
              std::tr1::bind(&Controller::notify, c, _1));
James McNellis
Thanks James, this is exactly what I was looking for.
bstpierre
+3  A: 

What about going old-school:

for(list<Thing>::iterator i = things.begin(); i != things.end(); i++)
  c.notify(*i);
sje397
Because that's far too obvious? :) Honestly, this is for learning, I'm trying to grok the new-school way of doing it.
bstpierre