Home > Backend Development > C++ > body text

How does a lambda expression in a C++ function capture external variables via parameter passing?

王林
Release: 2024-04-25 16:15:01
Original
1003 people have browsed it

C Lambda expressions can capture external variables through parameter passing. The specific steps are as follows: Define a function that accepts a lambda expression as a parameter. Capture external variables in lambda expressions. Pass a lambda expression as a parameter to the function. Call the lambda expression in the function to access the captured external variables.

C++ 函数中的 lambda 表达式如何通过参数传递捕获外部变量?

How Lambda expressions in C functions capture external variables through parameter passing

Lambda expressions are a type of expression in C A convenient way to define anonymous functions. They can capture external variables, making them accessible within function scope. When you need to pass a lambda expression as a parameter to another function, you can capture external variables through parameter passing.

The following steps show how to capture external variables via parameter passing:

#include <iostream>

using namespace std;

// 外部变量
int global_var = 10;

// 接受 lambda 表达式作为参数的函数
void print_captured_var(function<void(int)> lambda) {
    // 在 lambda 表达式中访问捕获的外部变量
    lambda(global_var);
}

int main() {
    // 定义 lambda 表达式,捕获外部变量 global_var
    auto lambda = [](int x) { cout << "捕获的变量:" << x << endl; };

    // 将 lambda 表达式作为参数传递给 print_captured_var 函数
    print_captured_var(lambda);

    return 0;
}
Copy after login

Output:

捕获的变量:10
Copy after login

In this example:

  • global_var is an external int variable whose value is initialized to 10.
  • print_captured_var The function accepts a function object lambda as a parameter. It will call the function object and pass the value of a captured external variable as an actual parameter.
  • lambda A function is a lambda expression that captures the external variable global_var. In a lambda expression, the value of the external variable is printed to standard output. The
  • main function passes the lambda expression as a parameter to the print_captured_var function and then calls the function.
  • print_captured_var The function prints the value of the captured external variable in the lambda expression, getting the output "Captured variable: 10".

The above is the detailed content of How does a lambda expression in a C++ function capture external variables via parameter passing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template