在这种情况下,我可以忽略C4251警告吗? [英] Could I ignore C4251 warning in this case?

查看:469
本文介绍了在这种情况下,我可以忽略C4251警告吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解为什么在编译代码时出现C4251警告,如此处所述。我的问题是如果可访问的导出类成员是从STL,我们可以忽略C4251警告?我举一个简单的例子来说明我的问题:



dll.h

  #include< iostream> 
#include< string>

using namespace std;

class __declspec(dllexport)HelloWorld
{
public:
string name;
HelloWorld();
HelloWorld(const string& str);

};

dll.cpp

  #includedll.h

HelloWorld :: HelloWorld()
{
name =;
}
HelloWorld :: HelloWorld(const string& str)
{
name = str;
}

我获得的警告信息如下:



警告1警告C4251:'HelloWorld :: name':class'std :: basic_string< _Elem,_Traits,_Ax>'需要有dll接口类'HelloWorld'* \ dll.h的客户使用9

我的问题是:我可以忽略这个警告吗?我如何使用这个库也很简单:

  #includedll.h
#include< ; iostream>
using namespace std;

int main(void)
{
HelloWorld myworld;
myworld.name =汤姆的世界;
cout<< myworld.name<< endl;
return 0;
}


解决方案

应该明白为什么会发生这种情况,以及您可能遇到的问题。



模板在编译时启动。这意味着它可能取决于你编译代码的方式。可能更改代码的最可能的事情是预编译器定义(例如#define _NDEBUG或-D_NDEBUG作为命令行参数)。



如果你的DLL接口包含依赖的对象类型对于在编译时发生的事情,DLL的所有客户端都必须确保这些类型的代码与你的代码一样,否则可能会出错。



编译相同也意味着使用相同的编译器。编译器之间的差异也可以使代码在生成的可执行文件和不同的编译器之间通常使用不同的STL库。



我会举个例子:

  template< typename T> 
class A
{
int m_data;
#ifndef NDEBUG
int m_debugData;
#endif

public:
void func()
{
m_data = 0;
#ifndef NDEBUG
m_debugData = 0;
#endif
}
};

在此示例中,如果您的DLL使用NDEBUG编译,并且使用代码在没有NDEBUG的情况下编译, func()从用户代码可能会导致运行时错误。


I understand why I have C4251 warning when I compile my codes as explained in here. My question is if the accessible export class members are from STL, could we ignore C4251 warning? I give a simple example to illustrate my question:

dll.h

#include <iostream>
#include <string>

using namespace std;

class __declspec(dllexport) HelloWorld
{
public:
  string name;
  HelloWorld();
  HelloWorld(const string &str);

};

dll.cpp

#include "dll.h"

HelloWorld::HelloWorld()
{
  name ="";
}
HelloWorld::HelloWorld(const string &str)
{ 
 name = str;
}

The warning information I have obtained is as followings:

Warning 1   warning C4251: 'HelloWorld::name' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'HelloWorld'    *\dll.h 9

My question is: can I ignore this warning? The way how I use this library is also very simple:

#include "dll.h"
#include <iostream>
using namespace std;

int main(void)
{
  HelloWorld myworld;
  myworld.name = "Tom's world";
  cout<<myworld.name<<endl;
  return 0;
}

解决方案

You can ignore, but you should understand why this happens and what are the problems you may encounter.

A template is initiated at compile time. This means it may depend on the way you compile your code. The most probable thing that can change code is precompiler define (e.g. #define _NDEBUG or -D_NDEBUG as a command line argument).

If your DLL interface includes object types that depends on things that happen at compile time, all the clients of your DLL must ensure that these types are compiled the same in their code as it is in yours, otherwise errors may occur.

Compiled the same also means using the same compiler. Differences between compilers can also make the code be different between the produced executables and different compilers usually use different STL libraries.

I'll give an example:

template <typename T>
class A
{
    int m_data;
#ifndef NDEBUG
    int m_debugData;
#endif

public:
    void func() 
    {
        m_data =0;
#ifndef NDEBUG
        m_debugData = 0;
#endif
    }
};

In this example, if your DLL is compiled with NDEBUG and the using code is compiled without NDEBUG, calling func() from the user code may cause a runtime error.

这篇关于在这种情况下,我可以忽略C4251警告吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆