[libcxx-dev] List begin iterator invalidation
Alexander Torstling via libcxx-dev
libcxx-dev at lists.llvm.org
Tue Feb 4 11:31:46 PST 2020
Hello!
I've stumbled upon a peculiarity with lists which I haven't been able to
explain so far.
Consider the following code:
#include <list>
#include <cstdio>
int main() {
std::list<int> l;
std::list<int>::iterator it = l.begin();
l.push_back(0);
l.insert(it, 1);
for(const int &i: l) {
printf("%d", i);
}
}
This will print "01". I fail to understand why the 1 is inserted at the
end. I would expect that the begin iterator would stay valid across the
`push_back`. For the record, g++ seems to have the same behavior.
I've already created a stack overflow question around this (
https://stackoverflow.com/questions/59974821/validity-of-list-begin-iterator-after-insertion)
and asked in the llvm IRC channel about it. So far without having an
explanation. So now I'm turning to you.
Thank you in advance
Alexander Torstling
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20200204/f861e408/attachment.html>
More information about the libcxx-dev
mailing list