back_inserter仅用于移动类型 [英] back_inserter for move-only type

查看:135
本文介绍了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.

这篇关于back_inserter仅用于移动类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆