So we know that foreach is something like :
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
I have implemented a
template <typename T>
class Range
The problem is that when I use this function with the for_Each :
static void add1(float &v)
{
++v;
}
it go in infinite loop because of first “!=” last (it’s not first”<“last), so how people do when they implemente their own forward iterator to work with for_each ?
The problem of your approach is that your iterator increment operators do not change the iterator, but rather the stored value. This means that inside the
for_eachloop, the condition is modified both in the increment operator of the iterator and also through the function.