清除std :: vector需要赋值运算符.为什么?

Pat*_*ick 5 c++ stl

在我的应用程序中,我需要存储一小部分临时数据.在这个临时数据中,我想存储对另一个类的引用,因为它不能是nullptr,我使用引用.

是使用向量来存储数据(我没有太多的数据,所以矢量很好).

填充向量,并迭代它工作正常,但清除向量似乎给出了问题.

这是一些显示问题的简化代码:

class Department
   {
   };

class Person
   {
   public:
      Person (const Department &dept)
      : m_dept(dept)
      , m_salary(1000)
      {}
   private:
      const Department &m_dept;
      double m_salary;
   };

#include <vector>

int main()
{
std::vector<Person> persons;

Department dept1;
Department dept2;

persons.push_back (Person(dept1));
persons.push_back (Person(dept2));

persons.clear();
}
Run Code Online (Sandbox Code Playgroud)

除了最后一个声明之外,所有内容都能完美地编译和工作.清除向量会显示此错误消息(Visual Studio 2010):

C:\DevStudio\Vs2010\VC\INCLUDE\xutility(2526) : error C2582: 'operator =' function is unavailable in 'Person'
        C:\DevStudio\Vs2010\VC\INCLUDE\xutility(2547) : see reference to function template  nstantiation '_OutIt std::_Move<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
        with
        [
            _OutIt=Person *,
            _InIt=Person *
        ]
        C:\DevStudio\Vs2010\VC\INCLUDE\vector(1207) : see reference to function template instantiation '_OutIt std::_Move<Person*,Person*>(_InIt,_InIt,_OutIt)' being compiled
        with
        [
            _OutIt=Person *,
            _InIt=Person *
        ]
        C:\DevStudio\Vs2010\VC\INCLUDE\vector(1190) : while compiling class template member function 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>)'
        with
        [
            _Myvec=std::_Vector_val<Person,std::allocator<Person>>,
            _Ty=Person
        ]
        test.cpp(21) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
        with
        [
            _Ty=Person
        ]
Run Code Online (Sandbox Code Playgroud)

原因似乎是std :: vector :: clear的实现调用了std :: vector :: erase,它调用_Move方法,这似乎需要赋值运算符.

为什么简单的方法不能简单:

  • 为向量中的所有元素调用析构函数
  • 将矢量大小设置为零

有趣的是,当我使用std :: list而不是std :: vector时,代码可以正确编译.

为什么是这样?

其他编译器也有这个问题吗?

Seb*_*edl 11

放入向量中的任何类都需要一个复制赋值运算符(或者至少是C++ 11中的移动赋值运算符).当您实际收到错误时,这只是一个实施质量问题.