Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable warning in MSVC++2010

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?!

like image 789
Felix Dombek Avatar asked Jun 22 '11 13:06

Felix Dombek


1 Answers

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

like image 175
mkaes Avatar answered Oct 05 '22 08:10

mkaes