From my understanding of STL, list iterators are supposed to remain valid after a splice operation, however I seem to be getting a problem when I use an iterator to splice one object from list A to list B. The iterator seems to have been invalidated by the STL implementation. If I then try to use the same iterator to splice the object back from list B to list A, "bad things" happen (assertion fail on a debug build, worse on a release build). Here is a simple test case:
This code builds and appears to run fine using GCC, but not with Visual Studio 2008 (the second splice operation fails an internal assertion). Am I missing something fundamental here or is this a problem with VC++?
I suspect you are correct, at least from what I can tell tracing through VC++'s STL list source. The problem is relatively easily fixed/worked around by reassigning the iterator to list2.end() and decrementing it after the first splice, but one line of code is usually preferable to three
Bookmarks