Future<Unit>::Future()
authorHans Fugal <fugalh@fb.com>
Thu, 30 Apr 2015 18:59:36 +0000 (11:59 -0700)
committerPraveen Kumar Ramakrishnan <praveenr@fb.com>
Tue, 12 May 2015 00:01:32 +0000 (17:01 -0700)
Summary: Allow `makeFuture()`-like default ctor for `Future<Unit>`

Test Plan: new unit test

Reviewed By: jsedgwick@fb.com

Subscribers: trunkagent, exa, folly-diffs@, jsedgwick, yfeldblum, chalfant

FB internal diff: D2029677

Signature: t1:2029677:1430417794:5ec7fca839294316957803229f4783f2ee875027

folly/futures/Future-inl.h
folly/futures/Future.h
folly/futures/Unit.h
folly/futures/test/UnitTest.cpp [new file with mode: 0644]

index 135440a9e1ebfc49569c54bcb492c0884fb04833..f6abe7b4575cd6de9c474fbfd985a6d540b72a3a 100644 (file)
@@ -51,11 +51,13 @@ Future<T>::Future(T2&& val) : core_(nullptr) {
   *this = p.getFuture();
 }
 
-template <>
-template <class F,
-          typename std::enable_if<std::is_void<F>::value, int>::type>
-Future<void>::Future() : core_(nullptr) {
-  Promise<void> p;
+template <class T>
+template <class T2,
+          typename std::enable_if<
+            folly::is_void_or_unit<T2>::value,
+            int>::type>
+Future<T>::Future() : core_(nullptr) {
+  Promise<T> p;
   p.setValue();
   *this = p.getFuture();
 }
index 3111fbf97806939dc076af118e3e56eb4ab7ca13..cc0bd36ae2d3aa9ad4028bde7f1635d2153aaff9 100644 (file)
@@ -58,8 +58,10 @@ class Future {
   /* implicit */
   template <class T2 = T> Future(T2&& val);
 
-  template <class F = T,
-            typename std::enable_if<std::is_void<F>::value, int>::type = 0>
+  template <class T2 = T,
+            typename std::enable_if<
+              folly::is_void_or_unit<T2>::value,
+              int>::type = 0>
   Future();
 
   ~Future();
index a01aa20dfce3135d75957d6c361cb9ff21a66747..cb0b92c1c0eda9d3aa1f6feb725fbd32cbbd56f2 100644 (file)
@@ -18,4 +18,11 @@ namespace folly {
 
 struct Unit {};
 
+template <class T>
+struct is_void_or_unit : public std::conditional<
+  std::is_void<T>::value || std::is_same<Unit, T>::value,
+  std::true_type,
+  std::false_type>::type
+{};
+
 }
diff --git a/folly/futures/test/UnitTest.cpp b/folly/futures/test/UnitTest.cpp
new file mode 100644 (file)
index 0000000..ea7cda7
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/futures/Future.h>
+#include <gtest/gtest.h>
+
+using namespace folly;
+
+TEST(Unit, FutureDefaultCtor) {
+  Future<Unit>();
+}
+
+TEST(Unit, voidOrUnit) {
+  EXPECT_TRUE(is_void_or_unit<void>::value);
+  EXPECT_TRUE(is_void_or_unit<Unit>::value);
+  EXPECT_FALSE(is_void_or_unit<int>::value);
+}