template <typename... Fs>
typename detail::VariadicContext<typename Fs::value_type...>::type
-whenAll(Fs&... fs)
+whenAll(Fs&&... fs)
{
auto ctx = new detail::VariadicContext<typename Fs::value_type...>();
ctx->total = sizeof...(fs);
auto f_saved = ctx->p.getFuture();
- detail::whenAllVariadicHelper(ctx, fs...);
+ detail::whenAllVariadicHelper(ctx, std::forward<Fs>(fs)...);
return std::move(f_saved);
}
/// This version takes a varying number of Futures instead of an iterator.
/// The return type for (Future<T1>, Future<T2>, ...) input
/// is a Future<std::tuple<Try<T1>, Try<T2>, ...>>.
-// XXX why does it take Fs& instead of Fs&&?
template <typename... Fs>
typename detail::VariadicContext<typename Fs::value_type...>::type
-whenAll(Fs&... fs);
+whenAll(Fs&&... fs);
/** The result is a pair of the index of the first Future to complete and
the Try. If multiple Futures complete at the same time (or are already
template <typename... Ts, typename THead, typename... Fs>
typename std::enable_if<sizeof...(Fs) == 0, void>::type
-whenAllVariadicHelper(VariadicContext<Ts...> *ctx, THead& head, Fs&... tail) {
+whenAllVariadicHelper(VariadicContext<Ts...> *ctx, THead&& head, Fs&&... tail) {
head.setContinuation([ctx](Try<typename THead::value_type>&& t) {
- const size_t i = sizeof...(Ts) - sizeof...(Fs) - 1;
- std::get<i>(ctx->results) = std::move(t);
+ std::get<sizeof...(Ts) - sizeof...(Fs) - 1>(ctx->results) = std::move(t);
if (++ctx->count == ctx->total) {
ctx->p.setValue(std::move(ctx->results));
delete ctx;
template <typename... Ts, typename THead, typename... Fs>
typename std::enable_if<sizeof...(Fs) != 0, void>::type
-whenAllVariadicHelper(VariadicContext<Ts...> *ctx, THead& head, Fs&... tail) {
+whenAllVariadicHelper(VariadicContext<Ts...> *ctx, THead&& head, Fs&&... tail) {
head.setContinuation([ctx](Try<typename THead::value_type>&& t) {
- const size_t i = sizeof...(Ts) - sizeof...(Fs) - 1;
- std::get<i>(ctx->results) = std::move(t);
+ std::get<sizeof...(Ts) - sizeof...(Fs) - 1>(ctx->results) = std::move(t);
if (++ctx->count == ctx->total) {
ctx->p.setValue(std::move(ctx->results));
delete ctx;
}
});
- whenAllVariadicHelper(ctx, tail...); // recursive template tail call
+ // template tail-recursion
+ whenAllVariadicHelper(ctx, std::forward<Fs>(tail)...);
}
template <typename T>