Home > Back-end >  Difference between std::boolalpha vs std::ios::boolalpha
Difference between std::boolalpha vs std::ios::boolalpha

Time:01-12

I understood that we can use both options below to change the boolalpha flag, but why do we need to qualify ios_base in the setf() function and can't use std::boolalpha within the function parameter?

std::cout << std::boolalpha; //using manipulator
std::cout.setf(ios_base::boolalpha); // we can't use std::cout.setf(std::boolalpha);

CodePudding user response:

std::boolalpha is a stream function object (also known as a manipulator) that sets the boolalpha flag on a stream. This flag causes boolean values to be written out as "true" or "false" rather than as "1" or "0".

The setf() function is a member function of the std::ios_base class, which is the base class for all stream classes. The setf() function is used to set various formatting flags on a stream, including the boolalpha flag. The first argument to setf() is an enumeration value that specifies which flag to set.

In this case, ios_base::boolalpha is the enumeration value that represents the boolalpha flag, so std::cout.setf(ios_base::boolalpha) sets the boolalpha flag on the std::cout stream.

The reason you can't use std::boolalpha within the function parameter, is that std::boolalpha is a manipulator, and setf() expects the enumeration type as its argument, not a manipulator.

Additionally, a manipulator can be used to simply change the flag, unlike setf() that is meant to be used as a more powerful and general set of operations.

  •  Tags:  
  • c
  • Related