Bug 42943 - std::partial_sum performs improper casts
Summary: std::partial_sum performs improper casts
Status: RESOLVED DUPLICATE of bug 22634
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.5.0
: P3 major
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-03 03:15 UTC by David Krauss
Modified: 2010-02-04 18:36 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Krauss 2010-02-03 03:15:05 UTC
std::partial_sum( first, last, result, binary_op ) (§26.4.2) is defined as

  binary_op(binary_op(..., binary_op(*first, *(first + 1)),...),
  *(first + (i - result)))

Ambiguity notwithstanding (what is the first value??), the result of each application is clearly supposed to be forwarded to the next.

However, the current implementation does this:

      typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
…
	  __value = __binary_op(__value, *__first);

This would be safer:

      typedef typename _BinaryOperation::result_type _ValueType;

Copying *first to *result would require an implicit cast from iterator_traits<_InputIterator>::value_type to _BinaryOperation::result_type, which is not required. Since an additional __binary_op() is not allowed,

      _ValueType __value( *__first );

might be the best compromise. Anyway, implicit casting the result_type to the input iterator type, as the current implementation does, seems much riskier.

The motivating case is pretty simple: I was specifically trying to avoid overflow by summing a std::vector<char> into a std::vector<int>. Furthermore, std::accumulate does correctly avoid overflow.
Comment 1 Paolo Carlini 2010-02-03 03:30:48 UTC
As far as I can tell, we are already implementing correctly the resolution of DR 539, I went through it one month ago or so:

  http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#539

If you disagree, please re-open. Thanks.
Comment 2 Paolo Carlini 2010-02-04 18:36:10 UTC
Reopen...
Comment 3 Paolo Carlini 2010-02-04 18:36:34 UTC
... to close as duplicate.

*** This bug has been marked as a duplicate of 22634 ***