[flang-commits] [flang] [libc++] Prevent calling the projection more than three times (PR #66315)

Louis Dionne via flang-commits flang-commits at lists.llvm.org
Mon Sep 18 07:17:02 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);
+    if (std::invoke(__comp, std::forward<decltype(__projected)>(__projected), std::invoke(__proj, __low)))
       return __low;
-    else if (std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __value)))
+    else if (std::invoke(__comp, std::invoke(__proj, __high), std::forward<decltype(__projected)>(__projected))
----------------
ldionne wrote:

Ah, thanks a lot! Like I said in the other comment, @pandaninjas let's add a test for that and go back to `std::forward`. Then I think this patch will be good to go.

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


More information about the flang-commits mailing list