1
我正在使用simple multithreaded library来尝试理解基本知识。在多线程库中转换未来
的lib中有一个排队功能,看起来像这样:
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
并存储的功能是这样的:
std::queue< std::function<void()> > tasks;
有关于如何通过lambda表达式的例子,但我想传递成员变量。所以我创建了一个小对象:
Threadpool tp(4);
class myClass{
vector<std::future<int>> res;
public:
int compute(int i){
return 2 * i;
};
void update(){
for(int i = 0; i < 10; i++){
res.emplace_back(
// C2664 - 'std::future<int>::future(const std::future<int> &)':
// cannot convert argument 1 from 'std::future<_Rx>' to 'std::future<int> &&'
tp.enqueue(std::bind(&myClass::compute, this), i)
);
};
}
};
创建无参数函数的工作原理。但是,即使在网站上的例子拉姆达可与传递的参数:
tp.enqueue([](int answer) { return answer; }, 42);
什么我没有得到吗?
另外一个侧面的问题:当涉及到标准的功能是什么用的优势:
auto fn(...) -> return type
代替:
return type fn(...)
缺少'bind'的占位符:标准::绑定(&MyClass的::计算,此,/ *的std ::占位符:: */_ 1)' – Jarod42
后返回'类型允许在'decltype'中使用参数,并允许使用范围类型(所以'auto C :: foo() - > iterator',而不是'typename C :: iterator') – Jarod42
谢谢你的答案,缺失的占位符是问题。 – val