The following program does not build in GCC 4.9.2 or clang 3.6:
#include <iostream>
#include <vector>
#include <thread>
/* Non-copyable type */
struct Foo {
Foo() {};
Foo(const Foo &) = delete;
Foo &operator=(const Foo &) = delete;
};
/* Bar depends on Foo */
struct Bar {
Bar(const Foo &) {}
};
template <class T, class... Args>
void my_function(Args &&... args) {
std::vector<T> data;
auto lambda = [&data](Args &&... ts) { data.emplace_back(ts...); };
lambda(std::forward<Args>(args)...); // <-- Compile
std::thread(lambda, std::forward<Args>(args)...); //<--- Does NOT compile
}
int main() {
my_function<Bar>(Foo());
}
This is because Foo is not copyable and forwarding an instance of Foo to an std:thread attempts to copy it (why?). However, if you forward the same instance to the lambda directly it compiles.
I guess, I don't fully understand all the requirements of the std::thread constructor. Any help would be greatly appreciated.
Thanks.
Aucun commentaire:
Enregistrer un commentaire