我正在尝试创建一个std :: thread的形式,它将一个包装器放在线程中执行的代码中.不幸的是,由于我对rvalues和Function我试图通过的模板类型的理解不充分,我无法将其编译.这是我的代码:
#include <vector>
#include <thread>
#include <utility>
void Simple2(int a, int b) {}
template <typename Function, typename... Args>
void Wrapper(Function&& f, Args&&... a) {
f(std::forward<Args>(a)...);
}
class Pool {
public:
template <typename Function, typename... Args>
void Binder(Function&& f, Args&&... a) {
std::thread t(Wrapper<Function, Args...>,
std::forward<Function>(f), std::forward<Args>(a)...);
}
};
int main() {
Wrapper(Simple2, 3, 4); // Works
Pool pool;
pool.Binder(Simple2, 3, 4); // Doesn't compile
}
Run Code Online (Sandbox Code Playgroud)
这里看起来很重要的Clang3.0输出是:
/usr/include/c++/4.6/functional:1286:9: error: non-const lvalue reference to type 'void (int, int)' …Run Code Online (Sandbox Code Playgroud)