[llvm] r307490 - [ADT] Add a default constructor and a bool conversion to function_ref.

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 8 23:12:56 PDT 2017


Author: chandlerc
Date: Sat Jul  8 23:12:56 2017
New Revision: 307490

URL: http://llvm.org/viewvc/llvm-project?rev=307490&view=rev
Log:
[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.

Modified:
    llvm/trunk/include/llvm/ADT/STLExtras.h
    llvm/trunk/unittests/ADT/FunctionRefTest.cpp

Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=307490&r1=307489&r2=307490&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Sat Jul  8 23:12:56 2017
@@ -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

Modified: llvm/trunk/unittests/ADT/FunctionRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/FunctionRefTest.cpp?rev=307490&r1=307489&r2=307490&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/FunctionRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/FunctionRefTest.cpp Sat Jul  8 23:12:56 2017
@@ -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) {




More information about the llvm-commits mailing list