base::Bind + uncapturing C++11 lambda?

648 views
Skip to first unread message

Taiju Tsuiki

unread,
Jun 24, 2016, 12:01:12 AM6/24/16
to cxx, Ken Rockot, Dana Jansens, Daniel Cheng
Hi, C++ experts.
Can we allow binding uncapturing C++11 lambda with base::Bind into base::Callback?

I heard mojo::Callback was able to hold C++11 lambda, while base::Bind doesn't accept it directly.
Though I'm not sure we should allow general capturing lambda as a base::Bind argument, I think it's safe and useful to allow uncapturing ones.
I.e.:

int i = 42;

base::Closure cb1 = base::Bind([i](){});  // Reject this.

base::Closure cb2 = base::Bind([](int j){}, i);  // Allow this.

On implementation side, it will accept an object if its class have `R T::operator()(Args...)` and the instance is convertible to `R(*)(Args...)`.

WDYT?

Daniel Cheng

unread,
Jun 24, 2016, 3:34:27 AM6/24/16
to Taiju Tsuiki, cxx, Ken Rockot, Dana Jansens
This seems reasonable to me and is the approach I advocated for in https://codereview.chromium.org/2090013004/.

Non-blocking random thought: in the future, I wonder if we should require annotating all pointer lifetimes for base::Bind to make code like this impossible:
  auto cb = base::Bind([](MyClass* c) { c->MyMethod(); }, raw_my_class_ptr);
I'm guessing this would be a pretty large project, and of course, it's already possible today: it's just lambdas make it a little easier.

Daniel

Jeremy Roman

unread,
Jun 24, 2016, 10:22:14 AM6/24/16
to Daniel Cheng, Taiju Tsuiki, cxx, Ken Rockot, Dana Jansens
FWIW, I think using captureless lambda in this way is reasonable (I just haven't gotten around to finishing a CL to do it). But base/OWNERS would have to OK it.

It already works if you explicitly cast to a function pointer; it's just that the template magic doesn't currently handle doing it automagically.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAF3XrKqCAf19G931y%2BDt5G_-kQaH3Qa5xDBAM18_M_Khtadodw%40mail.gmail.com.

dan...@chromium.org

unread,
Jun 24, 2016, 1:39:20 PM6/24/16
to Taiju Tsuiki, cxx, Ken Rockot, Daniel Cheng
That is in line with the guidance expressed in the style guide currently, and I think it would be nice. 

Nico Weber

unread,
Jun 24, 2016, 7:10:46 PM6/24/16
to Dana Jansens, Taiju Tsuiki, cxx, Ken Rockot, Daniel Cheng
Sounds good to me too (unless the implementation for this is complicated, since bind is already hard to understand).

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
Reply all
Reply to author
Forward
0 new messages