为什么将std :: streamsize定义为有符号而不是无符号? [英] Why is std::streamsize defined as signed rather than unsigned?

查看:126
本文介绍了为什么将std :: streamsize定义为有符号而不是无符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://en.cppreference.com/w/cpp/io/streamsize

std :: streamsize类型是一个有符号整数类型,用于表示 I/O操作中传输的字符数或大小 一个I/O缓冲区.

The type std::streamsize is a signed integral type used to represent the number of characters transferred in an I/O operation or the size of an I/O buffer.

据我所知,流的大小永远不会为负,所以我的问题是:

As far as I can imagine, a stream's size can never be negative, so, my question is:

为什么将std::streamsize定义为 signed 而不是 unsigned ?背后的原因是什么?

Why is std::streamsize defined as signed rather than unsigned? What's the rationale behind?

推荐答案

The draft C++ standard has the following footnote 296 in section 27.5.2 Types which says:

streamsize在大多数ISO C使用size_t的地方使用.最多 streamsize的使用可以使用size_t,但 strstreambuf构造函数,需要负值.它应该 可能是与size_t对应的带符号类型(这是 Posix.2调用ssize_t).

streamsize is used in most places where ISO C would use size_t. Most of the uses of streamsize could use size_t, except for the strstreambuf constructors, which require negative values. It should probably be the signed type corresponding to size_t (which is what Posix.2 calls ssize_t).

,我们可以在D.7.1.1 strstreambuf构造函数部分中看到以下条目(强调我的前进):

and we can see in section D.7.1.1 strstreambuf constructors we have the following entries (emphasis mine going forward):

strstreambuf(char* gnext_arg, streamsize n, char *pbeg_arg = 0);
strstreambuf(signed char* gnext_arg, streamsize n,
   signed char *pbeg_arg = 0);
strstreambuf(unsigned char* gnext_arg, streamsize n,
   unsigned char *pbeg_arg = 0);

并说:

gnext_arg应指向其数组对象的第一个元素 元素数N的确定如下:

gnext_arg shall point to the first element of an array object whose number of elements N is determined as follows:

,从下面的讨论中我们可以看到类型为streamsizen实际上必须具有负值:

and we can see from the following discussion that n which is of type streamsize is indeed required to be able to take on a negative value:

-如果n> 0,则N为n.

— If n > 0, N is n.

-如果n == 0,则N为std :: strlen(gnext_arg).

— If n == 0, N is std::strlen(gnext_arg).

如果n< 0,N为INT_MAX . 336

对于此要求和已完成的问题255 也来自 Howard Hinnant 的类似评论,内容为:

This seems like a poor argument for this requirement and the closed issue 255 has a similar comment from Howard Hinnant which says:

这有点像,但我想知道是否不会出现串流 比流大小更好的选择. pbump和gbump的论点必须 被签署. [...]这对pbump的论点似乎有点弱 和g.如果我们真的要摆脱strstream,这个脚注 可能会附带它,以及使streamsize签名的原因.

This is something of a nit, but I'm wondering if streamoff wouldn't be a better choice than streamsize. The argument to pbump and gbump MUST be signed. [...] This seems a little weak for the argument to pbump and gbump. Should we ever really get rid of strstream, this footnote might go with it, along with the reason to make streamsize signed.

这篇关于为什么将std :: streamsize定义为有符号而不是无符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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