-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Fix complexity guarantee in ranges::clamp #68413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
24d5794
1ba671d
c7bb32e
6b79d8b
f656911
18233d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would much rather we simply pass the projection down to another function, which ensures the lifetime of the projection, even in weird cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand. We're extending the lifetime here with rvalue references, is that what you would want to make more explicit? FWIW, this is how other implementations do it too, and lifetime extending with rvalue references is a fairly common pattern, so if that's what your concern is about my preference would be to keep it simple. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, so if a user passes a projection which happens to implicitly create the projected object in the call to the projection, For example:
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, so you are thinking about the following case?
Here what you're saying is that the That seems to be correct according to the lifetime extension docs on cppreference:
I think the "problem" I have with that is that it means we could technically never call any user-provided function (projection or else) and hold on to the result of that, since this case could happen anywhere. Every time we call a user-provided function with some arguments, there could be some implicit conversion happening and then we are vulnerable to this dangling reference issue. Am I right? If so, and if we want to guard against this, then we would in theory write all of our code which is potentially vulnerable to this issue in a single full expression. I'm not sure this is either reasonable or expected. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is always dangling no matter how you do it. This isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhhh, yes, indeed. So then I don't see a way that we can make this "work" even if we wanted to. |
||
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))) | ||
return __high; | ||
else | ||
return __value; | ||
|
Uh oh!
There was an error while loading. Please reload this page.