3

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"?

Lauchmelder
  • 130
  • 8
  • 4
    You do it by calling [`std::ostream::flags()`](https://en.cppreference.com/w/cpp/io/ios_base/flags), rather then via an IO manipulator. – Paul Sanders Jul 18 '20 at 23:40

1 Answers1

3

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.

Lauchmelder
  • 130
  • 8