Hi.I'm trying to understand the difference between the setf() and flags() functions but I'm a bit confused.
I'm reading the Herb beginners guide (It's too late , I'm nearing the end)and I can't see the difference between these two statements.
"fmtflags setf(fmtflags flags); This function returns the previous settings of the format flags and turns on those flags specified by flags."
"fmtflags flags( ); This function returns the current value of the flags relative to the invoking stream. The following form of flags( ) sets the flag values to those specified by flags and returns the previous flag values: fmtflags flags(fmtflags flags);"
#include <iostream>
#include <iomanip>
int main()
{
constauto coutwf = [] () -> std::ostream&
{ return std::cout << std::setw(20) << std::setfill('0') ; } ;
constexprdouble d = 12345678.9 ;
coutwf() << d << '\n' ; // 000000001.23457e+007
// save the current format flags associated with std::cout
constauto original_flags = std::cout.flags() ;
// set flag: use fixed notation for floating point types
// set flag: prefix non-negative numeric output with a '+'
// leave all other format flags unchanged
std::cout.setf( std::ios::fixed | std::ios::showpos ) ;
coutwf() << d << '\n' ; // 0000+12345678.900000
// set: use internal justification
// unset: left justification, right justification
// leave all other format flags unchanged
std::cout.setf( std::ios::internal, std::ios::adjustfield ) ;
coutwf() << d << '\n' ; // +000012345678.900000
// set: use left justification
// unset: right justification, internal justification
// leave all other format flags unchanged
std::cout.setf( std::ios::left, std::ios::adjustfield ) ;
coutwf() << d << '\n' ; // +12345678.9000000000
// replace *all* flags with the original flags
std::cout.flags(original_flags) ;
coutwf() << d << '\n' ; // 000000001.23457e+007
}
// save the current format flags associated with std::cout
// set flag: use fixed notation for floating point types
// set flag: prefix non-negative numeric output with a '+'
// leave all other format flags unchanged
constauto original_flags = std::cout.setf( std::ios::fixed | std::ios::showpos ) ;
There's one thing that confuses me.
What does the fmtflags bitmask enumeration look like ? Is It a single Integer which Is altered In a bitwise and operation when a flag Is turned on or Is It a number of Integers ? Or , as usual,am I completely off track! ?
What does the fmtflags bitmask enumeration look like ? Is It a single Integer
It can be an integer, an enumeration, or even std::bitset - that depends on implementation and a valid program should never depend on it (e.g. int n = std::ios_base::fixed; may or may not compile)
> What does the fmtflags bitmask enumeration look like ?
> Is It a single Integer which Is altered In a bitwise and operation when a flag Is turned on
> or Is It a number of Integers ? Or , as usual,am I completely off track! ?
No, you are not completely off track. std::ios_base::fmtflags are required to support these bitwise operations &, |, ^, ~, &=, |=, ^=
The format flags belong to an unspecified BitmaskTypehttp://en.cppreference.com/w/cpp/concept/BitmaskType
They may be implemented in different ways - an enumerated type that overloads these operators, an (unsigned) integer type, as a std::bitset<>. The actual implementation is unspecified. http://en.cppreference.com/w/cpp/io/ios_base/fmtflags (well, implementation defined as per this).
Some format flags are grouped because they are mutually exclusive; for example, output within an output field can be adjusted to the left or to the right, or to an internally specified adjustment. One and only one of the corresponding three format flags, left, right, or internal, can be set. (Iostreams does not prevent you from setting other invalid combinations of these flags, however.) If you want to set one of these bits to 1, you need to set the other two to 0. To make this easier, there are bit groups whose main function is to reset all bits in one group. The bit group for adjustment is adjustfield, defined as left | right | internal.