0

I'm trying to compile a code similar to the following one, it is part of a library dll which I'm trying to compile with MS Visual Compiler :

#include <iostream>
#include <map>
#include <string>
#include <memory>

struct B {

};

template <typename T>
class TemplateClass {
public:
    void print() {
        std::cout << "Hello, world!\n";
    }

private:
    std::map<std::string, std::unique_ptr<T>> m_map;
};

template class __declspec(dllexport) TemplateClass<B>;

int main()
{
    TemplateClass<B> test;
    test.print();
    return 0;
}

The compilation fails with the following error :

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\xmemory0(655): error C2280: 'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)': attempting to reference a deleted function
        with
        [
            _Kty=std::string,
            _Ty=std::unique_ptr<B,std::default_delete<B>>
        ]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\utility(96): note: see declaration of 'std::pair<const _Kty,_Ty>::pair'
        with
        [
            _Kty=std::string,
            _Ty=std::unique_ptr<B,std::default_delete<B>>
        ]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\xmemory0(773): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,_Ty&>(_Objty *,_Ty &)' being compiled
        with
        [
            _Other=std::_Tree_node<std::pair<const std::string,std::unique_ptr<B,std::default_delete<B>>>,void *>,
            _Objty=std::pair<const std::string,std::unique_ptr<B,std::default_delete<B>>>,
            _Ty=std::pair<const std::string,std::unique_ptr<B,std::default_delete<B>>>
        ]
...

I cannot find out what's wrong with this simple code. If I remove __declspec(dllexport), it compiles and executes perfectly.

Thank you in advance for your help.

sela
  • 1
  • 1
  • Just as a wild guess, maybe you can either export the template class itself or only a typedef to it. Not sure though. – Thomas Lang Apr 09 '20 at 20:11
  • Exporting the class forces the generation of every code, and not only called functions. The copy constructor and affectation operator are the cause of the this error because of the std::unique_ptr in the map. I solved my the problem by following [this solution](https://stackoverflow.com/questions/17614172/c-template-singletons-in-a-dll/17750734#17750734) – sela Apr 10 '20 at 12:43
  • I see, thank your for stating the solution you found. – Thomas Lang Apr 10 '20 at 12:52

0 Answers0