Assuming you know which formatting flag are actually used, you can just save their original value and restore them later. When the set of formatting flags being changed is large and possibly somewhat out of the control of the function, there are essentially two approaches which are both, unfortunately, not really very cheap:
Do not use the std::stringstream
directly but rather use a temporary stream to which the format flags get applied:
{
std::ostream out(ss.rdbuf());
out << std::showbase << std::uppercase << std::hex << 15;
}
ss << 15;
You can use the copyfmt()
member to copy all the formatting flags and later restore these:
std::ostream aux(0); // sadly, something stream-like is needed...
out.copyfmt(ss);
out << std::showbase << std::uppercase << std::hex << 15;
ss.copyfmt(aux);
out << 15;
Both approaches need to create a stream which is, unfortunately, not really fast due to a few essentially mandatory synchronizations (primarily for creating the std::locale
member).