Question: I have the following two classes that exhibit the Command pattern. (Unfortunately, both have a unique method name.)

```
//pseudocode 
class Subscriber {
    virtual void receive(const Event&) = 0;
}

class Dispatcher {
    virtual void dispatch(const Event&) = 0;
}

```

I have a class template that has a list of some type with a method to iterate over this list.

```
//pseudocode
template<typename T>
class Registry {
    typedef list<T> ObjectList;
    ObjectList _objects;
    void iterate(const Event& event) {
        for_each(_objects.begin(), _objects.end(), ...);  //not sure what to do here
    }
}

```

I would like to use `mem_fun` to create a Functor that calls `receive` or `dispatch` as appropriate. I'm able to create a simple use case where I simply invoke a method without any params. I.e.

```
//pseudocode
class Simple {
    void simple() {/*...*/}
}

```

and then I iterate:

```
for_each(_objects.begin(), _objects.end(), mem_fun(&Simple::simple);

```

Unfortunately, I have no idea how to get the `event` param passed to mem\_fun. Looking at the headers, it does appear that I can pass a single param, but I'm not well versed in C++ to understand what I need to do.

Ultimately, I would like to make the `iterate` method accept a type of functor so it will fire that method on every method in the list.

I would prefer to avoid Boost...I think this is entirely possible without dragging this framework into the mix.

Thanks!

Answer: 
This may be the most straight-forward way:

```
class event_forwarder // make private in Registry
{
public:
    event_forwarder(const Event& event) :
    mEvent(event)
    {}

    void operator()(Subscriber& subcriber) const
    {
        subscriber.receive(mEvent);
    }

    void operator()(Dispatcher& dispatcher) const
    {
        dispatcher.dispatch(mEvent);
    }

private:
    const Event& mEvent;
};

```

Then:

```
for_each(_objects.begin(), _objects.end(), event_forwarder(event));

```
If I understand correctly, what you want is `std::bind2nd`:

```
std::for_each(_objects.begin(), _objects.end(), 
              std::bind2nd(std::mem_fun_ref(&Subscriber::receive), event));

```

The member-function `Subscriber::receive` has two parameters. The first is the implicit `this` pointer, and the second the `const Event &`. `std::bind2nd`, given a function `f` taking two arguments, returns a function `f_1` taking one argument, that invokes the original function `f` with a fixed value for the second argument.

**Edit:**

To handle the different names of the dispatch functions, you can make the dispatch function a template parameter:

```
//pseudocode
template<typename T, void (T::*dispatch_method)(Event)>
class Registry {
    typedef list<T> ObjectList;
    ObjectList _objects;
    void iterate(const Event& event) {
        std::for_each(_objects.begin(), _objects.end(), 
                      std::bind2nd(std::mem_fun_ref(dispatch_method), event));
    }
}

```

**Unfortunately, there does not seem to be a way to make `bind2nd` handle `const` reference parameters, so my whole solution is moot, unless copying `Event` objects is fine with you.** This will work in C++0x with `std::bind` though, and the idea of making the dispatch function a template parameter is still valid. You can even use traits, to make that mechanism even more flexible.