back_inserter仅用于移动类型

问题描述:

在下面的代码中,对象队列是不可复制的,但由于std :: mutex是可移动的。

In the following code the object 'queue' is non-copyable, but is movable due to a std::mutex.

std::generate_n(std::back_inserter(thread_pool),
                std::thread::hardware_concurrency,
                [&](){return std::thread(handler(), exiting, queue);});

VC ++ 2012由于互斥体上的私有副本构造函数而无法编译。它无法为队列生成复制构造函数。为什么要尝试复制队列?

VC++2012 is failing to compile due to a private copy constructor on the mutex. It is failing to generate the copy constructor for queue. Why would anything be trying to copy queue? It appears to me that everything is taking it by reference, thus no copies.

都是通过引用而获得的尝试通过将值传递给 std :: thread 构造函数来复制 queue 。如果您要传递引用,请使用包装器: std :: ref(queue)

You are trying to copy queue by passing it by value to the std::thread constructor. If you mean to pass a reference, use a wrapper: std::ref(queue).

真的想将队列移动到 std :: thread ,您需要传递 std: :move(queue)使它成为右值。它仍然无法正常工作,因为VS中的错误

If you really want to move queue into the std::thread, you need to pass std::move(queue) to make it an rvalue. It still won't work though, because of a bug in VS.