【发布时间】:2013-09-29 14:28:48
【问题描述】:
是否可以使用 std::async 调用使用 std::bind 创建的函数对象。以下代码编译失败:
#include <iostream>
#include <future>
#include <functional>
using namespace std;
class Adder {
public:
int add(int x, int y) {
return x + y;
}
};
int main(int argc, const char * argv[])
{
Adder a;
function<int(int, int)> sumFunc = bind(&Adder::add, &a, 1, 2);
auto future = async(launch::async, sumFunc); // ERROR HERE
cout << future.get();
return 0;
}
错误是:
对“异步”的调用没有匹配的函数: 候选模板被忽略:替换失败 [with Fp = std::_1::function &, Args = ]: no type named 'type' in 'std:: _1::__invoke_of,>
是不能对 std::function 对象使用异步还是我做错了什么?
(这是使用 Xcode 5 和 Apple LLVM 5.0 编译器编译的)
【问题讨论】:
-
async从哪里得到函子参数?尝试传递std::function<void()>。
标签: c++ c++11 std-function stdasync