【发布时间】:2014-03-30 23:18:26
【问题描述】:
我正在尝试使用 unique_ptr 来管理我的内存,而 VS2013 似乎在我认为不应该的时候给我带来了麻烦。
似乎编译器出于某种原因试图访问已删除的复制构造函数,而实际上它确实没有理由这样做。
这是我的一门课的样子:
class Mesh
{
public:
Mesh(oglplus::Program* program, const std::vector<Vertex>& vertices,
const std::vector<GLuint>& indices);
void draw();
private:
const oglplus::Program* _program;
std::vector<Vertex> _vertices;
std::vector<GLuint> _indices;
oglplus::Buffer _faceBuffer;
oglplus::Buffer _vertexBuffer;
oglplus::VertexArray _vao;
};
class Model
{
public:
Model(std::string filename, oglplus::Program* program);
void draw();
private:
const oglplus::Program* _program;
std::vector<std::unique_ptr<Mesh>> _meshes;
};
问题在于线路
std::vector<std::unique_ptr<Mesh>> _meshes;
它开始喷出类似的东西
2>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(593): error C2280: 'std::unique_ptr<Model::Mesh,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function
2> with
2> [
2> _Ty=Model::Mesh
2> ]
2> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1486) : see declaration of 'std::unique_ptr<Model::Mesh,std::default_delete<_Ty>>::unique_ptr'
2> with
2> [
2> _Ty=Model::Mesh
2> ]
2> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(592) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
2> with
2> [
2> _Ty=std::unique_ptr<Model::Mesh,std::default_delete<Model::Mesh>>
2> ]
2> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
2> with
2> [
2> _Ty=std::unique_ptr<Model::Mesh,std::default_delete<Model::Mesh>>
2> ]
2> c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
2> with
2> [
2> _Ty=std::unique_ptr<Model::Mesh,std::default_delete<Model::Mesh>>
2> ]
2> c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
2> with
2> [
2> _Alloc=std::allocator<std::unique_ptr<Model::Mesh,std::default_delete<Model::Mesh>>>
2> ]
2> c:\users\vitali\projects\3d-stg\source\model\model.hpp(45) : see reference to class template instantiation 'std::vector<std::unique_ptr<Model::Mesh,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
2> with
2> [
2> _Ty=Model::Mesh
2> ]
2> model.cpp
我没有使用std::vector::resize() 或类似的东西(事实上我注释掉了我的 _meshes 向量的所有用途,甚至尝试完全注释掉实现无济于事),所以我不明白为什么编译器给我带来了问题。
有人有什么想法吗?
感谢 Preetish Kakkar 发现问题。事实证明,编译器使用 Mesh 类的复制构造函数和 operator= 隐式生成函数是一个问题,迫使编译器尝试使用已删除的函数。
【问题讨论】:
-
尝试为
Mesh实现移动构造函数。 AFAIK,VS2013 不会自动生成移动构造函数/移动赋值运算符。 -
是的,但我的理解是 std::vector 不使用移动构造函数,因此不应该出现错误。如果我正确读取错误,则它与复制构造函数有关。如果不是 std::vector<:unique_ptr>> _meshes;我做 std::unique_ptr
_mesh,一切都编译得很好。另外,如果我只使用常规指针 std::vector _meshes,一切都编译得很好。 -
-std=c++11 不适用于 VS afaik
-
这是 VS 2013 中的一个已知 bug。它被 Microsoft 归类为活动 bug。见下文:connect.microsoft.com/VisualStudio/feedback/details/858243/…
-
该错误仅适用于 C++/CLI。
标签: c++ visual-c++ visual-studio-2013 unique-ptr oglplus