
Darren Cook wrote:
Thanks for the reply. I can almost match that up to the parts in the while loop, except mention of "list" is missing. Should it be:
boost::bind(&Points::add, &list, boost::bind(&Info::key, boost::bind(&InfoContainer::get, &cs, _1))));
However, if Points is std::set<int>, then "add" should be "insert"
Sorry. Points is a class that can be thought of as a wrapper for std::set.
I
thought I could simplify the example by substituting, but of course I hadn't thought it through properly had I.
BTW, I was hoping I'd be able to do something like this: std::for_each(c->points.begin(),c->points.end(), list.add(cs.get( _1 )->key) );
But after some experiments with boost::lambda I see why this isn't possible. I think if just one call to bind is possible then it is worth it, but when bind is required 2 or more times, falling back to a while loop or a functor seems better. Is anyone else using a similar guideline?
Yes, this is a reasonable guideline, but you should also consider: 1. Adding InfoContainer::get_key as an alias for get(_1)->key 2. Using std::transform with a std::inserter, as Doug already mentioned. An overcomplicated bind expression sometimes indicates that you're using the wrong algorithm. Since Points is not an STL container, std::inserter won't work but one reasonable alternative would be template<class It, class F, class L> void transform_and_add(It first, It last, F f, L & list) { for(; first != last; ++first) list.add( f(*first) ); } transform_and_add(c->points.begin(), c->points.end(), bind(&InfoContainer::get_key, _1), list);