mercredi 13 mai 2015

working with c++11 lambda codes

C++ with its new features seems to be a new language for those who write old fashion codes. Consider this function

template<typename Type>
void Sequitur<Type>::printList(const Symbol * list, unsigned int number) const 
{
    list->forUntil([&number, this](const Symbol * item) {
        if(typeid(*item) == ValueType) {
          fout << static_cast<const Value*>(item)->getValue() << " ";
        }
        if(!--number) 
          return false;
        else 
          return true;
    });
}

This inline function is defined in a so called tpp file

template<typename Child>
template<typename Function> const Child * BaseList<Child>::forUntil(const Function & f) const
{
  const BaseList * item = this;
  const BaseList * next;
  while(item) {
    next = item->next_ptr;
    if(!f(static_cast<const Child*>(item))) 
      break;
    item = next;
  }
  return static_cast<const Child*>(item);
}

Assume everything is defined since the code is working. What I want to do is to add a counter to count how many time the while is executed. The counter value should be available at the end of printList().

How can I accomplish that?

Aucun commentaire:

Enregistrer un commentaire