GCC 4.8和char16_t流-错误? [英] GCC 4.8 and char16_t streams - bug?

查看:86
本文介绍了GCC 4.8和char16_t流-错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是libstdc ++错误吗?

Is this a libstdc++ bug?

#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下的输出:

Output, under 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下工作而不改变其签名的方法:

Edit Can someone suggest a way to make this function work under GCC 4.9 without changing its signature:

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之外的任何类型的流.

The standard doesn't require implementations to support streams of any type except char and wchar_t.

[iostreams.limits.pos]

[iostreams.limits.pos]

在第27章的类中,名称为charT的模板参数表示包含charwchar_t以及满足字符要求的任何其他实现定义的字符类型的类型集合的成员可以在其上实例化任何iostream组件的

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一起使用:

Additionally, it doesn't require facets used for extracting integers and floats from streams to work with char16_t either:

[category.numeric]

[category.numeric]

22.4.2子条款中num_putnum_get的成员函数的所有规范仅适用于表81和82(22.3.1.1.1)中要求的专业化,即num_get<char>num_get<wchar_t>num_get<C, InputIterator>num_put<char>num_put<wchar_t>num_put<C,OutputIterator>.

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:

名称为C的模板参数表示包含以下内容的类型的集合 charwchar_t以及满足可在其上实例化任何iostream组件的字符要求的其他任何实现定义的字符类型.

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)和codecvt对于char16_tchar32_t正常工作.

The standard only requires that char_traits (and so basic_string) and codecvt work correctly for char16_t and char32_t.

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

So to make your function work you would basically need to define specializations of all the missing pieces that the library doesn't provide.

这篇关于GCC 4.8和char16_t流-错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆