https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84928

            Bug ID: 84928
           Summary: std::accumulate should move the accumulator argument
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jens.auer-gcc at betaversion dot net
  Target Milestone: ---

std::accumulate is currently defined as

   template<typename _InputIterator, typename _Tp>
     inline _Tp
     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
     {
       // concept requirements
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
       __glibcxx_requires_valid_range(__first, __last);

       for (; __first != __last; ++__first)
         __init = __init + *__first;
       return __init;
  }

   template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
     inline _Tp
     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
                _BinaryOperation __binary_op)
     {
       // concept requirements
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
       __glibcxx_requires_valid_range(__first, __last);

       for (; __first != __last; ++__first)
         __init = __binary_op(__init, *__first);
       return __init;
  }

The accumulator argument should be moved to reduce unnecessary copies of when
passed to the binary operation or operator+:

29.8.2.2
... acc = std::move(acc) + *i or acc = binary_op(std::move(acc), *i) ...

Reply via email to