Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1945)

Unified Diff: base/bind_unittest.cc

Issue 2115533002: Allow binding captureless lambdas into base::Callback by base::Bind (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@05_remove_runnable
Patch Set: comment fix. rebase. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/bind_unittest.cc
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index 5a7ce38627a44d3d5e225e95fc7b9191ce8e259b..e2158d3781cae556e532725e3bcb34f4e65c5dac 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -1038,6 +1038,36 @@ TEST_F(BindTest, ArgumentCopiesAndMoves) {
EXPECT_EQ(0, move_assigns);
}
+TEST_F(BindTest, CapturelessLambda) {
+ EXPECT_FALSE(internal::IsCapturelessLambda<void>::value);
+ EXPECT_FALSE(internal::IsCapturelessLambda<int>::value);
+ EXPECT_FALSE(internal::IsCapturelessLambda<void(*)()>::value);
+ EXPECT_FALSE(internal::IsCapturelessLambda<void(NoRef::*)()>::value);
+
+ auto f = []() {};
+ EXPECT_TRUE(internal::IsCapturelessLambda<decltype(f)>::value);
+
+ int i = 0;
+ auto g = [i]() {};
+ EXPECT_FALSE(internal::IsCapturelessLambda<decltype(g)>::value);
+
+ auto h = [](int, double) { return 'k'; };
+ EXPECT_TRUE((std::is_same<
+ char(int, double),
+ internal::ExtractLambdaRunType<decltype(h)>>::value));
+
+ EXPECT_EQ(42, Bind([] { return 42; }).Run());
+ EXPECT_EQ(42, Bind([](int i) { return i * 7; }, 6).Run());
+
+ int x = 1;
+ base::Callback<void(int)> cb =
+ Bind([](int* x, int i) { *x *= i; }, Unretained(&x));
+ cb.Run(6);
+ EXPECT_EQ(6, x);
+ cb.Run(7);
+ EXPECT_EQ(42, x);
+}
+
// Callback construction and assignment tests.
// - Construction from an InvokerStorageHolder should not cause ref/deref.
// - Assignment from other callback should only cause one ref
« base/bind_internal.h ('K') | « base/bind_internal.h ('k') | base/bind_unittest.nc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698