c++linuxutf-8archlinuxcodecvt

Why is `wstring_convert` throwing a range_error?


I have C++ code which converts string that contains 🜆(ALCHEMICAL SYMBOL FOR AQUA REGIA) to u16string:

#include <string>
#include <codecvt>
#include <locale>
using namespace std;

int main() {
    setlocale(LC_ALL, "ru_RU.UTF-8");
    string s = "🜆";

    wstring_convert<codecvt_utf8<char16_t>, char16_t> converter;
    u16string s16 = converter.from_bytes(s);
    return 0;
}

Note, that I don't use wstring or any istream.

This gives me std::range_error:

terminate called after throwing an instance of 'std::range_error'
  what():  wstring_convert::from_bytes

But on ideone this code runs without error.

I receive error with both g++ 7.2.0 and clang 4.0.1, when compiling with -std=c++14.

Why there is no error on ideone and why I receive this error?


I have arch linux 4.12.13 and locale command gives me:

LANG=ru_RU.UTF-8
LC_CTYPE="ru_RU.UTF-8"
...
LC_IDENTIFICATION="ru_RU.UTF-8"
LC_ALL=

Solution

  • Isn't this what your converter should be like?

    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;