标准C ++输入输出流如何处理intXX_t类型? [英] How standard C++ input output streams have to handle intXX_t types?

查看:125
本文介绍了标准C ++输入输出流如何处理intXX_t类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此问题的答案所有基本整数类型的重载是否足以捕获所有整数?所有基本类型的重载可能无法处理 int8_t 之类的类型 int64_t 等。根据文档的另一面 std :: ostream格式的输出 std ::通过重载所有基本类型来完全实现istream格式的输入。因此,在这样的重载无法处理 int8_t 及其它平台的平台上,C ++流必须如何处理它们?它会编译失败吗?标准库实现者是否必须提供其他未记录的方法?

According to answers to this questions Is overloading on all of the fundamental integer types is sufficient to capture all integers? overloading of all fundamental types may not be able to handle types like int8_t int64_t etc. On another side according to documentation std::ostream formatted output and std::istream formatted input is implemented exactly by overloading all fundamental types. So on platform where int8_t and others could not be handled by such overloading how C++ streams would have to handle them? Would it fail to compile? Would standard library implementors have to provide additional undocumented methods? Something else?

推荐答案

对于整数类型,[istream]定义:

For integer types, [istream] defines:

basic_istream<charT,traits>& operator>>(bool& n);
basic_istream<charT,traits>& operator>>(short& n);
basic_istream<charT,traits>& operator>>(unsigned short& n);
basic_istream<charT,traits>& operator>>(int& n);
basic_istream<charT,traits>& operator>>(unsigned int& n);
basic_istream<charT,traits>& operator>>(long& n);
basic_istream<charT,traits>& operator>>(unsigned long& n);
basic_istream<charT,traits>& operator>>(long long& n);
basic_istream<charT,traits>& operator>>(unsigned long long& n);

任何格式为的代码都是>>如果 x 是不在该列表中的整数类型,则x 可能无法编译。

Any code of the form is >> x may fail to compile if x is an integer type that's not in that list.

质量方面的问题,提供扩展整数类型的实现可能会为这些类型添加 operator>> 重载。这是[member.functions] / 2(由hvd在注释中指出的)所允许的,它谈论标准库中的类:

As a quality of implementation issue, an implementation which offers extended integer types could add operator>> overloads for those types. This is permitted by [member.functions]/2 (as noted by hvd in comments), talking about classes in the standard library:


实现可以声明类中的其他非虚拟成员函数签名:

An implementation may declare additional non-virtual member function signatures within a class:

[...]


  • 通过添加成员函数

在C ++标准库中描述的对成员函数签名的调用的行为就像实现没有声明其他成员一样函数签名。

A call to a member function signature described in the C ++ standard library behaves as if the implementation declares no additional member function signatures.

这比一般规则(即实现中可以添加不会破坏符合程序的扩展)有更强的保证。使用SFINAE的一致性程序的行为可能会受到额外重载的影响。

This is a stronger guarantee than the general rule that implementations may add extensions that don't break conforming programs. The behaviour of conforming programs using SFINAE might be affected by the presence of the extra overloads.

对于输出,不在列表中的整数类型将可以隐式转换到列表中的另一个整数类型。

For output, integer types not in the list will be implicitly convertible to another integer type which is in the list.

这篇关于标准C ++输入输出流如何处理intXX_t类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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