Boost logo

Boost :

From: Stephan T. Lavavej (stl_at_[hidden])
Date: 2004-05-21 11:50:05

[Jaakko Jarvi]
> I'm reading the standard back and forth and I do not agree
> with you.

The Standard is not very well written when it comes to this point, yes.
However, I am right, and in the course of this E-mail, you will be convinced
that I am right. :->

> What do you mean by "inherently mutating sequences"?

Algorithms which always modify the sequences that they are given.

> Does std::replace inherently modidfy sequences?

Yes, because its purpose is to write NewValue in every place that OldValue

In contrast, for_each does not necessarily have to modify sequences, if
given a functor that inspects its argument and mutates itself. However, the
functor can modify the sequence. This caused me some confusion (I thought,
as you do, that conforming implementations could implement for_each in such
a way as to disallow functors from mutating the values, and hence that
for_each was morally bankrupt) until I read something that cleared it up for

"the function object may produce an effect by modifying the input sequence,
and it can produce a useful result by mutating itself in the course of its

for_each is special, because all of the other "non-modifying sequence
operations" are /really/ non-modifying.

If you are still unconvinced, see Library DR 290, where Angelika Langer
(coauthor of the above article) complains that there are insufficient
restrictions on for_each's functor. Note that she asks that the functor be
required to not invalidate iterators or subranges. The DR is still open,
but note that the LWG's response so far is "we think this should be a
blanket statement", not "what are you talking about, for_each's functor
cannot mutate its argument period, much less invalidate iterators".


"pg 524 add at the end of the page: "The for_each() algorithm is classified
as nonmodifying because it doesn't explicitly modify a sequence. However, if
applied to a non-const sequence for_each() may change the elements of the
sequence. For an example, see the use of negate() in 11.9." (recent
standards resolution)."

If your TC++PL3 is 5th printing or higher (or if it's any printing of
Special), you can look on page 524, and there it is. Spoken by the Bearded
One himself.

Stephan T. Lavavej

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