Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::boolalpha — textual bool values in streams

std::boolalpha enables textual output and input of bool values on a particular stream. With the classic locale, the names are true and false. The setting remains active until changed.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

std::ios_base& std::boolalpha(std::ios_base& str);
out << std::boolalpha << value;
in >> std::boolalpha >> value;

std::boolalpha is a stream manipulator declared in <ios>. Use out << std::boolalpha to configure output or in >> std::boolalpha to configure input. The function sets the std::ios_base::boolalpha flag and returns a reference to the same stream; it does not itself write or read a value.

With the flag enabled, values of type bool are written and recognized using names from the stream's locale. For std::locale::classic(), these are true and false. Do not assume that every locale uses the same names. The manipulator does not turn integers into words.

The setting belongs to the particular stream and remains active beyond the next operation. std::noboolalpha clears the flag, restoring numeric output and input for bool values without changing any other formatting flags. For example, with the classic locale and std::ios_base::showpos enabled, true is printed as +1 in decimal format. Changing the format of std::cout does not change a separate input stream's format.

On input, text that does not match the expected name causes failbit to be set; check the operation's result, for example with if (!(input >> std::boolalpha >> value)). The example gives both streams the classic locale, reads two values, and then demonstrates textual and numeric output.

Examples

#include <ios>
#include <iostream>
#include <locale>
#include <sstream>

int main() {
    std::cout.imbue(std::locale::classic());
    std::istringstream input("true false");
    input.imbue(std::locale::classic());

    bool first = false;
    bool second = true;
    if (!(input >> std::boolalpha >> first >> second)) {
        return 1;
    }

    std::cout << std::boolalpha << first << ' ' << second << '\n';
    std::cout << std::noboolalpha << first << ' ' << second << '\n';
}

Result

true false
1 0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources