diff --git a/stan/math/fwd/functor/reduce_sum.hpp b/stan/math/fwd/functor/reduce_sum.hpp index f8e03158c30..0a0bbeadc7b 100644 --- a/stan/math/fwd/functor/reduce_sum.hpp +++ b/stan/math/fwd/functor/reduce_sum.hpp @@ -73,19 +73,18 @@ struct reduce_sum_impl { msgs, std::forward(args)...); } else { return_type_t sum = 0.0; + std::decay_t sub_slice; for (size_t i = 0; i < (vmapped.size() + grainsize - 1) / grainsize; ++i) { size_t start = i * grainsize; size_t end = std::min((i + 1) * grainsize, vmapped.size()) - 1; - - std::decay_t sub_slice; + sub_slice.clear(); sub_slice.reserve(end - start + 1); + for (size_t i = start; i <= end; ++i) { sub_slice.emplace_back(vmapped[i]); } - - sum += ReduceFunction()(std::forward(sub_slice), start, end, msgs, - std::forward(args)...); + sum += ReduceFunction()(sub_slice, start, end, msgs, args...); } return sum; }