在 MSVC++2010 中禁用警告

发布于 2024-11-17 03:57:41 字数 985 浏览 3 评论 0原文

我有以下代码:

/** 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

预谋 2024-11-24 03:57:41

要消除此警告,您需要在包含该函数的头文件周围添加 #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;
}

除此之外,我建议进行正确的转换。例如,看看 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.

#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 ICU for example or at least use the function from standard. E.g. mbstowcs

时光瘦了 2024-11-24 03:57:41

当我想这样做时,我只需将 #pragma warning(disable, 2422) 放在有问题的 .cpp 文件的顶部,#include 之后。但如果我是你,我会尝试解决这个警告,而不是将其掩盖起来。抛弃常量可能会导致未定义的行为。

要解决警告,请尝试这样的操作(我们在解决方案中使用此函数):

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;
 }

调整它以接收 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):

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*

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

默嘫て 2024-11-24 03:57:41

尝试将您的函数更改为如下所示(没有可用的 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;
}

受 dario_ramos 答案启发)

当您在 Windows 上时,您甚至可以使用 Windows API 函数 WideCharToMultiByte,它基本上具有相同的功能,但允许您指定目标编码。

Try to change your function to something like this (no C++ compiler available to check if it compiles):

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)

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.

陌上青苔 2024-11-24 03:57:41

仅当在包含字符串 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文