Find current base of output stream

Viewed 137

Is it possible, when overloading the << operator to write to an output stream, to get what numeric base this stream is currently in? E.g. if std::hex was called before calling my overloaded operator, can I find out if the stream is currently in "hex mode"?

1 Answers

Okay after digging a little about how this base change actually works, I found out that there are basically only these 3 bases to choose from (std::dec, std::oct, std::hex). Calling std::setbase() with a value other than 10, 8 or 16 simply defaults to dec.

As Paul Sanders figured out one can get the current flags via std::ostream::flags() and then & those with the basefield:

(std::ostream::flags() & std::ios_base::<base> is 0 if it's not the current base, and its something else if it is.

Related