Save the following as foo.C, then g++ foo.C -o foo
Running foo blows the assertion. g++ -v produces: Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../gcc-4.0.1/configure Thread model: posix gcc version 4.0.1 Uncommenting the print statements shows that my_list.begin() has not been updated by the call to my_list.erase(), even though the first (only) element of this list has just been deleted. //---------------------------------------------------------------------------- #include <list> #include <iostream> #include <cassert> using namespace std; main() { list<int> my_list; my_list.push_front(1); list<int> my_other_list; my_other_list.push_front(2); //cerr << "Lists built \n"; //cerr << "my_list.end() = " << ((void *) (&*my_list.end())) << "\n"; //cerr << "my_list.begin() = " << ((void *) (&*my_list.begin())) << "\n"; //cerr << "my_other_list.end() = " << ((void *) (&*my_other_list.end())) << "\n"; //cerr << "my_other_list.begin() = " << ((void *) (&*my_other_list.begin())) << "\n"; swap(my_list,my_other_list); //cerr << "After swap \n"; //cerr << "my_list.end() = " << ((void *) (&*my_list.end())) << "\n"; //cerr << "my_list.begin() = " << ((void *) (&*my_list.begin())) << "\n"; //cerr << "my_other_list.end() = " << ((void *) (&*my_other_list.end())) << "\n"; //cerr << "my_other_list.begin() = " << ((void *) (&*my_other_list.begin())) << "\n"; list<int>::iterator it = my_list.begin(); while (it != my_list.end()) { //cerr << "erasing: " << ((void *)(&*it)) << "\n"; it = my_list.erase(it); //cerr << "my_list.end() = " << ((void *) (&*my_list.end())) << "\n"; //cerr << "my_list.begin() = " << ((void *) (&*my_list.begin())) << "\n"; } //cerr << "At end \n"; //cerr << "my_list.end() = " << ((void *) (&*my_list.end())) << "\n"; //cerr << "my_list.begin() = " << ((void *) (&*my_list.begin())) << "\n"; //cerr << "my_other_list.end() = " << ((void *) (&*my_other_list.end())) << "\n"; //cerr << "my_other_list.begin() = " << ((void *) (&*my_other_list.begin())) << "\n"; assert(my_list.begin() == my_list.end()); } -- Summary: std::swap() followed by list::erase() produces incorrect list::begin() Product: gcc Version: 4.0.1 Status: UNCONFIRMED Severity: critical Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: Simon dot Finn at reify dot co dot uk CC: gcc-bugs at gcc dot gnu dot org GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22591