为什么`wstring_convert`会抛出range_error?

时间:2017-09-17 09:35:00

标签: c++ linux utf-8 archlinux codecvt

我有C ++代码,可将包含stringALCHEMICAL SYMBOL FOR AQUA REGIA)的转换为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;
}

请注意,我没有使用wstring或任何istream

这给了我std::range_error

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

但是on ideone this code runs without error

使用-std=c++14进行编译时,我收到g ++ 7.2.0和clang 4.0.1的错误。

为什么ideone上没有错误以及为什么会收到此错误?

我有arch linux 4.12.13和locale命令给了我:

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

1 个答案:

答案 0 :(得分:4)

这不是你的转换器应该是什么样的?

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