Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Basic Syntax and Capturing Variables | Introduction to Lambda Expressions
C++ Lambda Expressions

bookBasic 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

main.cpp

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

question mark

Which lambda expression captures only the variable number by value from the surrounding scope

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain how to capture variables by reference in a lambda?

What are some common use cases for lambdas in C++?

Can you show an example of a lambda that modifies a captured variable?

Awesome!

Completion rate improved to 12.5

bookBasic Syntax and Capturing Variables

Stryg for at vise menuen

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

main.cpp

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

question mark

Which lambda expression captures only the variable number by value from the surrounding scope

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
some-alt