I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not
sure if this is correct behavior or not, and I'm using the gcc STL and
compiler.
When calling vector<int>::push_back(0), an iterator that I've set in a
loop gets changed. Here's an example of the problem (sorry about the
lack of indentation, posting this from Google):
#include <vector>
#include <iostream>
using namespace std;
typedef vector<int> T;
typedef T::iterator I;
T foo;
foo.push_back(1);
for (I i = foo.begin(); i != foo.end(); ++i) {
// Output of this is 0.
cout << (i - foo.begin());
foo.push_back(0);
// Output of this is -8.
cout << (i - foo.begin());
}
Now, I know that foo.end() will change because of the push_back and
this would create an infinite loop, but that's not an issue in my code,
I've just left out the irrelevant parts of the loop. I've put those
cout()s directly around the push_back(), and that's what I got. Should
an iterator be completely invalidated by a push_back? If so, is there a
way to re-align the iterator without something like "tmp = i -
foo.begin(); foo.push_back(0); i = foo.begin() + tmp;"? If not, and
this is a bug, how do I go about reporting this problem to the GCC
devs?
Thanks for your time and help!