From: Chandler Carruth Date: Sun, 9 Jul 2017 06:12:56 +0000 (+0000) Subject: [ADT] Add a default constructor and a bool conversion to function_ref. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cc60d7b17de095ac429f5066d0bea80837fabb65;p=llvm [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 --- diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index 8c28412bb60..83f289c42a2 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -100,6 +100,8 @@ class function_ref { } public: + function_ref() : callback(nullptr) {} + template function_ref(Callable &&callable, typename std::enable_if< @@ -110,6 +112,8 @@ public: Ret operator()(Params ...params) const { return callback(callable, std::forward(params)...); } + + operator bool() const { return callback; } }; // deleter - Very very very simple method that is used to invoke operator diff --git a/unittests/ADT/FunctionRefTest.cpp b/unittests/ADT/FunctionRefTest.cpp index 075d9a070df..b7ef7d79e5f 100644 --- a/unittests/ADT/FunctionRefTest.cpp +++ b/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 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) {