GCC 4.8 和 char16_t 流 - 错误?

GCC 4.8 and char16_t streams - bug?

这是 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);

标准不要求实现支持任何类型的流,除了 charwchar_t

[iostreams.limits.pos]

In the classes of Clause 27, a template parameter with name charT represents a member of the set of types containing char, wchar_t, and any other implementation-defined character types that satisfy the requirements for a character on which any of the iostream components can be instantiated.

此外,它不需要用于从流中提取整数和浮点数的方面来与 char16_t 一起工作:

[category.numeric]

All specifications of member functions for num_put and num_get in the subclauses of 22.4.2 only apply to the specializations required in Tables 81 and 82 (22.3.1.1.1), namely num_get<char>, num_get<wchar_t>, num_get<C, InputIterator>, num_put<char>, num_put<wchar_t>, and num_put<C,OutputIterator>.

其中 C:

A template parameter with name C represents the set of types containing char, wchar_t, and any other implementation-defined character types that satisfy the requirements for a character on which any of the iostream components can be instantiated.

标准只要求 char_traits(因此 basic_string)和 codecvtchar16_tchar32_t 正确工作。

因此,要使您的函数正常工作,您基本上需要定义库未提供的所有缺失部分的专业化。