Commit 25f4df21 authored by Marshall Cline's avatar Marshall Cline Committed by Facebook Github Bot

fix std::move() of a forwarding-ref - use std::forward instead

Summary:
Forwarding refs may hold lvalue-refs as well as rvalue-refs, so generally should be forward'd not move'd (to avoid erroneously consuming an lvalue).

The `initial` param of `folly::reduce(first, last, initial, func)` (also used by `folly::reduce(container, initial, func)`) is a forwarding-ref but the code used `std::move(initial)`. Goal: change that to `std::forward<T>(initial)`.

Reviewed By: yfeldblum

Differential Revision: D7827721

fbshipit-source-id: 12d4ee0ae3cfb0de71269575289c7cd8246eb468
parent b7012f16
...@@ -1413,7 +1413,7 @@ collectN(InputIterator first, InputIterator last, size_t n) { ...@@ -1413,7 +1413,7 @@ collectN(InputIterator first, InputIterator last, size_t n) {
template <class It, class T, class F> template <class It, class T, class F>
Future<T> reduce(It first, It last, T&& initial, F&& func) { Future<T> reduce(It first, It last, T&& initial, F&& func) {
if (first == last) { if (first == last) {
return makeFuture(std::move(initial)); return makeFuture(std::forward<T>(initial));
} }
typedef typename std::iterator_traits<It>::value_type::value_type ItT; typedef typename std::iterator_traits<It>::value_type::value_type ItT;
...@@ -1425,11 +1425,11 @@ Future<T> reduce(It first, It last, T&& initial, F&& func) { ...@@ -1425,11 +1425,11 @@ Future<T> reduce(It first, It last, T&& initial, F&& func) {
auto sfunc = std::make_shared<F>(std::move(func)); auto sfunc = std::make_shared<F>(std::move(func));
auto f = first->then( auto f = first->then([minitial = std::forward<T>(initial),
[ minitial = std::move(initial), sfunc ](Try<ItT> & head) mutable { sfunc](Try<ItT>& head) mutable {
return (*sfunc)( return (*sfunc)(
std::move(minitial), head.template get<IsTry::value, Arg&&>()); std::forward<T>(minitial), head.template get<IsTry::value, Arg&&>());
}); });
for (++first; first != last; ++first) { for (++first; first != last; ++first) {
f = collectAll(f, *first).then([sfunc](std::tuple<Try<T>, Try<ItT>>& t) { f = collectAll(f, *first).then([sfunc](std::tuple<Try<T>, Try<ItT>>& t) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment