Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 locale in Visual C++ 2010

I am trying to read a UTF-8 text file in Visual C++ 2010 using only the standard library and not Boost or Windows APIs. I define the locale as:

std::locale utf8_locale(std::locale(), new std::codecvt_utf8<wchar_t>);

but this results in the following compiler error:

error C2661: 'std::locale::facet::operator new' : no overloaded function takes 3 arguments
error C2664: 'std::locale::locale(const char *,std::locale::category)' : cannot convert parameter 1 from 'std::locale' to 'const char *'
like image 589
AMCoded Avatar asked Mar 02 '12 17:03

AMCoded


2 Answers

The error is occures in debug mode when the code is used in the file that micrsoft Visual c++ provided below macro is placed.

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

to get rid of this error the #define new DEBUG_NEW should be commented out or the code should be implemented in another file which does not have the above macro. This bug for Visual c++ 2010 is mentioned here http://connect.microsoft.com/VisualStudio/feedback/details/683483/mfc-c-fails-to-compile-use-of-codecvt-utf8-in-debug-configuration

like image 149
AMCoded Avatar answered Sep 20 '22 15:09

AMCoded


FYI, another work around is to just write

std::locale utf8(std::locale(), ::new std::codecvt_utf8<wchar_t>);

That will force the compiler to use the global new, instead of the locale new

like image 39
Alon Fital Avatar answered Sep 22 '22 15:09

Alon Fital