Most of the format options are boolean values, represented by
single bits. To set a bit, the implementation simply or's the
bit value with the existing value. For example, if you call
std::cout.setf( std::ios::showpos ), the results will be the
previous value, but with the showpos flag unconditionally set.
For such boolean values, the single argument form of setf is
appropriate, and to reset, you would normally use ios::unsetf.
Three of the format values, however, are non-boolean (i.e. they
have more than two possible values): floatfield, adjustfield and
basefield. Because they have more than two values, they
consist of more than one bit, and simple or'ing won't work.
Consider a case, for example, where the floatfield values are
defined as: unnamed default: 0, fixed: 1, scientific: 2. If the
current value is fixed, and you simply or in scientific, the
results will be 3---an illegal value, which may cause strange
things to happen. For these fields, the two argument form of
setf is used; this form first and's the format with the
complement of the second argument, effectively setting all of
the bits from the second argument to 0, before or'ing the first
argument (which is also and'ed with the second). And the values
floatfield, adjustfield and basefield are defined to contain all
of the bits which need to be reset---with the example values
above, the value would be 3.
Note that this two argument form could be used to reset any of
the one bit flags: call it with 0 as the first argument, and all
of the flags to be reset as the second. This is very
unidiomatic, however, and I wouldn't do it. Basically, in well
written code, the following rules apply:
-- The two argument form is always used to set the base, the
floating point format, and the alignment. The second
argument is always one of ios::floatfield, ios::basefield or
ios::alignfield; the acceptable values for the first
argument depend on the second argument (and may be 0, cast
to the type fmtflags, although unsetf can also be used in
this case). The two argument form is used to set one field
at a time; although it's quite possible to set more, it's
quite unidiomatic, which renders the code more difficult to
read.
-- The one argument form of setf is used to set any of the
other values, and unsetf is used to reset them. Unlike the
case of the two argument form, it's quite idiomatic to
combine values here, e.g. to call setf with something like
"ios::showpoint | ios::showpos".
-- The "easiest" way to do complicated manipulations on the
flags is to read them, using ios::flags(), manipulate your
local fmtflags variable, then set the flags to the resulting
value (using the non-const std::flags()). This is often
used as well because you want to save the original value,
and restore it when you're through.
I use the third solution in my custom manipulators (which
restore the original flags in their destructor); for some
examples of this, you might want to look at the StateSavingManip
and *Fmt in IO sub-system of my library
(
http://kanze.james.neuf.fr/code-en.html). (Note that most
application code should not manipulate the flags directly, nor
use the standard manipulators, but use rather application
specific manipulators.)