Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The bool type in C++ is a fundamental integral data type designed to represent boolean logic, holding one of two standard C++ boolean literals: true or false. As a distinct type, it participates in standard integral promotions and specific implicit type conversions defined by the C++ standard.

Memory Representation and Size

Although a boolean value theoretically requires only a single bit of information, the C++ standard dictates that sizeof(bool) is implementation-defined. In virtually all modern architectures, a bool occupies exactly 1 byte (8 bits). This is because the byte is the smallest addressable unit of memory in C++; the CPU cannot independently address a single bit.
#include <iostream>

int main() {
    bool is_valid = true;
    bool is_empty = false;
    
    // Typically outputs 1
    std::cout << sizeof(bool) << '\n'; 
    return 0;
}

Implicit Conversion Rules

Conversions involving bool are governed by specific standard rules ([conv.bool] and [conv.prom]), which differ depending on the source type. From Arithmetic and Unscoped Enum Types to bool: Any zero value (0, 0.0, \0) evaluates to false. Any non-zero value evaluates to true. From Pointers to bool: Pointers implicitly convert to bool due to a dedicated boolean conversion rule, not because bool is an integral type (pointers cannot implicitly convert to other integral types like int). A null pointer evaluates to false, while any non-null pointer evaluates to true. From std::nullptr_t to bool: The std::nullptr_t type cannot be implicitly converted to bool via copy-initialization. It requires direct-initialization or contextual conversion (e.g., within an if statement condition). From bool to Integer: When a bool is used in an arithmetic context, it undergoes integral promotion: false promotes to 0, and true promotes to 1.
// Arithmetic to bool conversions
bool b1 = 0;         // Evaluates to false
bool b2 = 42;        // Evaluates to true
bool b3 = -3.14;     // Evaluates to true

// Pointer and nullptr_t to bool conversions
int x = 10;
int* ptr = &x;
bool b4 = ptr;       // Evaluates to true (boolean conversion rule)
bool b5{nullptr};    // Evaluates to false (direct-initialization)
// bool b6 = nullptr; // ERROR: copy-initialization is ill-formed

// bool to integer promotions
int i1 = true;       // Evaluates to 1
int i2 = false;      // Evaluates to 0

The std::vector<bool> Specialization

The standard library provides a specialized implementation for std::vector<bool>. Unlike standard vectors, std::vector<bool> is space-optimized so that each element occupies only a single bit of memory. Because C++ cannot directly address individual bits, std::vector<bool>::operator[] does not return a standard reference (bool&). Instead, it returns a proxy object (std::vector<bool>::reference) that simulates a reference. This architectural quirk breaks compatibility with many standard template algorithms and functions that strictly require true references or pointers to elements.
#include <vector>

int main() {
    std::vector<bool> v = {true, false};
    
    // ERROR: Cannot bind non-const lvalue reference to a temporary proxy object
    // bool& ref = v[0]; 
    
    // Correct: Yields std::vector<bool>::reference proxy object
    auto proxy = v[0];   
    
    // Correct: Implicitly converts the proxy object to a standard bool
    bool val = v[0];     
    
    return 0;
}

Standard I/O Stream Behavior

By default, standard input/output streams (std::cin and std::cout) treat bool values as integers, outputting or expecting 1 for true and 0 for false. To alter the stream state to parse or emit the textual representations "true" and "false", the std::boolalpha I/O manipulator must be applied. The stream state can be reverted using std::noboolalpha.
#include <iostream>

int main() {
    bool flag = true;

    // Default behavior: outputs "1"
    std::cout << flag << '\n'; 

    // Modified stream state: outputs "true"
    std::cout << std::boolalpha << flag << '\n'; 

    // Reverted stream state: outputs "0"
    flag = false;
    std::cout << std::noboolalpha << flag << '\n'; 

    return 0;
}
Master C++ with Deep Grasping Methodology!Learn More