GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++ Performance Win

libstdc++: Use emplace_hint instead of emplace in ranges::to.

ranges::to uses emplace_hint for associative containers, improving efficiency.

The ranges::to function in libstdc++ now uses emplace_hint instead of emplace when constructing associative containers. This change implements LWG4121 and allows for more efficient construction by providing an iterator hint, which can avoid unnecessary element copying or moving.

In Details

The ranges::to utility in <ranges> has been updated to use emplace_hint(iterator, value) over emplace(iterator, value) for constructing associative containers. This addresses LWG4121, ensuring that associative containers which support emplace_hint are constructed more efficiently by leveraging insertion hints, especially when from_range constructors are not applicable or when emulating them.

For Context
ranges::to
A utility in C++20's <ranges> library that converts a range into a container. It can construct various standard containers, including associative ones like std::map and std::set.
emplace_hint
A member function of C++ associative containers (like std::map, std::set) that inserts an element into the container, providing an iterator as a hint for the insertion position. This can improve performance by helping the container find the correct location more quickly.
emplace
A member function of C++ containers that constructs an element in-place within the container. For associative containers, emplace(iterator, key, value) (or similar overloads) inserts an element, potentially using the iterator as a hint.
LWG4121
A specific issue raised and resolved by the Library Working Group (LWG) of the C++ Standards Committee, concerning the construction of associative containers via ranges::to.
Filed Under: libstdc++cpp20ranges