TITLE: streams, setiosflags, and manipulators (Newsgroups: comp.std.c++, 3 Feb 98) MANN: "Steve Mann" wrote: |> > #include |> > #include |> [snip] |> > int |> > main( int ac, char **av ) |> > { |> > int bNum = -55; |> > |> > cout << setiosflags( ios::showpos | ios::internal ) |> > << setfill( '0' ) |> > << setw( 9 ) |> > << bNum << endl; |> > } |> > |> > I get output that looks like: |> > |> > 000000-55 KUEHL: Dietmar.Kuehl@IZB-SOFT.de |> This output is plain wrong. Complain to your vendor about this |> misfeature. KANZE: kanze@gabi-soft.fr (J. Kanze) In this particular case. Although not relevant to the standardization issue, it is probably worth pointing out that you can't generally use the setiosflags manipulator to set ios::internal; because this manipulator doesn't reset anything, it can really only be used alone with the boolean flags. Normally, I would expect to see the above written: cout << showpoint << internal << ... To set ios::internal using the setiosflags, you have to use resetiosflags first, e.g.: cout << resetiosflags( ios::adjustfield ) << setiosflags( ios::internal | ios::showpos ). (Question: since there is a setbase manipulator, is there any reason why there aren't a setadjust and setfloat manipulators?) In general, unless the case is truely exceptional, I would suggest defining an explicit manipulator, something like: ios_base& signedDecimalInt( ios_base& stream ) { stream.flags( ios::dec | ios::showpos | ios::internal ) ; stream.fill( '0' ) ; return stream ; } // ... cout << signedDecimalInt << setw( 9 ) << bNum << endl ; Note that done this way, instead of using setf and resetf, you guarantee the state of the other bits as well. (General rule: always leave the formatting state the way you found it, but don't count on it being in any particular state.) If you want to get really clever, you can probably work out a way of returning a temporary whose destructor restores the stream's previous formatting state. (I've gotten into the habit of always declaring an instance of an iosave class at the top of any block or function which does formatting, and don't generally worry about restoring at the level of the individual statement.)