use*_*020 9 c++ visual-studio c++11 visual-studio-2013
一个简单的例子
class __declspec(dllexport) A
{
public:
vector<unique_ptr<int>> v;
};
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
产生编译错误:
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_root\cl\inc\xutility(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_root\cl\inc\memory(1430): note: see declaration of 'std::unique_ptr<int,std::default_delete<_Ty>>::operator ='
with
[
_Ty=int
]
c:\tools_root\cl\inc\xutility(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_root\cl\inc\vector(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_root\cl\inc\vector(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
]
Run Code Online (Sandbox Code Playgroud)
Rei*_*ica 13
它似乎添加__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;
};
Run Code Online (Sandbox Code Playgroud)
当然,如果你想要复制功能,自己定义它们也会有所帮助.
| 归档时间: |
|
| 查看次数: |
1165 次 |
| 最近记录: |