Search code examples
c++stlboost-bindboost-function

STL algorithms on containers of boost::function objects


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)();
}

Solution

  • 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);
        }
    );