mardi 2 octobre 2018

Transform hand written loop to std library call

I have recently watched the talk from Sean Parent about C++ seasoning from 2013. If I understood him right, he says that you can eliminate almost all (all?) hand written loops from your code. My question is how to achieve this? Let's consider the following code:

class ProgressDialog
{
  //interesting part of that class
  void SetPosition(int position);
  bool IsCancelRequested();
  void SetHeader(const std::string& status);
}
void foo( const std::vector<std::string>& v)
{
  ProgressDialog dlg;
  long position = 0;
  for( const auto& s : v)
  {
    ++position;
    dlg.SetPosition(position);
    dlg.SetHeader("Processing"+ s);
    DoSomethingThatTakesSomeTime(s);
    if(dlg.IsCancelRequested()) break;
  }
}

Is there a way to refactor away the hand written loop?

Aucun commentaire:

Enregistrer un commentaire