Boost logo

Boost :

Subject: Re: [boost] [accumulators] why not operator+=?
From: Eric Niebler (eric_at_[hidden])
Date: 2009-02-02 13:28:54

Alan Patterson wrote:
> Steven Watanabe wrote:
>> Alan Patterson wrote:
>>> Doesn't a += interface make an accumulate interface like:
>>> std::accumulate(data.begin(), data.end(), acc);
>>> more appealing than :
>>> std::for_each(data.begin(), data.end(), acc);
>> std::accumulate uses
>> result = result + *iter;
>> not
>> result += *iter;
> Yes. But I guess the original question was that why not a += (or +)
> operator implemented in accumulator_set to add samples instead of
> operator().

The answer to the original question is "because operator+ implies addition".

> Using std::accumulate to accumulate samples (in an accumulation
> library) seems more natural to me than the functional implementation.
> And, using accumulate implies addition operators.

I respectfully disagree, and "using accumulate implies addition" is one
reason. The other is that std::accumulate's interface gives it a very
limited usage. std::accumulate accepts a sequence, an initial state and
a binary operation. It applies that binary operation on each element and
  the current state to create a new state.

In contrast, an accumulator_set maintains some very complicated and
*hidden* internal state. Samples are added one at a time, and the way
the internal state is modified cannot be expressed simply or efficiently
as a binary operation that accepts the old state and the current sample.
  Conceptually, an accumulator_set is like std::accumulate's binary
function with its state argument bound to it, making it a unary function
-- unusable with std::accumulate. Trying to shoe-horn it back into the
std::accumulate interface with a too-clever operator overload is simply

Eric Niebler
BoostPro Computing

Boost list run by bdawes at, gregod at, cpdaniel at, john at