简体   繁体   中英

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 *'

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM