GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++

libstdc++: Fix unordered_map heterogeneous insertion copying [PR126800].

Corrected `unordered_map` heterogeneous insertion to avoid unnecessary key copying when no insertion occurs.

A bug in libstdc++‘s unordered_map heterogeneous insertion has been fixed, preventing unnecessary copying of the key when an insertion does not actually happen. The issue involved try_emplace calling an incorrect helper function for new usage cases, which went undetected by tests. This patch corrects try_emplace and other heterogeneous insertion methods (at, insert_or_replace) to call the right helpers and ensures tests verify that keys are not copied when elements are not inserted.

In Details

The try_emplace member function in std::unordered_map (via bits/hashtable.h) previously invoked an incorrect helper function for heterogeneous insertion when the key already existed. This led to unnecessary key copying, which was not caught by existing tests. This commit corrects try_emplace to use the appropriate helper and extends test coverage to verify that key arguments are not copied for operator[], at, try_emplace, and insert_or_replace when no insertion occurs, addressing PR libstdc++/126800.

For Context
heterogeneous insertion
In C++ containers like unordered_map, this refers to insertion operations where the type of the key being inserted (e.g., std::string) may differ from the map's key type (e.g., std::string_view), allowing for more flexible lookups and insertions.
unordered_map
A C++ Standard Library associative container that stores elements formed by combining a key and a mapped value. Elements are not ordered. It provides average worst-case time complexity for insertion and access.
try_emplace
A C++ unordered_map member function that constructs an element in-place if the key does not exist, avoiding unnecessary copies or moves of the key and value objects.
PR126800
A Public Relations or Problem Report number, used here to refer to a specific bug report filed against libstdc++.
Filed Under: libstdc++bugfixunordered_map