At various places in my code, I set certain stream properties, such as in std::cout << fixed << 4.56342;
, in order to manipulate how an integer
or double
appears when printed to standard out. Sometimes during a particular runtime flow, std::cout
is used without any manipulations but the output is inadvertently transformed because of a preceding manipulation.
So, what is the best way to reset all such properties of std::cout
so that a call to std::cout<<
will behave precisely as in the following example:
#include <iostream>
int main(int argc, char **argv) {
// let X be an integer or a double
std::cout << X;
return 0;
}
I see Effective use of C++ iomanip library, and it makes sense generally to not use the manipulators directly. That will be a good strategy going forward. Still, it would be nice to know how to undo all such manipulations as I describe above.
Use resetiosflags
:
std::cout << std::resetiosflags( std::ios_base::basefield ); // clears integer manipulations std::cout << std::resetiosflags( std::ios_base::floatfield ); // clears floating-point manipulations std::cout << std::resetiosflags( std::cout.flags() ); // clears all flags