Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the C++11 <codecvt> header available in the latest GCC?

Tags:

c++

gcc

c++11

After reading Is <codecvt> not a standard header? I am not sure what to do as my Windows version of the codebase uses <codecvt> to convert between wide strings and strings. I currently use GCC 4.7 for Linux version of my code. Is <codecvt> also missing in the latest GCC? What would be a workaround?

BTW, as it's stated here the following code wouldn't work with GCC:

wstring ws = L"hello";
string ns(ws.begin(), ws.end());
like image 497
Michael IV Avatar asked Jul 17 '14 11:07

Michael IV


1 Answers

How about using mbsrtowcs and wcsrtombs? Though they come from C are not very convenient to use with std::string and std::wstring (but you can always make your own convenient C++ functions based on them). Besides, the conversion is performed based on the currently installed C locale. So, your example code snippet can be changed to something like:

std::mbstate_t state = {};
const wchar_t* p = L"hello";
std::string ns(std::wcsrtombs(NULL, &p, 0, &state) + 1);
std::wcsrtombs(&ns[0], &p, ns.size(), &state);
ns.pop_back();
like image 180
Lingxi Avatar answered Nov 04 '22 10:11

Lingxi