> ## 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++ Mutable Lambda

A mutable lambda in C++ is an anonymous function object that allows the modification of variables captured by value. By default, the compiler-generated function call operator (`operator()`) of a lambda expression is `const`-qualified, making by-value captures read-only. Appending the `mutable` keyword removes this `const` qualification, enabling the lambda's body to mutate its internal state (the copied variables) without affecting the original variables in the enclosing scope.

## Syntax

The `mutable` specifier is placed after the parameter list and before the trailing return type (if specified) or the function body.

```cpp theme={"dark"}
[capture_list](parameters) mutable -> return_type {
    // body
};
```

**Parameter List Omission (C++23 vs. Older Standards):**
In C++11 through C++20, if the `mutable` specifier is used, the parameter list `()` is strictly mandatory, even if the lambda accepts no arguments. As of C++23 (via proposal P1102R2), the empty parameter list `()` is optional when using `mutable` or other specifiers.

```cpp theme={"dark"}
int x = 0;
auto valid_cpp23 = [x] mutable { x++; };   // Valid in C++23; Syntax Error in C++11 through C++20
auto valid_all   = [x]() mutable { x++; }; // Valid in all standard versions
```

## Under the Hood: The Closure Object

To understand `mutable`, you must examine the compiler-generated class (the closure type) created for the lambda.

### Default Lambda (Non-Mutable)

When you capture a variable by value without `mutable`:

```cpp theme={"dark"}
int x = 10;
auto standard_lambda = [x]() { 
    // x++; // ERROR: assignment of read-only variable 'x'
};
```

The compiler generates a class roughly equivalent to this:

```cpp theme={"dark"}
class __Lambda_Default {
    int x; // Captured by value
public:
    __Lambda_Default(int _x) : x(_x) {}
    
    // operator() is implicitly const
    void operator()() const { 
        // x++; // Fails because 'this' is const
    }
};
```

### Mutable Lambda

When you apply the `mutable` keyword:

```cpp theme={"dark"}
int x = 10;
auto mutable_lambda = [x]() mutable { 
    x++; 
};
```

The compiler generates a class where the `const` qualifier is stripped from the `operator()`:

```cpp theme={"dark"}
class __Lambda_Mutable {
    int x; // Captured by value
public:
    __Lambda_Mutable(int _x) : x(_x) {}
    
    // operator() is NOT const
    void operator()() { 
        x++; // Succeeds. Modifies the closure's internal copy of 'x'
    }
};
```

## Technical Characteristics and Edge Cases

1. **State Persistence:** Because `mutable` modifies the member variables of the closure object itself, the mutated state persists across multiple invocations of the *same* lambda instance.

```cpp theme={"dark"}
int val = 0;
auto counter = [val]() mutable { return ++val; };

counter(); // returns 1
counter(); // returns 2
```

2. **Isolation from Enclosing Scope:** Mutating a by-value capture only alters the closure object's internal copy. The original variable in the enclosing scope remains completely unchanged.
3. **Top-Level `const` Variables:** If a variable is declared with a top-level `const` in the enclosing scope, capturing it by value copies the `const` qualifier directly to the closure's internal data member. In this scenario, applying `mutable` strips the `const` from `operator()`, but the internal variable itself remains a `const` type and cannot be modified.

```cpp theme={"dark"}
const int y = 10;
auto edge_case = [y]() mutable {
    // y++; // ERROR: 'y' is still a const int inside the closure
};
```

4. **Irrelevance to Reference Captures:** The `mutable` keyword has no practical effect on variables captured by reference (`[&]`, `[&x]`). A reference capture allows modification of the original variable regardless of whether the lambda is `mutable`, because the `const` qualification of the default `operator()` applies to the reference itself (which is inherently immutable in what it points to), not the referenced data.

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