4

As stated in the [ostream.inserters.character] section of the C++ standard, after a char or string is inserted into a stream the stream's width is set to 0:

template<class traits>
  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
                                         const unsigned char* s);
Do lots of sensible and expected things...
Calls width(0).

Why does the C++ standard dictate a call to width(0)?

What is the rational for changing the streams width and not reseting it to its original value? (As far as I know all other properties of a stream are preserved by the stream insertion operators.)

There is a related question here, which demonstrates the confusion this causes, but does not explain why the standard dictates this behavior.

Community
  • 1
  • 1
andypea
  • 1,343
  • 11
  • 22
  • 4
    I don't want to write an answer, because only the people involved with this decision can say, but I'm very sure that the reason is convenience. The cases where you don't want to maintain `width` across multiple outputs heavily outweigh those where you do. – Collin Dauphinee Feb 25 '14 at 05:32
  • 1
    [This answer](http://stackoverflow.com/a/1533752/341970) contains a *guess*. – Ali Feb 25 '14 at 11:38

1 Answers1

1

It seems not to be "some" operator<< functions, in general Inserters and extractors (21.4.8.9). See c++ standard and a sensible reasoning in Tony D's answer

Community
  • 1
  • 1
Efren
  • 4,003
  • 4
  • 33
  • 75