C++ lambda 捕获 this 与通过引用捕获

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33575563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:17:31  来源:igfitidea点击:

C++ lambda capture this vs capture by reference

c++c++11lambdastd-function

提问by vikky.rk

If I need to generate a lambda that calls a member function, should I capture by reference or capture 'this'? My understanding is that '&' captures only the variables used, but 'this' captures all member variable. So better to use '&'?

如果我需要生成一个调用成员函数的 lambda,我应该通过引用捕获还是捕获“this”?我的理解是 '&' 仅捕获使用的变量,但 'this' 捕获所有成员变量。所以最好使用'&'?

class MyClass {
  public:
    int mFunc() {
      // accesses member variables
    }

    std::function<int()> get() {
      //return [this] () { return this->mFunc(); };
      //  or
      //return [&] () { return this->mFunc(); };
    }

  private:
    // member variables
}

采纳答案by Craig Scott

For the specific example you've provided, capturing by thisis what you want. Conceptually, capturing thisby reference doesn't make a whole lot of sense, since you can't change the value of this, you can only use it as a pointer to access members of the class or to get the address of the class instance. Inside your lambda function, if you access things which implicitly use the thispointer (e.g. you call a member function or access a member variable without explicitly using this), the compiler treats it as though you had used thisanyway. You can list multiple captures too, so if you want to capture both members and local variables, you can choose independently whether to capture them by reference or by value. The following article should give you a good grounding in lambdas and captures:

对于您提供的特定示例,this您想要的是捕获。从概念上讲,this通过引用捕获并没有多大意义,因为您无法更改 的值this,您只能将其用作访问类成员或获取类实例地址的指针。在 lambda 函数内部,如果您访问隐式使用this指针的事物(例如,您调用成员函数或访问成员变量而不显式使用this),编译器会将其视为您使用过this反正。您也可以列出多个捕获,因此如果您想捕获成员和局部变量,您可以独立选择是按引用还是按值捕获它们。以下文章应该为您提供 lambdas 和捕获的良好基础:

https://crascit.com/2015/03/01/lambdas-for-lunch/

https://crascit.com/2015/03/01/lambdas-for-lunch/

Also, your example uses std::functionas the return type through which the lambda is passed back to the caller. Be aware that std::functionisn't always as cheap as you may think, so if you are able to use a lambda directly rather than having to wrap it in a std::function, it will likely be more efficient. The following article, while not directly related to your original question, may still give you some useful material relating to lambdas and std::function(see the section An alternative way to store the function object, but the article in general may be of interest):

此外,您的示例std::function用作将 lambda 传递回调用者的返回类型。请注意,这std::function并不总是像您想象的那么便宜,因此如果您能够直接使用 lambda 而不是必须将其包装在 a 中std::function,它可能会更有效。以下文章虽然与您的原始问题没有直接关系,但仍可能为您提供一些与 lambdas 相关的有用材料,并且std::function(请参阅存储函数对象的替代方法部分,但一般而言,这篇文章可能会引起您的兴趣):

https://crascit.com/2015/06/03/on-leaving-scope-part-2/

https://crascit.com/2015/06/03/on-leaving-scope-part-2/

回答by skypHyman

Hereis a good explanation of what &, thisand the others indicate when used in the capture list.

这里很好地解释了 what &this其他人在捕获列表中使用时表示。

In your case, assuming that all what you have to do is calling a member function of the instance that is actually referenced by the thisof the method that is currently executing, put thisin your capture list should be enough.

在您的情况下,假设您所要做的就是调用this当前正在执行的方法实际引用的实例的成员函数,那么放入this捕获列表就足够了。

回答by R Sahu

Capturing thisand capturing by reference are two orthogonal concepts. You can use one, both, or none. It doesn't make sense to capture thisby reference but you can capture other variables by reference while capturing thisby value.

捕获this和通过引用捕获是两个正交的概念。您可以使用一个、两个或一个都不使用。this按引用捕获没有意义,但您可以在this按值捕获的同时通过引用捕获其他变量。