difference between setf() and flags() functions

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);"

I would appreciate any assistance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>

int main()
{
    const auto coutwf = [] () -> std::ostream&
    { return std::cout << std::setw(20) << std::setfill('0') ; } ;

    constexpr double d = 12345678.9 ;
    coutwf() << d << '\n' ; // 000000001.23457e+007

    // save the current format flags associated with std::cout
    const auto 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
}

http://ideone.com/C9UmBr

We could have written:

1
2
3
4
5
// 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
const auto original_flags = std::cout.setf( std::ios::fixed | std::ios::showpos ) ;


Last edited on
Thanks.I'll take a good look at this!
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)

PS: JLBorges said it better below
Last edited on
> 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 BitmaskType http://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.
- http://stdcxx.apache.org/doc/stdlibug/28-3.html

Check it out on your implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <typeinfo>
#include <type_traits>
#include <iostream>

int main()
{
    using type = decltype(std::ios_base::fixed) ;

    std::cout << "name: " << typeid(type).name() << '\n'
               << std::boolalpha
               << "integral type? " << std::is_integral<type>::value << '\n'
               << "enumeration type? " << std::is_enum<type>::value << '\n' ;
}

http://ideone.com/hNlyfk
Thanks for your time! It's pretty clear now.
Topic archived. No new replies allowed.