【发布时间】:2015-02-03 08:10:44
【问题描述】:
这是一个 libstdc++ 错误吗?
#include <string>
#include <sstream>
using namespace std;
int main() {
basic_string<char16_t> str(u"0.0");
basic_stringstream<char16_t> sstr(str);
double x = 9;
sstr >> x;
}
输出,在 GCC 4.8 Linux x86_64 下:
$ ./main
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Aborted (core dumped)
编辑有人可以建议一种方法来使这个函数在 GCC 4.9 下工作而不改变其签名:
template<typename T>
T fromString(std::basic_stringstream<char16_t>& stream)
{
T v;
stream >> v;
return v;
}
典型用途是:
std::basic_string<char16_t> string(...);
std::basic_stringstream<char16_t> sstream(string);
double v = fromString<double>(sstream);
【问题讨论】:
-
流不需要支持
char16_t或char32_t,只有char和wchar_t。 -
太棒了。那么,有没有办法使上述工作?
-
解决方法提示:使用 utf-8 流,然后使用 utfcpp 等工具将字符串转换为 utf-16:utfcpp.sourceforge.net
-
事实上它已经完全坏掉了,不是吗?
std::basic_string<char16_t> s; stream >> s;产生同样的异常。
标签: c++ gcc unicode iostream libstdc++