如何在 Visual Studio 的 dll 导出类中使用唯一指针的向量
How to use a vector of unique pointers in a dll exported class with Visual Studio
一个简单的例子
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;
};
当然,如果你想要复制功能,自己定义它们也会有所帮助。
相关文章:
- 计算排序向量的向量中唯一值的计数
- 如何更改唯一指针向量的可见性
- 使用 RTTI 克隆唯一指针的向量
- C++模板功能检查唯一向量
- C++唯一指针的向量
- 从数组中删除非唯一值、保持顺序和不使用向量的最佳方法?
- tbb:current_unordered_map()-向量中每个唯一元素的ID
- 初始化 C++14 中唯一指针向量的正确方法
- 对唯一 ptr 无效读取的引用向量
- 在创建对象向量时,不为每个对象唯一调用默认对象构造函数
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- 如何从向量正确返回唯一的_ptr
- C 移动语义:将唯一_ptr引用的矩阵附加到向量
- 在迭代过程中删除向量的唯一元素?
- 具有随机数的唯一向量
- 检查每行/列中的值在向量中是否唯一
- 如何在满足谓词的向量中获得唯一PTR的子集
- 将唯一的_ptr插入向量为一对
- 采用向量中包含的唯一指针的 Lambda 表达式
- 找到唯一向量的索引和逆映射