tags:

views:

213

answers:

4

for_each accepts InputIterators :

//from c++ standard
template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function f);

is it ok to change the object in Function f, like this :

struct AddOne
{
    void operator()(int & x){x = x + 1;}
};

std::vector<int> vec(10);
std::for_each(vec.begin(),vec.end(),AddOne());

This code works in VC++2008 and also with GCC, but is it also portable (legal) code ?
(InputIterators are only guaranteed to be usable as rvalue, in this case they are used as lvalue in AddOne's operator())

+5  A: 

Read this article.

To be pedantic: for_each is a non-modifying sequence operation. The intent is not to modify the sequence. However, it is okay to modify the input sequence when using for_each.

dirkgently
+2  A: 

It is legal - the input iterators are used to specify the range, not to do the processing.

Interestingly, Josuttis in his book "The C++ Standard Library" lists for_each as modifying, raher than non-modifying.

anon
+3  A: 

You misunderstand something. Saying input iterators are only guaranteed to be usable as rvalues doesn't mean you can't get an lvalue out of an iterator somehow. So it does not mean that the result of *iterator is an rvalue. What you/for_each passes to AddOne is the result of operator* - not the iterator itself.

About for_each and a modifying function object - read this question

Johannes Schaub - litb
+1  A: 

If in doubt use std::transform as it will convey even to the casual reader of your code that you intend to modify something.

lothar