C++ 在 MSVC++2010 中禁用警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6440614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable warning in MSVC++2010
提问by Felix Dombek
I have the following code:
我有以下代码:
/** Stupidly copies unicode chars into normal chars. */
std::string wstring2string(__in const std::wstring& s)
{
std::string temp(s.length(), ' ');
#pragma warning(push)
#pragma warning(disable: 4244) // possible loss of data
std::copy(s.begin(), s.end(), temp.begin());
#pragma warning(pop)
return temp;
}
My compiler stillshows me warning C4244:
我的编译器仍然显示警告 C4244:
1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(2144): warning C4244: '=': Konvertierung von 'const wchar_t' in 'char', m?glicher Datenverlust
1> c:\program files\microsoft visual studio 10.0\vc\include\xutility(2165): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_OutIt std::_Copy_impl<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)".
(in English: "Conversion of const wchar_t
to char
, possible loss of data, see reference to instantiation of the just compiled function template …").
(英文:“ const wchar_t
to 的转换char
,可能丢失数据,请参阅对刚刚编译的函数模板的实例化的引用……”)。
How can I disable it?!
我怎样才能禁用它?!
回答by mkaes
Well to get rid of this warning you need to add the #pragma warning...
around your header file that includes the function. In your case this is xutility. Which is included by multiple other files. So it will be difficult to find. But you can try it this way.
那么要摆脱这个警告,您需要在#pragma warning...
包含该函数的头文件周围添加。在您的情况下,这是 xutility。其中包含多个其他文件。所以会很难找到。但是你可以这样试试。
#pragma warning(push)
#pragma warning(disable: 4244) // possible loss of data
#include <xutility>
#pragma warning(pop)
Your includes go here...
std::string wstring2string(__in const std::wstring& s)
{
std::string temp(s.length(), ' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp;
}
Beside of this I would recommend to a correct conversion. Have a look at ICUfor example or at least use the function from standard. E.g. mbstowcs
回答by dario_ramos
When I want to do that, I just put #pragma warning( disable, 2422 ) at the top of the offending .cpp file, after the #include's. But if I were you, I'd try to solve the warning instead of sweeping it under the carpet. Casting away constness might result in undefined behavior.
当我想这样做时,我只是将 #pragma warning( disable, 2422 ) 放在有问题的 .cpp 文件的顶部,在 #include 之后。但如果我是你,我会尝试解决警告而不是把它扫到地毯下。抛弃常量可能会导致未定义的行为。
To solve the warning, try something like this (we use this function in our solution):
要解决警告,请尝试以下操作(我们在解决方案中使用此函数):
string wtoString( const wchar_t *ws ){
size_t bufferSize = (wcslen(ws) + 1) * sizeof(wchar_t);
char * buffer = new char[ bufferSize ];
size_t convertedChars;
wcstombs_s( &convertedChars, buffer, bufferSize, ws, _TRUNCATE);
string result(buffer);
delete[] buffer;
return result;
}
Adapt it to receive a const wstring&, it should be easy considering that when you call c_str() for a wstring(), you get a const wchar_t*
调整它以接收一个 const wstring&,考虑到当你为 wstring() 调用 c_str() 时,你会得到一个 const wchar_t*,这应该很容易
EDIT: Now that I look at this again, it can be further improved if you use RAII for the buffer local variable. Just in case.
编辑:现在我再看一遍,如果您将 RAII 用于缓冲区局部变量,则可以进一步改进。以防万一。
EDIT: Corrected code to consider character size
编辑:更正代码以考虑字符大小
回答by Stephan
Try to change your function to something like this (no C++ compiler available to check if it compiles):
尝试将您的函数更改为这样的(没有可用的 C++ 编译器来检查它是否编译):
std::string wstring2string(__in const std::wstring& s)
{
size_t bufferSize;
// first call to wcstombs_s to get the target buffer size
wcstombs_s(&bufferSize, NULL, 0, ws.c_str(), ws.size());
// create target buffer with required size
char* buffer = new char[bufferSize];
// second call to do the actual conversion
wcstombs_s(&bufferSize, s.c_str(), bufferSize, ws.c_str(), ws.size());
string result(buffer, bufferSize);
delete[] buffer;
return result;
}
(Inspired by dario_ramos answer)
(灵感来自 dario_ramos 答案)
As you are on Windows, you could even use the Windows API function WideCharToMultiByte, which basically does the same but allows you to specify the target encoding.
正如您在 Windows 上一样,您甚至可以使用 Windows API 函数 WideCharToMultiByte,它的作用基本相同,但允许您指定目标编码。
回答by Alex F
The warning is not shown only if it is disabled before including string h-file. Thinking about the reason of this behavior, I guess that this is template-specific issue. When template class is included, compiler makes kind of pre-compilation. Full compilation is done only when template is instantiated. It looks like VC++ compiler keeps warning settings from pre-compilation stage, and changing them before instantiation doesn't help.
只有在包含字符串 h-file 之前被禁用时,才会显示警告。考虑这种行为的原因,我猜这是模板特定的问题。当包含模板类时,编译器会进行某种预编译。只有在实例化模板时才完成完整编译。看起来 VC++ 编译器在预编译阶段保留警告设置,并且在实例化之前更改它们无济于事。