vector的push_back错误背后的原因是什么?

时间:2013-01-22 08:23:56

标签: c++

我有一个简单的c ++ std :: vector,在其中,我存储线程如下所示。你能否解释为什么注释“不编译”的行在编译期间显示错误?为什么注释“编译”的行有效?

#include<thread>
#include<vector>

using namespace std;
void abc() {}
int main()
{
   vector<thread> workers;
   workers.push_back(thread(abc)); // compiles
   thread t(abc);
   workers.push_back(t); // does not compile

   return 0;
 }

更新:我在linux上使用g ++ 4.4.6。以下是错误

[jim@cola c++]$ g++ -std=c++0x -pthread -g -Wall t.cpp -o t
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/c++allocator.h:34,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/allocator.h:48,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/string:43,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/locale_classes.h:42,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:43,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ios:43,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:40,
                 from t.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::thread]’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:737:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::thread, _Alloc = std::allocator<std::thread>]’
t.cpp:29:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread:122: error: deleted function ‘std::thread::thread(const std::thread&)’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ext/new_allocator.h:105: error: used here
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/vector:69,
                 from t.cpp:4:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = const std::thread&, _Tp = std::thread, _Alloc = std::allocator<std::thread>]’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:741:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::thread, _Alloc = std::allocator<std::thread>]’
t.cpp:29:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread:122: error: deleted function ‘std::thread::thread(const std::thread&)’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/vector.tcc:314: error: used here

4 个答案:

答案 0 :(得分:13)

您收到错误是因为std::thread 不可复制,并且您正试图将t的副本插入到矢量中。

你能做到这一点的唯一方法就是:

workers.push_back(std::move(t));

然而,这意味着在你这样做之后,t不再代表一个线程(它代表的线程被移动到向量中)。

答案 1 :(得分:2)

这是一个更简洁,更快速的解决方案,既不需要复制也不需要移动:

workers.emplace_back(abc);

答案 2 :(得分:1)

原因是std::thread有一个移动构造函数,但没有复制构造函数。

答案 3 :(得分:1)

由于std::thread不是可复制,您可以将其移至矢量:

   thread t(abc);
   workers.push_back(std::move(t));  

更好的解决方案是将智能指针存储在向量中:

std::vector<std::shared_ptr<std::thread>> workers;

因为当使用lambda时,无法捕获仅移动类型,解决方法是在std::shared_ptr<std::thread>中存储仅移动类型。