6

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());
Community
  • 1
  • 1
Michael IV
  • 11,016
  • 12
  • 92
  • 223
  • 2
    Look [here](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011). – n. m. could be an AI Jul 17 '14 at 12:11
  • The wstring is typedef of basic_string. From the link n.m. provides GCC is only missing codecvt and codecvt. So your code snippet should work. Can you provide more detailed code which doesn't work? – Peng Zhang Jul 20 '14 at 07:20
  • The code above is not what I am trying to run.I am talking about codecvt specific stuff.The code above is not reliable as it is stated in the SO thread and it sounds logical – Michael IV Jul 20 '14 at 08:05

1 Answers1

1

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();
Lingxi
  • 14,579
  • 2
  • 37
  • 93