Say I have a simple program as so:
int main(void) { std::list<int> l; auto it = l.begin(); auto it2 = l.insert(l.begin(), 5); std::cout << (it == it2) << std::endl; } Doesn't this show that the iterator it has been invalidated by inserting into the list. Yet the C++ standard says that insertion into a list does not invalidate iterators.
Originally it would probably hold a nullptr since the list was empty. Now it no longer points to any iterator part of the list. So is it not invalidated?
auto it = l.begin();returnsstd::list::end()as the container is currently empty. You can never have a item inserted at this position; so the test will always fail.itremains a valid iterator.(it == l.end())instead, the program outputs 1.it == l.end(), whileit2will point to the inserted item. So the result you're getting is what would be expected. No iterators are invalidated here. "Doesn't this show that the iterator it has been invalidated by inserting into the list." No, it does not show that.itwould probably hold a nullptr since the list was empty." Probably not. But it doesn't matter, so long as the requirements for iterators are satisfied.