From: Hannes Roth Date: Wed, 5 Mar 2014 17:50:28 +0000 (-0800) Subject: (Wangle) Actually support the universal reference used in whenAll X-Git-Tag: v0.22.0~658 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=dacdff5d2c900f709259a18f91a8848ddd574848;p=folly.git (Wangle) Actually support the universal reference used in whenAll Summary: Need to use `std::decay` to actually support the universal reference. The futures are moved in, and invalidated. Test Plan: Test; does not compile before. Reviewed By: hans@fb.com FB internal diff: D1199841 --- diff --git a/folly/wangle/Future-inl.h b/folly/wangle/Future-inl.h index 9a606915..6b5907dc 100644 --- a/folly/wangle/Future-inl.h +++ b/folly/wangle/Future-inl.h @@ -282,13 +282,16 @@ makeFuture(E const& e) { // when (variadic) template -typename detail::VariadicContext::type +typename detail::VariadicContext< + typename std::decay::type::value_type...>::type whenAll(Fs&&... fs) { - auto ctx = new detail::VariadicContext(); + auto ctx = + new detail::VariadicContext::type::value_type...>(); ctx->total = sizeof...(fs); auto f_saved = ctx->p.getFuture(); - detail::whenAllVariadicHelper(ctx, std::forward(fs)...); + detail::whenAllVariadicHelper(ctx, + std::forward::type>(fs)...); return std::move(f_saved); } diff --git a/folly/wangle/Future.h b/folly/wangle/Future.h index f590e091..d89f7134 100644 --- a/folly/wangle/Future.h +++ b/folly/wangle/Future.h @@ -197,8 +197,10 @@ whenAll(InputIterator first, InputIterator last); /// This version takes a varying number of Futures instead of an iterator. /// The return type for (Future, Future, ...) input /// is a Future, Try, ...>>. +/// The Futures are moved in, so your copies are invalid. template -typename detail::VariadicContext::type +typename detail::VariadicContext< + typename std::decay::type::value_type...>::type whenAll(Fs&&... fs); /** The result is a pair of the index of the first Future to complete and diff --git a/folly/wangle/test/FutureTest.cpp b/folly/wangle/test/FutureTest.cpp index a70af1ab..e356d0de 100644 --- a/folly/wangle/test/FutureTest.cpp +++ b/folly/wangle/test/FutureTest.cpp @@ -521,6 +521,27 @@ TEST(Future, whenAllVariadic) { EXPECT_TRUE(flag); } +TEST(Future, whenAllVariadicReferences) { + Promise pb; + Promise pi; + Future fb = pb.getFuture(); + Future fi = pi.getFuture(); + bool flag = false; + whenAll(fb, fi) + .then([&](Try, Try>>&& t) { + flag = true; + EXPECT_TRUE(t.hasValue()); + EXPECT_TRUE(std::get<0>(t.value()).hasValue()); + EXPECT_EQ(std::get<0>(t.value()).value(), true); + EXPECT_TRUE(std::get<1>(t.value()).hasValue()); + EXPECT_EQ(std::get<1>(t.value()).value(), 42); + }); + pb.setValue(true); + EXPECT_FALSE(flag); + pi.setValue(42); + EXPECT_TRUE(flag); +} + TEST(Future, whenAll_none) { vector> fs; auto f = whenAll(fs.begin(), fs.end());