[clang-tools-extra] [libc++] Fix complexity guarantee in std::clamp (PR #68413)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 26 22:14:54 PDT 2023


================
@@ -37,9 +37,10 @@ struct __fn {
     _LIBCPP_ASSERT_UNCATEGORIZED(!bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
                                  "Bad bounds passed to std::ranges::clamp");
 
-    if (std::invoke(__comp, std::invoke(__proj, __value), std::invoke(__proj, __low)))
+    auto&& __projected = std::invoke(__proj, __value);
----------------
timsong-cpp wrote:

This is always dangling no matter how you do it. This isn't `__proj(__something)`, it's `std::invoke(__proj, __something)`. No temporary is created for the call to `std::invoke` itself, that just binds a bunch of references. The full-expression with the temporary is somewhere inside the `std::invoke` machinery, and the temporary is destroyed by the time `std::invoke` returns.

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


More information about the cfe-commits mailing list