diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2017-07-09 06:12:56 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2017-07-09 06:12:56 +0000 |
commit | a809f28956fabc28d29103aada5f72cf847efe91 (patch) | |
tree | a484c2527a0bc4c5a619523e68fc3f7124eb0894 | |
parent | b80b44b7b9a47058c3df08e390517617968ebc83 (diff) | |
download | bcm5719-llvm-a809f28956fabc28d29103aada5f72cf847efe91.tar.gz bcm5719-llvm-a809f28956fabc28d29103aada5f72cf847efe91.zip |
[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.
llvm-svn: 307490
-rw-r--r-- | llvm/include/llvm/ADT/STLExtras.h | 4 | ||||
-rw-r--r-- | llvm/unittests/ADT/FunctionRefTest.cpp | 14 |
2 files changed, 18 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 8c28412bb60..83f289c42a2 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -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 diff --git a/llvm/unittests/ADT/FunctionRefTest.cpp b/llvm/unittests/ADT/FunctionRefTest.cpp index 075d9a070df..b7ef7d79e5f 100644 --- a/llvm/unittests/ADT/FunctionRefTest.cpp +++ b/llvm/unittests/ADT/FunctionRefTest.cpp @@ -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) { |