[capture](parameters) -> return_type {// function body
}
- capture: 捕获列表,指定如何捕获周围作用域中的变量。
- parameters: 参数列表,与普通函数类似。
- return_type: 返回类型,可以省略,编译器会自动推断。
- function body: 函数体,包含要执行的代码。
示例
int main() { int x = 10; int y = 20; // 捕获外部变量 auto add = [x, y]() { return x + y; }; std::cout << "x + y = " << add() << std::endl; // 输出: x + y = 30 return 0; }