c++ - 在类中使用时,[this] 和 [&] 在 lambda 捕获列表中是否等效?

标签 c++ lambda

在下面的示例中,在捕获列表中使用 [this] 与在捕获列表中使用按引用捕获 [&] 有什么区别,如图所示?我都试过了,它们产生相同的输出。

#include <iostream>                                                          

class Test {                                                                 
public:                                                                      
   int x = 2;                                                                
   void test1(void) { std::cout << "test1" << std::endl; };                  

    void test_lambda(void) {                                                 
        auto lambda = [&] () {                                               
            std::cout << "x: " << x << " y: " << y << " z: " << z << std::endl;
            this->test1();                                                         
        };                                                                   

        lambda();                                                            
    }                                                                        

protected:                                                                   
   int y = 3;                                                                

private:                                                                     
   int z = 4;                                                                
};                                                                           

int main() {                                                                 
    Test t;                                                                  
    t.test_lambda();                                                         
}  

在 C++ 编程语言中,Stroustrop 说:

    Members are always captured by reference. That is, [this] implies that members are accessed through this rather than copied into the lambda. 

这似乎暗示它们可能是同一个意思。如果是这样的话,我们为什么需要 [this]?

最佳答案

根据 cppreference :

[&] captures all automatic variables used in the body of the lambda by reference and current object by reference if exists

而使用 [this] 将仅捕获 this 指针。

当范围内有自动变量时,这会有所不同,例如:

struct Test {
  void run() {
    int y = 2;

    // all automatic variables are accessible, both local and members
    auto l1 = [&](){ cout << x << " " << y << endl; };
    l1();

    // y is not accessible, x is only because it's a member
    auto l2 = [this]() { cout << this->x << endl; };
    l2();
  }

  int x = 1;
};

int main() {
  Test t;
  t.run();
}

那么,我们为什么需要它?

允许捕获 [this] 与允许捕获任何其他指针相同。

为什么不一直捕获所有自动变量?有几个原因,包括:

  • 封装:有时,您不希望 lambda 熟悉其他值
  • 灵 active :有时,我们需要复制一些值并通过引用传递其中一些值

注意:使用 & 捕获所有自动变量不会引入额外的性能成本,因为编译器仅传递我们在 lambda 内部使用的变量。

关于c++ - 在类中使用时,[this] 和 [&] 在 lambda 捕获列表中是否等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648659/

相关文章:

c++ - 在多个 * 操作后调用析构函数时,重载 * 运算符失败

c++ - 尝试读取二进制文件会清除文件本身

c++ - 从字符串中提取单个单词 C++

c++ - float C++,如何在数组的每个元素末尾添加 "f"

lambda - 在 Racket 中使用纯 lambda 演算和 Church 数字实现斐波那契数列

c++ - 使用指定的初始值设定项进行模板参数推导

c++ - 将 lambda 隐式转换为 boost::function

c++ - initializer_list 返回的生命周期延长

使用模板的 C++ 11 异步编程

c# - 使用 Entity Framework 4 使用 Web Api 返回复杂对象