[英]std::async with class member function
我對std::async
有點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();
}
};
您應該能夠使用 std::bind 創建一個可調用的函數對象:
f = std::async(std::launch::async, std::bind(&Test::test, this));
在您的示例中, &Test::test 不是一個采用Test
類型參數的函數,正如std::async
所期望的那樣。 相反,它是成員函數,必須以不同的方式調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.