Question
Is a back_insert_iterator valid for the lifetime of the container?
I think I know the answer to this, but I'd appreciate a sanity check.
Does iterator invalidation apply to std::back_insert_iterators?
#include <cassert>
#include <iterator>
#include <vector>
int main() {
auto v = std::vector<int>{ 0, 1, 2 };
auto iter = std::back_inserter(v);
*iter++ = 3;
v.clear(); // invalidates iterators, but
*iter++ = 4; // back_insert_iterator is special?
assert(v.size() == 1 && v[0] == 4);
return 0;
}
This code works for me because the std::back_insert_iterator implementation from my vendor doesn't hold an iterator (or pointer or reference). It simply calls the container's push_back method.
But does the standard require that implementation? Could another vendor's back_insert_iterator hold and maintain a one-past-the-end iterator to use with a call to the container's insert method? It seems that would meet the requirements. This difference, of course, is that it would be vulnerable to invalidation.
I know cppreference.com is not authoritative, but it's more accessible than the standard.
[A vector's clear method] [i]nvalidates any ... iterators referring to contained elements. Any past-the-end iterators are also invalidated. [cppreference.com, emphasis added]
A std::back_insert_iterator could be the poster child of a past-the-end iterator.