<div dir="ltr">This should probably be constructible from nullptr, then? (same as std::function) But I guess that's perhaps sufficiently orthogonal to be done separately/whenever someone wants that.</div><br><div class="gmail_quote"><div dir="ltr">On Sat, Jul 8, 2017 at 11:13 PM Chandler Carruth via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: chandlerc<br>
Date: Sat Jul  8 23:12:56 2017<br>
New Revision: 307490<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=307490&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=307490&view=rev</a><br>
Log:<br>
[ADT] Add a default constructor and a bool conversion to function_ref.<br>
<br>
The internal representation has a natural way to handle this and it<br>
seems nicer than having to wrap this in an optional (with its own<br>
separate flag).<br>
<br>
This also matches how std::function works.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/ADT/STLExtras.h<br>
    llvm/trunk/unittests/ADT/FunctionRefTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/STLExtras.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=307490&r1=307489&r2=307490&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=307490&r1=307489&r2=307490&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Sat Jul  8 23:12:56 2017<br>
@@ -100,6 +100,8 @@ class function_ref<Ret(Params...)> {<br>
   }<br>
<br>
 public:<br>
+  function_ref() : callback(nullptr) {}<br>
+<br>
   template <typename Callable><br>
   function_ref(Callable &&callable,<br>
                typename std::enable_if<<br>
@@ -110,6 +112,8 @@ public:<br>
   Ret operator()(Params ...params) const {<br>
     return callback(callable, std::forward<Params>(params)...);<br>
   }<br>
+<br>
+  operator bool() const { return callback; }<br>
 };<br>
<br>
 // deleter - Very very very simple method that is used to invoke operator<br>
<br>
Modified: llvm/trunk/unittests/ADT/FunctionRefTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/FunctionRefTest.cpp?rev=307490&r1=307489&r2=307490&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/FunctionRefTest.cpp?rev=307490&r1=307489&r2=307490&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/ADT/FunctionRefTest.cpp (original)<br>
+++ llvm/trunk/unittests/ADT/FunctionRefTest.cpp Sat Jul  8 23:12:56 2017<br>
@@ -14,6 +14,20 @@ using namespace llvm;<br>
<br>
 namespace {<br>
<br>
+// Ensure that there is a default constructor and we can test for a null<br>
+// function_ref.<br>
+TEST(FunctionRefTest, Null) {<br>
+  function_ref<int()> F;<br>
+  EXPECT_FALSE(F);<br>
+<br>
+  auto L = [] { return 1; };<br>
+  F = L;<br>
+  EXPECT_TRUE(F);<br>
+<br>
+  F = {};<br>
+  EXPECT_FALSE(F);<br>
+}<br>
+<br>
 // Ensure that copies of a function_ref copy the underlying state rather than<br>
 // causing one function_ref to chain to the next.<br>
 TEST(FunctionRefTest, Copy) {<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>