Re: another cout format question
On Aug 2, 4:01 am, "RB" <NoMail@NoSpam> wrote:
I do (out of ignorance) wonder "why" they would reset the
cout.width back to zero. I'm sure they must have had a reason
but it does seem inconvenient to the user.
Would you really like it if you wrote something like:
std::cout << std::setw(5) << width << " cm" << std::endl
and got:
25 cm
? For similar reasons, iostreams ignores the precision
specifier for strings, since it isn't reset. In printf,
a precision specifier of a string sets the maximum number of
characters in the string, e.g.:
printf( "%-8.8s\n", someString);
will always print exactly 8 characters. In iostream, this was
deamed less useful than having precision "sticky".
My own feelings are that nothing should be sticky: a formatted
output function should reset everything to its default state.
I've been hit too often by someone who slips in an std::hex for
some debugging output, without resetting it. In my own work,
I use semantically significant application domain manipulators,
which restore the original state at the end of the full
expression. I would argue that this is the only reasonable
solution, except maybe in a case like width. (But in practice,
width doesn't matter much anymore. It's classical use was to
format tables, but now that almost no output uses fixed width
fonts, it doesn't work for that.)
--
James Kanze