Clarify state of OnceCallback after move. |
||
Issue description
This came up in a code review.
I couldn't find documentation on the state of a OnceCallback after moving or running it. Is it true that after being moved from or Run()ed, we can depend on the callback being null? Put another way, is
void f(OnceCallback<void()> cb) {
OnceCallback<void()> cb2 = std::move(cb); // or std::move(cb).Run();
CHECK(!cb);
}
guaranteed to not check? If yes, this would be nice to document.
,
Feb 27 2017
There's really no convention to not specify the state after moving, see e.g. unique_ptr, but I'll go ahead and use ResetAndReturn when I need it to be cleared.
,
May 19 2017
WontFix per discussion at https://groups.google.com/a/chromium.org/forum/#!topic/cxx/5JWdjOKuqh8. |
||
►
Sign in to add a comment |
||
Comment 1 by tzik@chromium.org
, Feb 27 2017For the current implementation of OnceCallback, that's true, |cb| is cleared on the assignment. And we likely keep the behavior. However, in general C++ convention, the moved-out object is in an unspecified state. Confusingly, similar code doesn't clear the callback object on RepeatingCallback case: void f(const RepeatingCallback<void()>& cb) { RepeatingCallback<void()> cb2 = std::move(cb); CHECK(cb); // Non-null. We can't modify |cb| in |cb2| construction. std::move(cb2).Run(); CHECK(cb2); // Non-null. We don't have Run() for rvalue-qualified callback. } I think it's hard to see at a glance, so I'd recommend using ResetAndReturn.