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.

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.
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:
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:
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.
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()(); 
Master C++ with Deep Grasping Methodology!Learn More