Boost logo

Boost :

From: Giovanni Bajo (giovannibajo_at_[hidden])
Date: 2003-05-14 22:06:31


Terence Wilson <tez_at_[hidden]> wrote:

> I could use some help with bind syntax. I have a container:
>
> Class MyClass
> {
> public:
> void foo(void);
> }
>
> Std::map<int, boost::shared_ptr<MyClass> >
>
> I would like to construct a for_each loop with calls to foo for every
> pointer to MyClass in the map. Help greatly appreciated.

typedef std::map<int, boost::shared_ptr<MyClass> > my_map;
typedef my_map::value_type my_map_pair;

my_map m;

std::for_each(m.begin(), m.end(),
    boost::bind(&MyClass::foo, boost::bind(&my_map_pair::second, _1)));

The inner bind extracts the field "second" from the iterator. The outer bind
calls the method. You can call a method from either an instance of the
class, a dumb pointer to an instance or a smart pointer to an instance, it
will just work.

Giovanni Bajo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk