导出DLL接口中包含相同类的STL类成员
Export a STL class member containing the same class in a DLL interface
假设我有一个在STL容器中引用自身的类:
class __declspec(dllexport) Widget {
protected:
std::vector<Widget *> children;
}
如果我说int
的vector
,在类定义之前,我可以有以下内容:
template class __declspec(dllexport) std::vector<int>;
应该可以工作。但如果类还没有定义,我该怎么做呢?前向声明类:
class Widget;
template class __declspec(dllexport) std::vector<Widget *>;
不能消除我从MSVC得到的警告,它是;
warning C4251: 'Widget::children' : class 'std::vector<Widget *,std::allocator<_Ty>>' needs to have dll-interface to be used by clients of class 'Widget'
我相信警告本身的信息在某种程度上是相关的,但我不确定究竟如何进行
从这里开始,这些错误族本质上是噪音;
C4251本质上是噪声,可以消声
- Stephan T. Lavavej(微软c++库的维护者之一).
只要编译器选项在整个项目中是一致的,只是沉默这个警告应该是好的。
作为一个更全面的替代,您可以查看pimpl模式并从类定义中删除std::vector
,因此它不需要从dll中导出。
class __declspec(dllexport) Widget {
protected:
struct Pimpl;
Pimpl* pimpl_;
}
// in the cpp compiled into the dll
struct Widget::Pimpl {
// ...
std::vector<Widget*> children;
// ...
}
MSDN对此有一篇很好的文章,作为c++ 11新特性介绍的一部分。
一般来说,在dll接口中不使用std
类更容易,特别是在需要互操作性的情况下。
警告本身确实是相关的。其中提到的头文件安装在与我正在处理的目录不同的目录中,因此看不到我的更改。修复该警告的正确方法是:
class Widget;
template class __declspec(dllexport) std::vector<Widget *>;
在类定义之前
相关文章:
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- 具有 STL 向量类型成员的类的复制内存
- "迭代器"和"const_iterator"不是 STL 容器的必需成员?
- C++:在子类中扩展静态 STL 容器/映射成员?
- 成员函数不能为集合迭代器和const_iterator的输入重载(但可以为其他 STL 迭代器重载)
- 如何在不在基类中的派生类中访问 STL 类的成员函数?(正文详细说明)
- 打印/修改类对象的特定成员变量,其类定义列表 (STL) 包含的元素类型
- 更新成员集合使用STL给定输入收集
- 为什么每个STL容器都具有将其定义为成员函数的交换函数
- 为什么STL"空"成员函数不叫"isEmpty"?
- 没有stl和动态内存分配的成员函数类
- CPPrestsdk 处理程序类在使用 STL 容器作为成员时崩溃
- 简单,但找不到:使用类型类的 STL 队列的成员变量的语法
- 如何制作特定类的成员函数或变量(如 STL 容器)的别名
- 如何将模板对象存储在STL容器和成员功能调用中
- STL 链表::如何访问成员
- 使用 STL 查找列表中成员的最小值<MyStruct>
- 通过类构造函数初始化成员变量 STL 向量
- 复制包含STL成员(如vector)的C++结构的最佳方法是什么
- PIMPL:导出具有单个STL成员的类(std::unique_ptr)