[libcxx-commits] [libcxx] [libc++] Avoid constructing additional objects when using map::at (PR #157866)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 17 02:00:31 PDT 2025


================
@@ -1085,6 +1094,30 @@ public:
   _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
 #  endif
 
+  template <class _Arg,
+            __enable_if_t<is_convertible<_Arg, value_type>::value &&
+                              __desugars_to_v<__less_tag, key_compare, key_type, key_type> &&
+                              __is_transparently_comparable_v<key_type, __remove_cvref_t<_Arg> >,
+                          int> = 0>
+  _LIBCPP_HIDE_FROM_ABI inline mapped_type& at(_Arg&& __arg) {
+    auto [_, __child] = __tree_.__find_equal(__arg);
+    if (__child == nullptr)
+      std::__throw_out_of_range("map::at:  key not found");
+    return static_cast<__node_pointer>(__child)->__get_value().second;
+  }
+
+  template <class _Arg,
+            __enable_if_t<is_convertible<_Arg, value_type>::value &&
+                              __desugars_to_v<__less_tag, key_compare, key_type, key_type> &&
+                              __is_transparently_comparable_v<key_type, __remove_cvref_t<_Arg> >,
+                          int> = 0>
+  _LIBCPP_HIDE_FROM_ABI inline const mapped_type& at(_Arg&& __arg) const {
----------------
philnik777 wrote:

Given the improvement vs. having const-correctness problems I don't think it's worth it.

https://github.com/llvm/llvm-project/pull/157866


More information about the libcxx-commits mailing list