> ## 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.

# C++ bool

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.

```cpp theme={"dark"}
#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`.

```cpp theme={"dark"}
// 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.

```cpp theme={"dark"}
#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`.

```cpp theme={"dark"}
#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;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
