This code suffers from overflow because the type of intermediate results does not depend on the destination type:
vector< uint8_t > increments;
…
vector< uint32_t > increasing( increments.size() );
partial_sum( increments.begin(), increments.end(), increasing.begin() );
However, so does this (GCC 4.2):
partial_sum( increments.begin(), increments.end(), increasing.begin(),
plus< uint32_t >() );
Shouldn’t plus< uint32_t > promote its operands and avoid the overflow?
Edit: I’m too SO-addicted. After a short break, I sat back down and checked the implementation. It does this:
/* input_iterator::value_type */ __value = __binary_op(__value, *__first);
*++__result = __value;
I don’t think that’s compliant, so I’ll check the latest version and maybe file a bug… and here we go: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42943
According to http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#539,
partial_sumhas been completely redefined since n3000 (the latest release):and
I really can’t see the advantage of doing things this way. Reading the defect report, I don’t see any justification besides
Arrrgh.
Edit: I went ahead and implemented a widening input iterator. Works as advertised.
Would be a lot shorter if there were a generic filter-by-functor input iterator.