给定std::thread的 vector

std::vector<std::thread> vThreads;

vThreads.push_back(std::thread([]() {
        std::thread _t;
        _t.detach();
    }));

for (int i=0; i < vThreads.size(); i++)
{
    std::thread _t = (std::thread) vThreads.at(i); <!-- ERROR


}

尝试在我得到的for loop中执行转换:Calling a private constructor of class 'std::__1::thread'
但是看着http://www.cplusplus.com/reference/thread/thread/我没有看到任何标记为私有(private)的构造函数。

我会误解什么?

最佳答案

std::thread不可复制。根据您缩进for循环中要执行的操作,您想要

std::thread& _t = vThreads.at(i);

要么
std::thread _t = std::move(vThreads.at(i));

我建议使用基于范围的for遍历vector
for(auto&& t : vThreads) {
  // t is a reference to an std::thread object
}

关于c++ - 转换为std::thread states calling a private constructor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24845874/

10-11 19:37
查看更多