New issue
Advanced search Search tips

Issue 694945 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner:
Closed: May 2017
EstimatedDays: 0
NextAction: ----
OS: ----
Pri: 3
Type: Task



Sign in to add a comment

Clarify state of OnceCallback after move.

Project Member Reported by maxmorin@chromium.org, Feb 22 2017

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.
 

Comment 1 by tzik@chromium.org, Feb 27 2017

For 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.
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.
Status: WontFix (was: Untriaged)
WontFix per discussion at https://groups.google.com/a/chromium.org/forum/#!topic/cxx/5JWdjOKuqh8.

Sign in to add a comment