-
Notifications
You must be signed in to change notification settings - Fork 168
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
feat: Enable Delegates to conveniently use stateful lambdas #1410
feat: Enable Delegates to conveniently use stateful lambdas #1410
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1410 +/- ##
==========================================
+ Coverage 47.76% 47.79% +0.02%
==========================================
Files 380 380
Lines 20164 20173 +9
Branches 9387 9387
==========================================
+ Hits 9632 9641 +9
Misses 4083 4083
Partials 6449 6449
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this just explicitly tries to grab the operator()
function pointer for a lambda? Should be ok I think!
@paulgessinger is this still waiting for the sentinel backlog? |
Still working on fixing that, sorry. I'll override the merge here. |
This PR adds functionality to the `Acts::Delegate`, that enables to construct, assign and connect stateful lambdas like this: ```c++ int n = 0; auto lambda = [&](){ ++n; }; Acts::Delegate<void()> d(lambda); d = lambda; d.connect(lambda); ``` The use of temporary stateful lambdas is forbidden due to deleted overloads with `Callable &&`: ```c++ Acts::Delegate<void()> d([&](){ ++n; }); // compile error ``` I havn't checked yet if this can simplify code somewhere in Core or Examples, but I ran in situations myself where this would be useful. (cherry picked from commit f673126)
…1410 to develop/v19.x] (#1414) Backport f673126 from #1410. --- This PR adds functionality to the `Acts::Delegate`, that enables to construct, assign and connect stateful lambdas like this: ```c++ int n = 0; auto lambda = [&](){ ++n; }; Acts::Delegate<void()> d(lambda); d = lambda; d.connect(lambda); ``` The use of temporary stateful lambdas is forbidden due to deleted overloads with `Callable &&`: ```c++ Acts::Delegate<void()> d([&](){ ++n; }); // compile error ``` I havn't checked yet if this can simplify code somewhere in Core or Examples, but I ran in situations myself where this would be useful.
Can we document the additional functionality in this PR in the comments at the top. I saw the limitations listed there and used a complicated workaround, when I could have just used |
Thinking about this again, I'm wondering if we should make this more explicit somehow, with a tag type to clearly mark what's going on. I've seen some cases where this was (ab)used and it can be a bit of a footgun. We use the lvalue-ness to imply it's owned somewhere... but that's not clear to the user at all. Thoughts? |
ping @benjaminhuth |
I assume the issue is that the delegate does not own the lambda - or is there something else I might be missing? My use has the lambda in the same scope as the extensions (and hence delegate), so this is good. It is made clear in the comments at the top of |
@paulgessinger I'm not 100% sure what you mean by "make it more explicit"? Like So to me it seems like a good proposal to improve the documentation to showcase the construction/connection with stateful lambdas... |
I've seen it being used to set a lambda and then not being aware that it immediately dangles when the lambda itself goes out of scope. This can happen as well of course with member functions but it's less likely to do by mistake I'd say. |
This PR adds functionality to the
Acts::Delegate
, that enables to construct, assign and connect stateful lambdas like this:The use of temporary stateful lambdas is forbidden due to deleted overloads with
Callable &&
:I havn't checked yet if this can simplify code somewhere in Core or Examples, but I ran in situations myself where this would be useful.