首页 > 解决方案 > 创建不可复制类型的 std::vector

问题描述

我正在使用 MSVC 2015,我有一个可以移动但不能复制的类型。据我所知,从 C++11 开始,容器类型不必是可复制的。但是,在创建这种类型的 std::vector 时,我从 xmemory 标头中得到了许多编译错误(这表明一堆 std::allocator 成员函数需要复制构造函数)。

我试过写一个最小的测试用例,但编译得很好。现有代码是一大段代码,我无法在此处复制和粘贴。我可以调查哪些可能的事情来弄清楚为什么请求复制构造函数?

根据要求,我收到的错误消息是:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(737): error C2280: 'MyClass::MyClass(const MyClass&)': attempting to reference a deleted function (compiling source file src\MyClass.cpp)
3>  c:\<some path>\src\../inc/MyClass.h(161): note: see declaration of 'MyClass::MyClass' (compiling source file src\MyClass.cpp)
3>  c:\<some path>\../inc/MyClass.h(161): note: see declaration of 'MyClass::MyClass' (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,_Ty>(_Objty *,_Ty &&)' being compiled
3>          with
3>          [
3>              _Ty=MyClass,
3>              _Objty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,_Ty>(_Objty *,_Ty &&)' being compiled
3>          with
3>          [
3>              _Ty=MyClass,
3>              _Objty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(996): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,_Ty>(std::allocator<_Ty> &,_Objty *,_Ty &&)' being compiled
3>          with
3>          [
3>              _Alloc=std::allocator<MyClass>,
3>              _Ty=MyClass,
3>              _Objty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(995): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,_Ty>(std::allocator<_Ty> &,_Objty *,_Ty &&)' being compiled
3>          with
3>          [
3>              _Alloc=std::allocator<MyClass>,
3>              _Ty=MyClass,
3>              _Objty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory(278): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,MyClass>(_Ty *,MyClass &&)' being compiled
3>          with
3>          [
3>              _Ty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory(278): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,MyClass>(_Ty *,MyClass &&)' being compiled
3>          with
3>          [
3>              _Ty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory(305): note: see reference to function template instantiation '_FwdIt *std::_Uninitialized_move_al_unchecked1<_InIt,_FwdIt,std::allocator<_Ty>>(_InIt,_InIt,_FwdIt,std::_Wrap_alloc<std::allocator<_Ty>> &,std::_General_ptr_iterator_tag,std::_Any_tag)' being compiled
3>          with
3>          [
3>              _FwdIt=MyClass *,
3>              _InIt=MyClass *,
3>              _Ty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory(318): note: see reference to function template instantiation '_FwdIt *std::_Uninitialized_move_al_unchecked<_Iter,_Iter,std::allocator<_Ty>>(_InIt,_InIt,_FwdIt,std::_Wrap_alloc<std::allocator<_Ty>> &)' being compiled
3>          with
3>          [
3>              _FwdIt=MyClass *,
3>              _Iter=MyClass *,
3>              _Ty=MyClass,
3>              _InIt=MyClass *
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(1660): note: see reference to function template instantiation '_FwdIt *std::_Uninitialized_move<_Iter,MyClass*,std::allocator<_Ty>>(_InIt,_InIt,_FwdIt,std::_Wrap_alloc<std::allocator<_Ty>> &)' being compiled
3>          with
3>          [
3>              _FwdIt=MyClass *,
3>              _Iter=MyClass *,
3>              _Ty=MyClass,
3>              _InIt=MyClass *
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(1605): note: see reference to function template instantiation 'MyClass *std::vector<MyClass,std::allocator<_Ty>>::_Umove<MyClass*>(_Iter,_Iter,MyClass *)' being compiled
3>          with
3>          [
3>              _Ty=MyClass,
3>              _Iter=MyClass *
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(1605): note: see reference to function template instantiation 'MyClass *std::vector<MyClass,std::allocator<_Ty>>::_Umove<MyClass*>(_Iter,_Iter,MyClass *)' being compiled
3>          with
3>          [
3>              _Ty=MyClass,
3>              _Iter=MyClass *
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(1601): note: while compiling class template member function 'void std::vector<MyClass,std::allocator<_Ty>>::_Reallocate(unsigned __int64)'
3>          with
3>          [
3>              _Ty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
3>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(1631): note: see reference to function template instantiation 'void std::vector<MyClass,std::allocator<_Ty>>::_Reallocate(unsigned __int64)' being compiled
3>          with
3>          [
3>              _Ty=MyClass
3>          ] (compiling source file src\MyClass.cpp)
src\MyClass.cpp(143): note: see reference to class template instantiation 'std::vector<MyClass,std::allocator<_Ty>>' being compiled
3>          with
3>          [
3>              _Ty=MyClass
3>          ]

但是,如果我使用已删除的复制构造函数(默认移动构造函数)创建一个最小类,我可以毫无问题地创建此类的 std::vector 和 emplace_back。(我没有在任何地方使用 push_back,而只是 emplace_back)。

该错误是针对我尝试将适当的构造函数类型 emplace_back 放入此向量的行触发的。

第 143 行是:vec.emplace_back();

标签: c++11visual-studio-2015stdvector

解决方案


推荐阅读