Summary:
As a replacement for makeFuture(). The main advantage is for non-void futures, implicit conversion works pretty well.
See unittest for examples
Test Plan: fbconfig -r folly/futures; fbmake runtests
Reviewed By: hannesr@fb.com
Subscribers: yfeldblum, trunkagent, doug, folly-diffs@, jsedgwick
FB internal diff:
D1806575
Signature: t1:
1806575:
1422465608:
6099f791591b70ce1bcda439b49307b8f3187d89
return *this;
}
+template <class T>
+template <class F>
+Future<T>::Future(
+ const typename std::enable_if<!std::is_void<F>::value, F>::type& val)
+ : core_(nullptr) {
+ Promise<F> p;
+ p.setValue(val);
+ *this = p.getFuture();
+}
+
+template <class T>
+template <class F>
+Future<T>::Future(
+ typename std::enable_if<!std::is_void<F>::value, F>::type&& val)
+ : core_(nullptr) {
+ Promise<F> p;
+ p.setValue(std::forward<F>(val));
+ *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;
+ p.setValue();
+ *this = p.getFuture();
+}
+
+
template <class T>
Future<T>::~Future() {
detach();
Future(Future&&) noexcept;
Future& operator=(Future&&);
+ // makeFuture
+ template <class F = T>
+ /* implicit */
+ Future(const typename std::enable_if<!std::is_void<F>::value, F>::type& val);
+
+ template <class F = T>
+ /* implicit */
+ Future(typename std::enable_if<!std::is_void<F>::value, F>::type&& val);
+
+ template <class F = T,
+ typename std::enable_if<std::is_void<F>::value, int>::type = 0>
+ Future();
+
~Future();
/** Return the reference to result. Should not be called if !isReady().
promise.fulfil([]{return 1l;});
}
+
+TEST(Future, Constructor) {
+ auto f1 = []() -> Future<int> { return Future<int>(3); }();
+ EXPECT_EQ(f1.value(), 3);
+ auto f2 = []() -> Future<void> { return Future<void>(); }();
+ EXPECT_NO_THROW(f2.value());
+}
+
+TEST(Future, ImplicitConstructor) {
+ auto f1 = []() -> Future<int> { return 3; }();
+ EXPECT_EQ(f1.value(), 3);
+ // Unfortunately, the C++ standard does not allow the
+ // following implicit conversion to work:
+ //auto f2 = []() -> Future<void> { }();
+}