C++ unique_ptr & vector,尝试访问已删除的函数,Visual Studio 2013

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22751203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:11:52  来源:igfitidea点击:

unique_ptr & vector, trying to access deleted function, Visual Studio 2013

c++visual-c++visual-studio-2013unique-ptroglplus

提问by Vitali

I am trying to use unique_ptr to manage my memory, and VS2013 seems to be giving me trouble when I think it shouldn't be.

我正在尝试使用 unique_ptr 来管理我的内存,而 VS2013 在我认为不应该时似乎给我带来了麻烦。

It would seem that the compiler is for whatever reason trying to access a deleted copy constructor when it really should have no reason to do so.

似乎编译器出于某种原因试图访问已删除的复制构造函数,而实际上它没有理由这样做。

This is what one of my classes looks like:

这是我的一堂课的样子:

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;
};

The issue is with the line

问题出在线路上

 std::vector<std::unique_ptr<Mesh>> _meshes;

it starts spewing things like

它开始喷出类似的东西

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\projectsd-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

I am not using std::vector::resize()or anything like that (in-fact I commented out all uses of my _meshes vector, and even tried commenting the implementation out completely to no avail), so I don't understand why the compiler is giving me issues.

我没有使用std::vector::resize()或类似的东西(实际上我注释掉了我的 _meshes 向量的所有用途,甚至尝试完全注释掉实现无济于事),所以我不明白为什么编译器给我带来问题。

Does anybody have any ideas?

有人有任何想法吗?



Thank you Preetish Kakkar for finding the issue. It turns out it was an issue with compiler generated functions implicitly using the copy constructor and operator= of the Mesh class forcing the compiler to try and use deleted functions.

感谢 Preetish Kakkar 发现问题。事实证明,这是编译器生成的函数隐式使用 Mesh 类的复制构造函数和 operator= 的问题,迫使编译器尝试使用已删除的函数。

采纳答案by Blackhole

I reproduced your problem, below is sample code.

我重现了您的问题,以下是示例代码。

#include <vector>
#include <memory>

class Mesh
{
public: 
    Mesh() {}
    void draw() {}
private:
};

class Model
{
public:
    Model() {}
    void draw() {}
private:
    typedef std::unique_ptr<Mesh> MeshUniquePtr;
    std::vector<MeshUniquePtr> _meshes;
};


int _tmain(int argc, _TCHAR* argv[])
{
    Model m;
    Model m1;
    m = m1; // causes error as you can't copy unique ptr

    return 0;
}

The problem is at some point you are trying to copy two model object which can't be done as unique_ptr is not copyable.

问题是在某些时候您试图复制两个模型对象,这是无法完成的,因为 unique_ptr 不可复制。