代码之家  ›  专栏  ›  技术社区  ›  Sonicsmooth

将std::vector<unique\u ptr<…>>声明为“dllexport”类中的成员变量

c++
  •  0
  • Sonicsmooth  · 技术社区  · 6 年前

    一个简单的例子

    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_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
            ]
    
    0 回复  |  直到 11 年前
        1
  •  13
  •   Angew is no longer proud of SO    11 年前

    它似乎在增加 __declspec(dllexport) 强制编译器定义隐式声明的复制构造函数和复制赋值运算符(通常,只有在使用它们时才会发生这种情况)。它们反过来调用的复制构造函数/赋值运算符 v std::vector<T> 不可复制的 T ,例如 std::unique_ptr

    A 也会被删除。

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

    当然,如果你 希望 复制功能,自己定义也会有帮助。