c++boostboost-multi-index

Pointer/reference stability in boost::multi_index


https://www.boost.org/doc/libs/1_81_0/libs/multi_index/doc/tutorial/indices.html#guarantees discusses iterator stability on rehash. However, this doesn't necessarily imply pointer and reference stability. Which operations on boost::multi_index, if any, invalidate pointers and references?


Solution

  • For all the indices of Boost.MultiIndex, iterators, pointers and references to an element remain valid as long as the element is not erased. In the case of hashed indices, this is in contrast with the guarantees provided by std::unordered_map and other standard C++ unordered associative containers, where iterator stability is not maintained upon rehashing --to restate it, if you have an iterator/pointer/reference to an element in a hashed index and then rehash the index, the iterator/pointer/reference will remain valid after that.