Repository navigation
Conversation
|
inspired by #71130 (comment) |
This comment was marked as outdated.
This comment was marked as outdated.
|
Should we just disable the move and copy constructors on this? We shouldn't be doing that with scope guards on stack I don't think (but I'm on mobile right now, so maybe we use scope_guard more flexibly than that). |
The defaulted move constructor left the moved-from guard armed, so a guard that was moved ran its action twice, once from each object. The defaulted move assignment threw away the action the target was holding, and the moved-from guard still ran later. It only compiled for callables that can be assigned, which rules out any lambda with captures. No caller moves or assigns a guard, and make_scope_guard() doesn't need to move one either: since C++17, both its return statement and "auto g = make_scope_guard(...)" construct the guard in place. So instead of fixing the move constructor, drop it. Deleting the copy constructor and copy assignment also suppresses the implicit move operations, so a guard can be neither copied nor moved. make_scope_guard() didn't compile when given an lvalue: with F = T&, the const F& and F&& constructors end up with the same signature. Decay F, so the guard keeps its own copy of the callable. While at it, mark the constructors [[nodiscard]], and drop the in_place_type overload of make_scope_guard(), which nobody calls. Signed-off-by: Kefu Chai <[email protected]>
scope_guard had no tests at all. Cover the fixes from the previous
commit: the guard can be neither copied nor moved, and
make_scope_guard() accepts an lvalue and copies it.
The other tests make sure the simpler implementation still handles
move-only lambdas, "scope_guard g{...}" without template arguments,
and std::in_place construction.
Signed-off-by: Kefu Chai <[email protected]>
A guard is often used for rollback: it should undo a change if the function bails out early, and do nothing once the function gets to the end. Without a way to turn the guard off, the caller has to keep a bool next to the guard and test it in the lambda. ceph#71130 does exactly that to roll back a half-created pool in OSDMonitor::prepare_new_pool(). Add an "active" flag, and dismiss() to clear it. Put the callable under [[no_unique_address]], so a guard holding a captureless lambda is no bigger than the flag. Signed-off-by: Kefu Chai <[email protected]>
9652a89 to
5523698
Compare
then we can no longer return a named guard from a function.. but yeah, i think you are right. we are not likely to do this as in general, the scope_guard tackles the cleanup of (local) variables on stack. dropped. |
|
jenkins test make check arm64 |
1 similar comment
|
jenkins test make check arm64 |
gregsfortytwo
left a comment
There was a problem hiding this comment.
(Sorry I dropped getting back to you on this!)
I think this is okay, but honestly it's got more new C++ and wizardry than I'm familiar with. @adamemerson I think you keep up on such things; do you have a couple minutes to check it out?
|
I honestly would have made move call dismiss on the moved-from, but either works. |
|
@adamemerson hi Adam, thanks for your review and approval. yeah, the current implementation follows Greg's suggestion at #73241 (comment) . as we could bring the move ctor back when we need to return a named guard. |
|
This PR has been added to wip-bharath24-testing-20261114.234450-main. |
The defaulted move constructor left the moved-from guard armed, so a guard that was moved ran its action twice, once from each object. The defaulted move assignment threw away the action the target was holding, and the moved-from guard still ran later. It only compiled for callables that can be assigned, which rules out any lambda with
captures.
No caller moves or assigns a guard, and
make_scope_guard()doesn't need to move one either: since C++17, both its return statement and "auto g = make_scope_guard(...)" construct the guard in place. So instead of fixing the move constructor, drop it. Deleting the copy constructor and copy assignment also suppresses the implicit move operations, so a guard can be neither copied nor moved.make_scope_guard()didn't compile when given an lvalue: withF = T&, theconst F&andF&&constructors end up with the same signature. Decay F, so the guard keeps its own copy of the callable.While at it, mark the constructors
[[nodiscard]], and drop thein_place_typeoverload ofmake_scope_guard(), which nobody calls.