简体   繁体   中英

back_inserter for move-only type

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 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.

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) .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM