在lambda函数语法中,“捕获列表”服务的目的是什么? [英] In lambda functions syntax, what purpose does a 'capture list' serve?

查看:854
本文介绍了在lambda函数语法中,“捕获列表”服务的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取自此问题的答案,作为示例,此是一个计算 std :: vector 中的元素总和的代码:

  std :: for_each(
vector.begin(),
vector.end(),
[&](int n){
sum_of_elems + = n;
}
);

我明白,lambda函数只是无名的函数。



我理解 lambda函数语法如下所述



我不明白为什么lambda函数需要捕获列表,而普通函数不需要。






  • 为什么普通功能不需要这些信息? b
  • lambda函数不仅仅是无名函数?


  • 解决方案

    你给了,捕获列表定义什么从lambda的外部应该在函数体内可用和如何



    普通函数可以几种方式使用外部数据:


    1. 静态栏位

    2. 实例栏位

    3. (包括参考参数)

    4. 全局

    Lambda添加了一个未命名函数另一个。然后,lambda可以使用您指定的值。与普通函数不同,这可以包括来自外部函数的局部变量。



    如答案所述,您还可以指定要捕获的方式。 awoodland在另一个答案中给出了几个例子。例如,你可以通过引用捕获一个外部变量(如引用参数),其他所有值可以通过值来捕获:

      [= ,& epsilon b $ b  

    编辑:



    区分签名和lambda在内部使用是很重要的。 lambda的签名是参数类型的有序列表,加上返回值的类型。



    例如,一元函数取特定类型的单个值,并返回另一个类型的值。



    但是,在内部它可以使用其他值。作为一个简单的例子:

      [x,y](int z) int 
    {
    return x + y - z;
    }

    lambda的调用者只知道它需要一个 int 并返回 int 。但是,在内部,它恰好使用另外两个变量的值。


    Taken from an answer to this question, as an example, this is a code that calculates the sum of elements in a std::vector:

    std::for_each(
        vector.begin(),
        vector.end(),
        [&](int n) {
            sum_of_elems += n;
        }
    );
    

    I understand that lambda functions are just nameless functions.

    I understand the lambda functions syntax as explained here.

    I do not understand why lambda functions need the capture list, while normal functions do not.

    1. What extra information does a capture list provide?
    2. Why do normal functions not need that information?
    3. Are lambda functions more than just nameless functions?

    解决方案

    From the syntax link you gave, the capture list "defines what from the outside of the lambda should be available inside the function body and how"

    Ordinary functions can use external data in a few ways:

    1. Static fields
    2. Instance fields
    3. Parameters (including reference parameters)
    4. Globals

    Lambda add the ability to have one unnamed function within another. The lambda can then use the values you specify. Unlike ordinary functions, this can include local variables from an outer function.

    As that answer says, you can also specify how you want to capture. awoodland gives a few exampls in another answer. For instance, you can capture one outer variable by reference (like a reference parameter), and all others by value:

    [=, &epsilon]
    

    EDIT:

    It's important to distinguish between the signature and what the lambda uses internally. The signature of a lambda is the ordered list of parameter types, plus the type of the returned value.

    For instance, a unary function takes a single value of a particular type, and returns a value of another type.

    However, internally it can use other values. As a trivial example:

    [x, y](int z) -> int 
    {
       return x + y - z;
    }
    

    The caller of the lambda only knows that it takes an int and returns an int. However, internally it happens to use two other variables by value.

    这篇关于在lambda函数语法中,“捕获列表”服务的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆