Duplicate: http://stackoverflow.com/questions/717509/is-it-ok-to-mutate-objects-with-stdforeach
This is based on an earlier question which I can't seem to find anymore on stack overflow. Basically, is something like this legal?
struct doSomething
{
void operator()(int& i) {++i;}
};
int main()
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
std::for_each(vec.begin(), vec.end(), doSomething());
}
I ask this because for_each is described as a non-modifying algorithm in the standard. Also, it is mentioned here.