[libcxx-commits] [libcxx] [libc++] Fix complexity guarantee in std::clamp (PR #68413)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 23 12:00:42 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);
----------------
EricWF wrote:

OK, so if a user passes a projection which happens to implicitly create the projected object in the call to the projection, For example:

```
std::string const& project_string(std::string const&); // pass "abc"
```

We currently do accept code that does this, because the returned object is never used outside of the expression which created it. We can keep this behavior simply by creating a `__clamp_impl` function that takes the projected values as argument and then returns the result. Ex `return __clamp_impl(  __low,  __high, _Comp __comp, std::invoke(__proj, __value), std::invoke(__proj, __low), std::invoke(__proj, __high)); // now the projections live the entire time`

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


More information about the libcxx-commits mailing list