我有C ++代码,可将包含string
(ALCHEMICAL 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=
答案 0 :(得分:4)
这不是你的转换器应该是什么样的?
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;