在 MSVC++2010 中禁用警告
我有以下代码:
/** 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;
}
我的编译器仍然向我显示警告 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)".
英文:“Conversion of const wchar_t
to char
,可能丢失数据,请参阅刚刚编译的函数模板的实例化参考……”)。
我怎样才能禁用它?
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 still shows me warning 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 …").
How can I disable it?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要消除此警告,您需要在包含该函数的头文件周围添加
#pragma warning...
。在你的情况下,这是 xutility。它被多个其他文件包含。所以会很难找到。但你可以尝试用这种方法。除此之外,我建议进行正确的转换。例如,看看 ICU 或至少使用标准中的函数。例如 mbstowcs
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.Beside of this I would recommend to a correct conversion. Have a look at ICU for example or at least use the function from standard. E.g. mbstowcs
当我想这样做时,我只需将 #pragma warning(disable, 2422) 放在有问题的 .cpp 文件的顶部,#include 之后。但如果我是你,我会尝试解决这个警告,而不是将其掩盖起来。抛弃常量可能会导致未定义的行为。
要解决警告,请尝试这样的操作(我们在解决方案中使用此函数):
调整它以接收 const wstring&,考虑到当您为 wstring() 调用 c_str() 时,您会得到一个 const ,这应该很容易wchar_t*
编辑:现在我再次查看这个,如果您使用 RAII 作为缓冲区局部变量,它可以进一步改进。万一。
编辑:更正代码以考虑字符大小
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.
To solve the warning, try something like this (we use this function in our solution):
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*
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.
EDIT: Corrected code to consider character size
尝试将您的函数更改为如下所示(没有可用的 C++ 编译器来检查它是否编译):(
受 dario_ramos 答案启发)
当您在 Windows 上时,您甚至可以使用 Windows API 函数 WideCharToMultiByte,它基本上具有相同的功能,但允许您指定目标编码。
Try to change your function to something like this (no C++ compiler available to check if it compiles):
(Inspired by dario_ramos answer)
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.
仅当在包含字符串 h 文件之前禁用该警告时,才不会显示该警告。考虑到这种行为的原因,我猜这是模板特定的问题。当包含模板类时,编译器会进行某种预编译。仅当模板实例化时才完成完整编译。看起来 VC++ 编译器保留了预编译阶段的警告设置,并且在实例化之前更改它们没有帮助。
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.