views:

188

answers:

2

I have the following code that uses a for loop and I would like to use transform, or at least for_each instead, but I can't see how.

typedef std::list<boost::function<void(void) > CallbackList;
CallbackList callbacks_;

//...
for(OptionsMap::const_iterator itr = options.begin(); itr != options.end(); ++itr)
{
   callbacks_.push_back(boost::bind(&ClassOutput::write_option_,this,*itr));
}

Later on in the code, I actually want to call this collection of nullary function objects. I'm using a for loop here as well, and it seems like I should be able to use for_each somehow.

for(CallbackList::iterator itr = callbacks_.begin(); itr != callbacks_.end(); ++itr)
{    
  (*itr)();
}
+3  A: 

I managed to figure out part 2:

typedef boost::function<void(void)> NullaryFunc;
for_each(callbacks_.begin(),callbacks_.end(),boost::bind(&NullaryFunc::operator(),_1));
Dan Hook
+2  A: 

To do all this in a single transform call, you I think you need to call bind on itself, because you need a functor which calls boost:bind. That's something I've never attempted. Would you settle for something like this (untested)?

struct GetFunc {
    ClassOutput *obj;
    boost::function<void(void) > operator()(const OptionsMap::value_type &v) {
        return boost::bind(&ClassOutput::write_option_, obj, v);
    }
    GetFunc(ClassOutput *obj) : obj(obj) {}
};

transform(options.begin(), options.end(), back_inserter(callbacks_), GetFunc(this));

In C++0x you can use a lambda instead of the functor class:

transform(options.begin(), options.end(), back_inserter(callbacks_), 
    [this](const OptionsMap::value_type &v) {
        return boost::bind(&ClassOutput::write_option_, this, v);
    }
);
Steve Jessop
That works with one change. The argument of operator() should be OptionsMap::value_type. Thanks.
Dan Hook
Fixed it (15 chars).
Steve Jessop