C
Carsten Fuchs
Dear group,
I was wondering what the best way is to produce predictable output with std::cout (and other output
streams)?
That is, one of the biggest weaknesses of std::cout seems to be the fact that a simple statement like
int i=1234;
cout << i;
can yield different outputs depending on the flags set earlier (e.g. decimal, hex or oct numbers).
Due to the global nature of std::cout, any component (e.g. a third-party library or module) of a
program that sets any ios_base flags for its purposes may make the output of std::cout for the rest
of the program entirely unpredictable!
Well, I'm looking for the "best" method in order to solve this unpredictability, or at least for the
most common solutions to the problem.
It's certainly possible to reset or properly set all the flags prior to each cout output manually,
but in practice, that's infeasible (e.g. when one third-party library sets certain flags, and the
other doesn't have the "reset" code) and cumbersome (the flags setup code would be a lot longer than
the actual cout << i; statement).
(Note that good old printf(), despite its many problems and disadvantages, used to be entirely free
of this kind of problem due to its stateless nature. printf("%i", i); though not type-safe and
not useful for custom classes, is short and predictable...)
I'd be very happy about your advice!
Thank you very much in advance, and
best regards,
Carsten
I was wondering what the best way is to produce predictable output with std::cout (and other output
streams)?
That is, one of the biggest weaknesses of std::cout seems to be the fact that a simple statement like
int i=1234;
cout << i;
can yield different outputs depending on the flags set earlier (e.g. decimal, hex or oct numbers).
Due to the global nature of std::cout, any component (e.g. a third-party library or module) of a
program that sets any ios_base flags for its purposes may make the output of std::cout for the rest
of the program entirely unpredictable!
Well, I'm looking for the "best" method in order to solve this unpredictability, or at least for the
most common solutions to the problem.
It's certainly possible to reset or properly set all the flags prior to each cout output manually,
but in practice, that's infeasible (e.g. when one third-party library sets certain flags, and the
other doesn't have the "reset" code) and cumbersome (the flags setup code would be a lot longer than
the actual cout << i; statement).
(Note that good old printf(), despite its many problems and disadvantages, used to be entirely free
of this kind of problem due to its stateless nature. printf("%i", i); though not type-safe and
not useful for custom classes, is short and predictable...)
I'd be very happy about your advice!
Thank you very much in advance, and
best regards,
Carsten