On Mon, 17 Nov 2025 at 19:08, François Dumont <[email protected]> wrote: > > libstdc++: [_GLIBCXX_DEBUG] Fix std::erase_if for std::vector behavior > > When using directly __gnu_debug::vector the std::erase_if is called > with a > reference to the std::vector base class and so is missing the > invalidation > of the iterators that might result from the erase.
When you first described this problem, I thought your explanation was correct. But now I'm looking at the specification of std::erase_if and I think the existing code is correct, and this change would be wrong. The standard is very clear about the implementation of std::erase_if, it is precisely what we do: call remove_if and then call c.erase(it, c.end()). That means it does not invalidate all iterators from the first erasure, it only invalidates iterators to the elements removed from the end. I think this patch is wrong, and I think the erase_if in your __gnu_debug::inplace_vector patch is also wrong. > > To fix this provide a std::erase_if overload dedicated to > __gnu_debug::vector. > Doing so we can cleanup the implementation dedicated to std::vector > from any > _GLIBCXX_DEBUG consideration. > > libstdc++-v3/ChangeLog: > > * include/debug/vector (std::erase_if, std::erase): New > overloads for > __gnu_debug::vector instances. > * include/std/vector (std::erase_if, std::erase): Make > overloads specific > to normal std::vector implementation. > * testsuite/23_containers/vector/debug/erase.cc: New test case. > * > testsuite/23_containers/vector/debug/invalidation/erase.cc: New test case. > > Tested under Linux x86_64, > > ok to commit ? > > François
