导出类时编译器错误

Zac*_*own 5 c++ visual-c++ c++11

我正在使用Visual Studio 2013,我遇到了一个奇怪的问题.当我导出一个类时,它会抛出"试图引用已删除的函数"错误.但是,当未导出类时,它的行为正确.

让我举个例子...

class Foo
{

};

      // note the export
class __declspec(dllexport) Bar
{
    // the following line throws the error
    std::unordered_map<std::string, std::unique_ptr<Foo>> map;
};
Run Code Online (Sandbox Code Playgroud)

现在,如果我删除导出,所以它看起来像以下一样按预期工作.

class Foo
{

};

// note I removed the export
class Bar
{
    // the following line now compiles without any issues
    std::unordered_map<std::string, std::unique_ptr<Foo>> map;
};
Run Code Online (Sandbox Code Playgroud)

现在,这是一个编译器错误还是我显然缺少的其他东西?仅供参考,上述代码适用于GCC或Clang.

Error   2   error C2280: 'std::unique_ptr<Foo,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 592 
Run Code Online (Sandbox Code Playgroud)

Nia*_*all 9

从dll导出类时,编译器显式生成所有特殊成员方法(复制构造函数等,在这种情况下,否则将保持未声明).如您所见,生成的复制构造函数然后在唯一指针上生成无效副本; 因此错误.

我不认为这只是一个错误,我认为它很可能是不受支持的情况的一部分.

您可以尝试在Bar类复制构造函数中显式删除,并检查编译器是否接受它.