[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
Fri Sep 15 13:00:34 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:

Ok, so actually it turns out that MSVC does the same thing here and they went through the exact same questioning as us, see https://github.com/microsoft/STL/pull/1898#discussion_r625118642.

However, I [played around](https://godbolt.org/z/fMM7nrn74) and I wasn't able to craft a case where passing an lvalue would fail (despite what @timsong-cpp said in that review). Unless we are able to come up with one, I think it would be reasonable to pass a lvalue.

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


More information about the flang-commits mailing list