Home > Backend Development > C++ > body text

What are the writing specifications for function lambda expressions in C++?

WBOY
Release: 2024-04-11 12:54:01
Original
921 people have browsed it

C The standard syntax for writing function Lambda expressions is: [capture](parameters) -> return_type { // Function body}, where capture is the capture of external variables, parameters is the function parameters, and return_type is the function return value type. Its types are divided according to the capture list and parameter list: capture all external variables, no parameters: auto type; capture specific external variables, no parameters: auto type (can be reduced); no capture, parameters: function type. Lambda expressions are used to create temporary function objects, which can be assigned to variables or function pointers, or passed directly as arguments.

C++ 函数lambda表达式的写法规范是什么?

C function Lambda expression writing specification

Syntax:

[capture](parameters) -> return_type {
  // 函数体
}
Copy after login

Among them:

  • ##capture: Capture external variables, optional, default is [&] (capture all external variables)
  • parameters: function parameters, optional
  • return_type: function return value type, optional

Lambda expression type:

Lambda expressions belong to the anonymous function type. Its type depends on the capture list and parameter list:

    Capture all external variables, no parameters:
  • auto Type
  • Capture specific external variables, no parameters:
  • auto Type (can be reduced)
  • No capture, with parameters:
  • function Type

Usage specifications:

    Lambda expression is used to create a temporary function object.
  • It can be assigned to a variable or function pointer, or passed directly as a parameter.
  • Captured external variables can be accessed in a Lambda expression, but they cannot be modified (unless captured by reference).
  • The
  • this pointer in Lambda expressions points to the context in which they were created.

Actual case:

// 捕获所有外部变量,无参数
auto lambda1 = []() {
  // 可以访问外部变量
  std::cout << "Lambda 1: " << x << std::endl;
};

// 捕获特定外部变量,无参数
int x = 10;
auto lambda2 = [x]() {
  // 只可以访问捕获的外部变量 x
  std::cout << "Lambda 2: " << x << std::endl;
};

// 无捕获,有参数
auto lambda3 = [](int y) {
  // 没有捕获外部变量,y 为函数参数
  std::cout << "Lambda 3: " << y << std::endl;
};

int main() {
  lambda1();
  lambda2();
  lambda3(20);

  return 0;
}
Copy after login

Output:

Lambda 1: 10
Lambda 2: 10
Lambda 3: 20
Copy after login

The above is the detailed content of What are the writing specifications for function lambda expressions in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
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