Search code examples
c++stliteratorinvalidation

std::list<>::splice invalidates iterators. Rationale?


I wonder what is the rationale behind making std::list<>::splice to invalidate the iterators that refer to the subsequence being spliced into the new container. This looks kinda illogical to me, especially in light of standard std::container::swap specification. According to the language standard std::container::swap does not invalidate any iterators. This is a perfectly reasonable practical specification. However, I'd say that std::list<>::splice would also benefit greatly from iterator-preserving behavior.

I understand that there might be some purely academic considerations based on the concepts of iterator reachability, etc. But at the same time splice is a std::list-specific operation, meaning that providing a custom-tailored specification for it probably would not make a serious conceptual damage to the STL design in general.

So what was it? Would it outlaw or overcomplicate some practical implementations of std::list, which I fail to recognize?


Solution

  • In C++11 splice does not invalidate the iterators, but make them refer to the appropriate elements in the *this container. This is all described in 23.3.5.5.