There hasn't been such issue with std::map
at least since g++ 4.5:
// stripped comments and requirements
mapped_type&
operator[](const key_type& __k)
{
iterator __i = lower_bound(__k);
if (__i == end() || key_comp()(__k, (*__i).first))
__i = insert(__i, value_type(__k, mapped_type()));
return (*__i).second;
}
The snippet you posted isn't from std::map
, but from hash_map
, which was a GCC extension to the library:
00052 /** @file backward/hash_map
00053 * This file is a GNU extension to the Standard C++ Library (possibly
00054 * containing extensions from the HP/SGI STL subset).
00055 */
Since it's an extension the maintainers are not really obligated to follow any complexity/performance rules (even though your proposed function would be faster). Note that hash_map
has been replaced by an implementation for std::unordered_map
, which doesn't use the constructor if the element exists.