如何在 Visual Studio 的 dll 导出类中使用唯一指针的向量

How to use a vector of unique pointers in a dll exported class with Visual Studio

本文关键字:唯一 向量 指针 Visual Studio dll      更新时间:2023-10-16

一个简单的例子

class __declspec(dllexport) A
{
public:
    vector<unique_ptr<int>> v;
};

VS2013编译中错误删除了unique_ptr的复制构造函数。如果我删除__declspec(dllexport),那就好了。如果我只使用unique_ptr<int> v,那也很好。这是编译器错误吗?有什么方法可以解决它吗?谢谢。

您可以使用以下完整代码在 http://webcompiler.cloudapp.net/上尝试

#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class __declspec(dllexport) A
{
public:
    vector<unique_ptr<int>> v;
};
int main()
{
   cout << "Hello World" << endl; 
}

产生编译器错误:

Compiled with /EHsc /nologo /W4 /c
main.cpp
main.cpp(9): warning C4251: 'A::v': class 'std::vector<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' needs to have dll-interface to be used by clients of class 'A'
        with
        [
            _Ty=int
        ]
c:tools_rootclincxutility(2144): error C2280: 'std::unique_ptr<int,std::default_delete<_Ty>> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
        with
        [
            _Ty=int
        ]
c:tools_rootclincmemory(1430): note: see declaration of 'std::unique_ptr<int,std::default_delete<_Ty>>::operator ='
        with
        [
            _Ty=int
        ]
c:tools_rootclincxutility(2165): note: see reference to function template instantiation '_OutIt std::_Copy_impl<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
        with
        [
            _OutIt=std::unique_ptr<int,std::default_delete<int>> *,
            _InIt=std::unique_ptr<int,std::default_delete<int>> *
        ]
c:tools_rootclincvector(973): note: see reference to function template instantiation '_OutIt std::_Copy_impl<std::unique_ptr<int,std::default_delete<_Ty>>,std::unique_ptr<_Ty,std::default_delete<_Ty>>*>(_InIt,_InIt,_OutIt)' being compiled
        with
        [
            _OutIt=std::unique_ptr<int,std::default_delete<int>> *,
            _Ty=int,
            _InIt=std::unique_ptr<int,std::default_delete<int>> *
        ]
c:tools_rootclincvector(956): note: while compiling class template member function 'std::vector<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::operator =(const std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &)'
        with
        [
            _Ty=int
        ]
main.cpp(10): note: see reference to function template instantiation 'std::vector<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::operator =(const std::vector<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &)' being compiled
        with
        [
            _Ty=int
        ]
main.cpp(9): note: see reference to class template instantiation 'std::vector<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
        with
        [
            _Ty=int
        ]

似乎添加__declspec(dllexport)强制编译器定义隐式声明的复制构造函数和复制赋值运算符(通常,这仅在使用它们时才会发生)。这些反过来调用v的复制构造函数/赋值运算符。但是std::vector<T>的复制操作对于不可复制的T(例如 std::unique_ptr)来说是格式不正确的。因此错误。

当成员只是std::unique_ptr 时,不会出现问题,因为它显式删除了复制操作,因此A的默认复制操作也会被删除。

因此,如果您显式删除复制操作,则问题已解决:

class __declspec(dllexport) A
{
public:
    A(const A&) = delete;
    A& operator=(const A&) = delete;
    vector<unique_ptr<int>> v;
};

当然,如果你想要复制功能,自己定义它们也会有所帮助。