tags:

views:

105

answers:

1

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.

+1  A: 

This question is a duplicate.

wilhelmtell
Thank you for finding the question I wanted!
rlbond