__declspec(dllexport) 强制错误模板覆盖编译错误

问题描述

在创建模板类的子类时,我注意到我在重载函数上出错。 此编译器错误是正确的,因为其中一个重载正在使用副本并且类型不可复制。 但是,我没有使用该功能(是否正确重载)。所以我很惊讶收到这个错误

经过一番搜索并在 Godbolt 中重现后,罪魁祸首似乎是 __declspec(dllexport)。
Reproduction in godbolt

删除 declspec 似乎会导致正确的编译。 Godbolt 中的代码

#include <memory>
#include <vector>

using namespace std;

template<class V>
struct Foo{


    void update(const V& v);
    void update(V&& v);

    std::vector<V> values;
};

template<class V>
void Foo<V>::update(const V& v)
{
    values[0] = v;
}

template<class V>
void Foo<V>::update(V&& v)
{
    values[0] = std::move(v);
}

struct __declspec(dllexport) Bar : public Foo<std::unique_ptr<int>>
{

};

int main() 
{    
    Bar f;
    
    auto i = std::make_unique<int>(5);
    //f.update(i);
    //f.update(std::move(i));
}

我的问题主要是,declspec 是如何导致这种行为的? 并且,在模板类或派生类中有什么可以做的吗?

错误日志:

(19): 错误 C2280: 'std::unique_ptr> &std::unique_ptr>::operator =(const std::unique_ptr> &)': 试图 使用 [ 引用已删除函数 _Ty=int ] C:/data/msvc/14.16.27023.1/include\memory(2338): 注意:见声明 'std::unique_ptr>::operator =' 与 [ _Ty=int ] C:/data/msvc/14.16.27023.1/include\memory(2338): 注意:'std::unique_ptr> &std::unique_ptr>::operator =(const std::unique_ptr> &)': 函数是 用 [ 显式删除 _Ty=int ] (18): 注意:在编译类模板成员函数时'void Foo>>::update(const V &)' 和 [ _Ty=int,V=std::unique_ptr> ] (29):注意:参见类模板实例化的参考 'Foo>>' 正在编译 和 [ _Ty=int ]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)