I recently was bitten by the fact that ios_base::width
and/or the setw
manipulator have to be reset with every item written to the stream.
That is, you must do this:
while(whatever)
{
mystream << std::setw(2) << myval;
}
Rather than this:
mystream.width(2);
while(whatever)
{
mystream << myval;
}
Ok, fine.
But does anyone know why this design decision was made? Is there some rationale that I'm missing, or is this just a dark corner of the standard?
Other stream formatting modifiers (as mentioned in the linked SO question) are 'sticky', while setw
is not.
The way i see it is : You can always do something like below if you want it to be applied uniformly.
but if it was sticky as you mention:
and if i wanted a different width every line I have to keep setting width.
So essentially both approaches are almost the same, and i would like or dislike them depending on what i am doing now.
The decisions of which manipulators should affect only the next operation seem to be based on logical and empirical observations about what tends to factor common functional needs better, and hence be easier for the programmer to write and get right.
The following points strike me as relevant:
some_stream << x
should just work right most of the timesetfill()
is not relevant unless there's a pendingsetw()
, so won't adversely affect thesome_stream << x
statement topping our listhex
andoct
are persistent, but their use is typically in a block of code that either pops the prior state or (nasty but easier) sets it back to decimalThe point leading from this that answers your question...
setw()
were presistent, it would need to be reset between each streaming statement to prevent unwanted fill...