https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112934

            Bug ID: 112934
           Summary: excessive code for std::map::erase(key)
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pobrn at protonmail dot com
  Target Milestone: ---

It is probably expected that calling erase(key) is equivalent to or better than

  auto it = m.find(k);
  if (it != m.end())
    m.erase(it);

However, currently that is not the case: https://gcc.godbolt.org/z/f1Mh3bodf

This is because std::map::erase(key) calls erase(key) on the underlying tree,
which then uses equal_range() and tries to delete a range of iterators. This is
unnecessary since a map enforces unique keys.

libc++ has an __erase_unique() method on the underlying tree type to handle
this, which does essentially what - I assume - most people expect erase(key) to
do:
https://github.com/llvm/llvm-project/blob/b88b480640f173582ffbfd2faae690f2bc895d14/libcxx/include/__tree#L2453

I believe the same applies to std::set.

Reply via email to