[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
This commit is contained in:
Chandler Carruth 2017-07-09 06:12:56 +00:00
parent 7e96a89b3c
commit cc60d7b17d
2 changed files with 18 additions and 0 deletions

View File

@ -100,6 +100,8 @@ class function_ref<Ret(Params...)> {
}
public:
function_ref() : callback(nullptr) {}
template <typename Callable>
function_ref(Callable &&callable,
typename std::enable_if<
@ -110,6 +112,8 @@ public:
Ret operator()(Params ...params) const {
return callback(callable, std::forward<Params>(params)...);
}
operator bool() const { return callback; }
};
// deleter - Very very very simple method that is used to invoke operator

View File

@ -14,6 +14,20 @@ using namespace llvm;
namespace {
// Ensure that there is a default constructor and we can test for a null
// function_ref.
TEST(FunctionRefTest, Null) {
function_ref<int()> F;
EXPECT_FALSE(F);
auto L = [] { return 1; };
F = L;
EXPECT_TRUE(F);
F = {};
EXPECT_FALSE(F);
}
// Ensure that copies of a function_ref copy the underlying state rather than
// causing one function_ref to chain to the next.
TEST(FunctionRefTest, Copy) {