Basic Syntax and Capturing Variables
Lambda expressions in C++ provide a concise way to define anonymous functions directly within your code. The basic structure of a lambda consists of four main parts: the capture list, the parameter list, the body, and an optional return type. The general syntax is:
[capture_list](parameters) { body }
The capture list (enclosed in square brackets) specifies which variables from the surrounding scope the lambda can access. The parameter list (inside parentheses) works like a regular function's parameter list. The body (inside curly braces) contains the statements to execute. If needed, you can specify a return type after the parameter list, but C++ can often deduce it automatically.
Capturing variables is a key feature of lambdas. When you capture a variable, you allow the lambda to access or use that variable, even though it is defined outside the lambda's body. The most common way to capture is by value, which copies the variable into the lambda.
main.cpp
12345678910111213#include <iostream> int main() { int number = 42; // Lambda capturing 'number' by value auto printNumber = [number]() { std::cout << "The captured number is: " << number << std::endl; }; printNumber(); }
In this example, the lambda [number]() { ... } captures the variable number by value. This means the lambda makes its own copy of number at the moment it is defined. If you omit the capture list entirely, as in []() { ... }, the lambda cannot access any local variables from the surrounding scope. Capturing by value allows the lambda to use variables even after their original scope has ended, but changes to the original variable after the lambda's creation will not affect the captured value.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 12.5
Basic Syntax and Capturing Variables
Desliza para mostrar el menú
Lambda expressions in C++ provide a concise way to define anonymous functions directly within your code. The basic structure of a lambda consists of four main parts: the capture list, the parameter list, the body, and an optional return type. The general syntax is:
[capture_list](parameters) { body }
The capture list (enclosed in square brackets) specifies which variables from the surrounding scope the lambda can access. The parameter list (inside parentheses) works like a regular function's parameter list. The body (inside curly braces) contains the statements to execute. If needed, you can specify a return type after the parameter list, but C++ can often deduce it automatically.
Capturing variables is a key feature of lambdas. When you capture a variable, you allow the lambda to access or use that variable, even though it is defined outside the lambda's body. The most common way to capture is by value, which copies the variable into the lambda.
main.cpp
12345678910111213#include <iostream> int main() { int number = 42; // Lambda capturing 'number' by value auto printNumber = [number]() { std::cout << "The captured number is: " << number << std::endl; }; printNumber(); }
In this example, the lambda [number]() { ... } captures the variable number by value. This means the lambda makes its own copy of number at the moment it is defined. If you omit the capture list entirely, as in []() { ... }, the lambda cannot access any local variables from the surrounding scope. Capturing by value allows the lambda to use variables even after their original scope has ended, but changes to the original variable after the lambda's creation will not affect the captured value.
¡Gracias por tus comentarios!