9

A simple example

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

Error in VS2013 compilation for deleted copy constructor of unique_ptr. If I remove __declspec(dllexport), it is fine. If I use only unique_ptr<int> v, it is fine too. Is this a compiler bug? Any way to work around it? Thanks.

You may try it on http://webcompiler.cloudapp.net/ with the following complete code

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

yielding the compiler error:

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
        ]
8
  • 1
    Clear problem description, compiler output, MCVE - if only all SO questions looked like this! Commented Apr 10, 2015 at 15:46
  • 3
    If I might suggest: Just don't do this. What do you perceive as an advantage of putting this class in a DLL instead of just linking it? The main advantages of DLLs (in particular, reusability) no longer apply when you export classes.
    – Ben Voigt
    Commented Apr 10, 2015 at 16:11
  • 1
    I agree with Ben. If you want to have the benefits of DLLs, you need to export only a C API (or, see the classic "hourglass interface" presented at CppCon 2014). Otherwise, just use static linking (.lib / .a files). Dealing with C++ ABI issues when distributing / reusing DLLs is such a nightmare that their advantages pale in comparison. Commented Apr 10, 2015 at 17:02
  • @BenVoigt It is better not to do it if dll advantages don't show here. Currently, I have a project which makes a dll kernel-computation library (most are classes) for a GUI to use. Is it better to use a static library for the kernel computation? Thanks. Commented Apr 10, 2015 at 17:36
  • 1
    @user1899020: Yes, static linking will work very well for that and without any of the complications associated with DLLs.
    – Ben Voigt
    Commented Apr 10, 2015 at 17:55

1 Answer 1

14

It appears adding __declspec(dllexport) forces the compiler to define the implicitly-declared copy constructor and copy assignment operator (normally, this only happens if they're used). These in turn call the copy constructor/assignment operator of v. But the copy operations of std::vector<T> are ill-formed for a non-copyable T, such as std::unique_ptr. Hence the error.

When the member is just std::unique_ptr, the problem does not occur, because that has copy operations explicitly deleted and so the default copy operations of A become deleted as well.

So the issue is solved if you explicitly delete the copy operations:

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

Of course, if you want copy functionality, defining them yourself would help as well.

4
  • 2
    Not really "wrong", so to speak. vector<unique_ptr<int>>'s copy constructor is not deleted; it merely will fail to instantiate. As a result, A's copy constructor isn't deleted either. Ditto for copy assignment.
    – T.C.
    Commented Apr 10, 2015 at 18:58
  • @T.C. You're right of course. I will re-word the answer. Commented Apr 10, 2015 at 19:00
  • 2
    A(A&&)=default; A& operator=(A&&)=default; would give you move semantics (if your compiler wasn't broken, oh wait, nm) and would remove the need to delete the copy ctors. Commented Apr 10, 2015 at 21:10
  • Since the entire class is being exported, all member functions of the class, whether used or not, need to be instantiated and exported since they may be needed by code that imports the class. Commented May 28, 2019 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.