Search code examples
c++gccmultibyte-characters

C++ Use of wstring_convert on Linux


I would like to be able to convert text read from a file into multibyte characters. I have the following C++ code on Windows that is working for me. When I try to compile the code on Linux though it is failing.

#include <locale>
....
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utfconv;
std::string line;
while (std::getline(infile, line))
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utfconv;
    std::wstring widestr = utfconv.from_bytes(line);

This throws the following errors:

wstring_convert is not a member of std
codecvt_utf8_utf16 is not a member of std
expected primary-expression before wchar_t

I'm using GCC Red Hat 4.4.7-4. Based on what I read, I've imported 'locale', but it still can't find it.

If wstring_convert is not available, is there something equivalent that I can do?


Solution

  • Most likely your standard is not set properly. std::wstring_convert was first introduced in C++11 and deprecated in C++17, so you need to add the compiler flag -std=c++11 or -std=c++14.

    EDIT: Just saw the GCC version you're using. It's way out of date. Everything should work fine if you download GCC 4.9.x or above