jeudi 28 juillet 2016

std::accumulate BinaryOperator side effects

std::accumulate documentation on cppreference.com states that:

op must not invalidate any iterators, including the end iterators, or modify any elements of the range involved (since c++11)

Later, it is shown a possibile implementation, that I report here:

template<class InputIt, class T, class BinaryOperation>
T accumulate(InputIt first, InputIt last, T init, 
             BinaryOperation op)
{
    for (; first != last; ++first) {
        init = op(init, *first);
    }
    return init;
}

How op could "invalidate some iterators" or "modify elements of the range", assuming this implementation of std::accumulate ?

Aucun commentaire:

Enregistrer un commentaire