C++ 在 lambda 函数中使用此指针和绑定

C++ using this pointer with bind inside lambda function

我正在使用 std::bind 在 lambda 函数中绑定一个成员函数,代码如下:

class A {
...
...

public:
   foo(function<void()> f) {

   }
...
...
};

class B {
...
...
A a;
public:
   B_function_1(){
      a.foo([](){
         some_other_function(bind(&B::B_function_2, this, _1,_2));
   }
...
private:
   B_function_2(arg1, arg2) {
   ...
   }
};

我的问题是当我尝试编译时出现此错误:

error: ‘this’ was not captured for this lambda function

在我的例子中,这是指当前的 class (class B)。 所以,我的问题是这里的问题是什么?我错过了什么?

谢谢。

要在 lambda 中捕获 this 指针,您可以使用 a.foo([this]()

[this]按值捕获this指针 [&] 通过引用捕获 lambda 主体中使用的所有自动变量 odr

来自文档