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

A static lambda (introduced in C++23) is a lambda expression explicitly declared with the `static` specifier, which instructs the compiler to generate a closure type where the overloaded call operator (`operator()`) is a static member function rather than a non-static member function.

Because static member functions do not possess an implicit `this` pointer, a static lambda is strictly prohibited from capturing any state.

## Syntax

The `static` keyword is placed after the parameter list and before any trailing return type, `noexcept` specifier, `requires` clause, or the function body.

```cpp theme={"dark"}
auto static_lambda = [](int x, int y) static -> int {
    return x + y;
};
```

## Compiler Translation

To understand the mechanics, observe how the compiler translates a standard stateless lambda versus a static lambda.

**Standard Stateless Lambda:**

```cpp theme={"dark"}
auto normal = [](int x) { return x; };

// Compiler-generated equivalent:
struct __ClosureType_Normal {
    inline auto operator()(int x) const {
        return x;
    }
    // Implicit conversion operator to function pointer
    using fptr_t = int(*)(int);
    inline operator fptr_t() const {
        return __invoke;
    }
    static int __invoke(int x) {
        return x;
    }
};
```

**Static Lambda:**

```cpp theme={"dark"}
auto static_l = [](int x) static { return x; };

// Compiler-generated equivalent:
struct __ClosureType_Static {
    inline static auto operator()(int x) {
        return x;
    }
    // Implicit conversion operator to function pointer
    using fptr_t = int(*)(int);
    inline operator fptr_t() const {
        return operator();
    }
};
```

In the static lambda, the `operator()` itself is static. This eliminates the need for the compiler to generate a separate static `__invoke` thunk function to facilitate function pointer decay. The call operator natively matches the signature and calling convention of a standard free function.

## Constraints and Rules

1. **Empty Capture List:** The capture list must be strictly empty (`[]`). Attempting to capture variables by value or reference (`[=]`, `[&]`, `[x]`) will result in a compilation error, as there is no instantiated closure object state to access.
2. **No `mutable` Specifier:** A static lambda cannot be marked `mutable`. The `mutable` keyword is used to remove the implicit `const` qualification from a non-static `operator()`, which is semantically invalid for a static member function.
3. **C++ Standard:** Requires compilation with `-std=c++23` or later.

## Invocation Mechanics

Because the call operator is static, the compiler does not need to instantiate or pass a hidden closure object instance (the `this` pointer) to invoke the function.

```cpp theme={"dark"}
auto my_lambda = []() static { return 42; };

// Standard invocation
int a = my_lambda(); 

// Direct invocation via the closure type's static operator
using ClosureType = decltype(my_lambda);
int b = ClosureType::operator()(); 
```

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