在MSVC ++ 2010中禁用警告

问题描述 投票:12回答:4

我有以下代码:

/** 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)".

(英文:“const wchar_t转换为char,可能丢失数据,请参阅刚刚编译好的函数模板的实例化...”)。

我该怎么禁用它?!

c++ visual-studio-2010 visual-c++ warnings
4个回答
16
投票

好了摆脱这个警告你需要在包含该功能的头文件周围添加#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


5
投票

当我想这样做时,我只是在#include之后将#pragma warning(disable,2422)放在有问题的.cpp文件的顶部。但如果我是你,我会尝试解决警告而不是把它扫到地毯下。抛弃constness可能会导致未定义的行为。

要解决警告,请尝试这样的事情(我们在解决方案中使用此功能):

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作为缓冲区局部变量,它可以进一步改进。以防万一。

编辑:更正代码以考虑字符大小


2
投票

尝试将您的函数更改为类似的东西(没有可用的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,它基本上做同样的事情,但允许你指定目标编码。


1
投票

仅在包含字符串h文件之前禁用警告时才会显示警告。考虑到这种行为的原因,我想这是模板特定的问题。当包含模板类时,编译器会进行预编译。仅在实例化模板时才进行完全编译。看起来VC ++编译器会在预编译阶段保留警告设置,并且在实例化之前更改它们没有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.