具有类成员功能的std :: async

std::async with class member function

本文关键字:std async 功能 成员      更新时间:2023-10-16


我是std::async的新手,我有问题了解它的工作原理。我看到了如何将成员功能传递到网络某个地方的std::async的示例,但似乎没有编译。这里有什么问题?没有Lambda,该怎么办?

class Test {
  std::future<void> f;
  void test() {}
  void runTest() {
    // ERROR no instance overloaded matches argument list
    f = std::async(std::launch::async, &Test::test, this); 
    // OK...
    f = std::async(std::launch::async, [this] {test();}); 
  }
  void testRes() {
    f.get();
  }
};
f = std::async(std::launch::async, std::bind(&Test::test, this));

在您的示例中&amp; test :: test并不是std::async所期望的Test类型参数的函数。相反,它是成员函数,必须以不同的方式调用。