Summary:
To support following code:
```
void foo(folly::FunctionRef<void(void)> callback = {}) {
if (callback) {
callback();
}
}
```
Reviewed By: yfeldblum
Differential Revision:
D4372296
fbshipit-source-id:
7d21e6a44b6f6b046b424f0139465511dbae7b8b
ReturnType operator()(Args... args) const {
return call_(object_, static_cast<Args&&>(args)...);
}
+
+ explicit operator bool() const {
+ return object_;
+ }
};
} // namespace folly
TEST(FunctionRef, DefaultConstructAndAssign) {
FunctionRef<int(int, int)> fref;
+ EXPECT_FALSE(fref);
EXPECT_THROW(fref(1, 2), std::bad_function_call);
int (*func)(int, int) = [](int x, int y) { return 10 * x + y; };
fref = func;
+ EXPECT_TRUE(fref);
EXPECT_EQ(42, fref(4, 2));
}