On Thursday 28 June 2007 05:14, Erik Johansson wrote:
> Without using boost, it can sometimes be cumbersome to create the
> functors that you need, instead of using a simple loop. But
> boost::lambda looks like it might be the answer to all my prayers, so
> I'm willing to give it a try.

Have you taken a look at boost::bind? It really makes things simple. I haven't 
found an example like this, but it works nicely:

// inside a long function
for (iter = users.begin().....)
{
  (*iter)->calcFunction();
  if ((*iter)->verifyFunction)
  {
     delete (*iter);
     users.erase(iter);
     break;
  }

  (*iter)->doMoreWork();
}


And then you can add a new function to the class..

void theClass::doWork(ICQUser *&user) // <-- pointer reference is necessary 
here
{
  user->calcFunction()
  if (user->verifyFunction())
  {
     delete user;
     user = 0;
     return;
  }

  user->doMoreWork();
}

Then in the original code...

// Do the task with the idea of a function having one main purpose
std::for_each(users.begin(), users.end(), boost::bind(&theClass::doWork, this, 
_1));
// Remove any deleted iterators
users.erase(std::remove(users.begin(), users.end(), static_cast<ICQUser 
*>(0)), users.end());

There is also the BOOST_FOREACH, but it doesn't promote modular code. In fact 
it does the opposite, so I'm not really that interested in using it.

Jon

-- 
________________________________________________________
Jon Keating                ICQ: 16325723
[EMAIL PROTECTED]               MSN: [EMAIL PROTECTED]
http://www.licq.org        GPG: 2290A71F
http://www.thejon.org      HOME: Minamiashigara, Japan

Reply via email to