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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Maybe we could do this:

--- a/libstdc++-v3/include/std/numeric
+++ b/libstdc++-v3/include/std/numeric
@@ -483,12 +483,16 @@ namespace __detail
                   _OutputIterator __result, _Tp __init,
                   _BinaryOperation __binary_op)
     {
-      while (__first != __last)
+      if (__first != __last)
        {
-         auto __v = __init;
-         __init = __binary_op(__init, *__first);
-         ++__first;
-         *__result++ = std::move(__v);
+         auto __sum = __binary_op(__init, *__first);
+         *__result++ = std::move(__init);
+         while (++__first != __last)
+           {
+             auto __tmp = __sum;
+             __sum = __binary_op(__sum, *__first);
+             *__result++ = std::move(__tmp);
+           }
        }
       return __result;
     }

We'd need something similar for inclusive_scan, reduce etc.

But I don't think this is actually allowed by the standard. It clearly lists
the forms allowed when invoking the summation operator, and binary_op(sum,
*first) isn't one of them.

The reason for the requirement that the result of the summation operation can
be converted to T is precisely so that the summation can be done in that type.

Reply via email to