简体   繁体   中英

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

I managed to figure out part 2:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM