Ban absl::AnyInvocable?

871 views
Skip to first unread message

Darren Shen

unread,
Jan 17, 2024, 11:54:24 AMJan 17
to cxx
Hello,

I saw that absl::AnyInvocable is still TBD. There was a previous discussion around absl/base FunctionRef, which resulted in the creation of base::FunctionRef.

IIUC, absl::AnyInvocable is just a version of std::function can do moves and avoid some allocations. Given the lifetime concerns around std::function, it seems like absl::AnyInvocable should be banned too in favour of base::OnceCallback?

Thanks!

Peter Kasting

unread,
Jan 17, 2024, 3:33:34 PMJan 17
to Darren Shen, cxx
AnyInvocable overlaps with both Once and RepeatingCallback. It eliminates some of the problems with std::function that had originally led to the existence of Callback, but it doesn't support e.g. the stricter lifetime checks the base types do (e.g. requiring the `this` argument to either be reference-counted or be explicitly marked, using something like `Unretained()`).

What it does do, that Callbacks currently don't, is provide better support for member qualifiers -- e.g. invoking an rvalue-ref-qualified member function (and requiring the object it's invoked on to be an rvalue). I am slowly working on adding that kind of support to Callbacks, but it's a background task.

I don't see a path where we'd replace Callback with AnyInvocable (I'm not sure how we'd propose our lifetime stuff upstream to Abseil) and it seems undesirable for the two to exist in parallel when they're so closely overlapping. Therefore, I agree we should ban it.

PK
Reply all
Reply to author
Forward
0 new messages