llvm.org GIT mirror llvm / cc60d7b
[ADT] Add a default constructor and a bool conversion to function_ref. The internal representation has a natural way to handle this and it seems nicer than having to wrap this in an optional (with its own separate flag). This also matches how std::function works. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307490 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 2 years ago
2 changed file(s) with 18 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
9999 }
100100
101101 public:
102 function_ref() : callback(nullptr) {}
103
102104 template
103105 function_ref(Callable &&callable,
104106 typename std::enable_if<
109111 Ret operator()(Params ...params) const {
110112 return callback(callable, std::forward(params)...);
111113 }
114
115 operator bool() const { return callback; }
112116 };
113117
114118 // deleter - Very very very simple method that is used to invoke operator
1313
1414 namespace {
1515
16 // Ensure that there is a default constructor and we can test for a null
17 // function_ref.
18 TEST(FunctionRefTest, Null) {
19 function_ref F;
20 EXPECT_FALSE(F);
21
22 auto L = [] { return 1; };
23 F = L;
24 EXPECT_TRUE(F);
25
26 F = {};
27 EXPECT_FALSE(F);
28 }
29
1630 // Ensure that copies of a function_ref copy the underlying state rather than
1731 // causing one function_ref to chain to the next.
1832 TEST(FunctionRefTest, Copy) {