c++ - __declspec(dllexport) 强制错误的模板覆盖编译错误
问题描述
在创建模板类的子类时,我注意到重载函数出现错误。此编译器错误是正确的,因为其中一个重载使用了副本并且类型不可复制。但是,我没有使用该功能(正确的重载与否)。所以我很惊讶得到这个错误
在搜索了一下并在godbolt中重现之后,罪魁祸首似乎是__declspec(dllexport)。
神螺栓中的复制
删除 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) 显式删除函数: 注意: 在编译类时带有 [ _Ty=int, V=std:: 的模板成员函数 'void Foo>>::update(const V &)'unique_ptr> ] (29): 注意:参见使用 [ _Ty=int ] 编译的类模板实例化 'Foo>>' 的参考
解决方案
推荐阅读
- neo4j - 列表模式的密码查询
- reactjs - 扩展 MUI 组件会导致有关“css”属性丢失的 TS 错误
- git - 如何git添加2个具有相同扩展名但位于不同文件夹中的相同文件
- swiftui - 使 NavigationLink 目标视图执行“onShow”
- python - Grid 和 Pack 同时 :( 我能做些什么来同时使用 pack 或 grid 或者有其他方法吗?
- java - 无论我做什么,我都无法保存相机拍摄的图像。我该怎么做?
- laravel - 如何将分页文本“下一个”“上一个”更改为 laravel 中的图标或箭头
- python - 有没有办法使用 Selenium 在 Python 中找到动态变化的元素的“内部文本”?
- php - 想学习/运行 PHP 单元测试但是意识到我有旧版本的 PHP
- java - 将对话框中的数据插入 Android Studio 上的 Firestore 数据库