Home >Backend Development >C++ >What is the difference between lambda expressions and function pointers in C++ functions?

What is the difference between lambda expressions and function pointers in C++ functions?

WBOY
WBOYOriginal
2024-04-25 16:12:021165browse

Lambda expressions and function pointers are both mechanisms for encapsulating code in C, but they differ in implementation and characteristics: Implementation method: function pointers point to the memory address of the function, while Lambda expressions are inline anonymous code piece. Return type: The return type of a function pointer is fixed, whereas the return type of a lambda expression is determined by its body code block. Variable capture: Function pointers cannot capture external variables, while lambda expressions can capture external variables by reference or value through the [&] or [=] keywords. Syntax: Use asterisks (*) for function pointers and square brackets ([]) for lambda expressions.

C++ 函数中的 lambda 表达式与函数指针有何区别?

The difference between Lambda expressions and function pointers in C functions

Introduction

Lambda expressions and function pointers are both mechanisms in C for encapsulating code into callable entities. Although they have similar purposes, they have some key differences in their implementation and features.

Implementation

  • Function pointer: A variable pointing to the memory address of the function.
  • Lambda expression: An anonymous inline block of code that can capture external variables and operate on them.

Return type

  • Function pointer: The return type of the function pointer is fixed to the return type of the pointed function.
  • Lambda expression: The return type of a Lambda expression is determined by its body code block.

Variable capture

  • Function pointer: Cannot capture external variables.
  • Lambda expression: can be captured by [&] (capture by reference) or [=] (capture by value) keyword external variables.

Syntax

  • Function pointer: returnType (*functionPointerName)(parameterList);
  • Lambda expression: [captureList](parameterList) -> returnType { body }

Practical case

Function pointer case

// someFunction 是一个返回 int 的函数,接受 int 和指针作为参数
int someFunction(int a, int *ptr);

// 定义指向 someFunction 的函数指针
int (*pFunc)(int, int *) = someFunction;

// 使用函数指针调用 someFunction
int result = (*pFunc)(10, &x);

Lambda expression case

// 定义捕获外部变量 x 的 lambda 表达式
auto f = [&](int a) -> int { return a * x; };

// 使用 lambda 表达式
int result = f(10);

Summary of key differences

Features Function pointer Lambda expression
Implementation Point to memory address Inline anonymous code block
Return type Fixed to function type Determined by the body code block
Variable capture Prohibited Allow, capture by value or by reference
Syntax Use asterisks (*) Use square brackets ([])

The above is the detailed content of What is the difference between lambda expressions and function pointers in C++ functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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