当我们在 VC 中编译代码时,我们会遇到一些 C4251 警告。在我的例子中,大部分警告来自于 STL 是 _declspec(dllexport)
类的成员这一事实。为了深入了解警告,我计划创建一个可以激发警告的类。我试过以下情况,都没有给出C4251警告,想知道能否给出一个简单的例子。谢谢!
我尝试过的案例:
class _declspec(dllexport) ClassTest
{
public:
ExportClass<int> myexport;
ClassUse myuse;
int i;
};
案例一:
template <typename T>
class ExportClass
{
public:
T a;
T b;
};
案例二:
class ClassUse
{
public:
int counts;
float f_value;
std::vector<int> abc;
void get_func();
template <typename T>
T ouput_value()
{
return static_cast<T>(3);
};
template<class T>
int connected()
{
T abc;
return 3;
}
};
编辑:
我想明确指出警告不会来自调用 STL。
最佳答案
你应该考虑MSDN在这种情况下:
Compiler Warning (level 1) C4251
'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
这可能发生在什么时候
// C4251.cpp
// compile with: /EHsc /MTd /W2 /c
#include <vector>
using namespace std;
class Node;
class __declspec(dllimport) VecWrapper : vector<Node *> {}; // C4251
关于c++ - 我如何创建一个可以给我 C4251 警告的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17144954/