Skip to main content
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.

Compiler Translation

To understand the mechanics, observe how the compiler translates a standard stateless lambda versus a static lambda. Standard Stateless Lambda:
Static Lambda:
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.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More