的typedef签署的类型,它可以包含一个为size_t? [英] typedef for a signed type that can contain a size_t?

查看:166
本文介绍了的typedef签署的类型,它可以包含一个为size_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个签名的类型,可以包含全系列为size_t 值的标准(或专有MSVC)的typedef?即在64位系统上,这将是一个128位整数。

Is there a standard (or MSVC proprietary) typedef for a signed type that can contain the full range of size_t values? I.e. on a 64-bit system, it would be a 128-bit signed integer.

推荐答案

这是不可能的一般定义这样一个类型。这是完全合法的实现,使为size_t 最大支持无符号类型,这意味着没有符号的类型可以容纳所有的值。

It's not possible in general to define such a type. It's perfectly legal for an implementation to make size_t the largest supported unsigned type, which would mean that no signed type can hold all its values.

ptrdiff_t的未必足够宽。这两个指针相减的结果,但没有什么,说一个指针减法不能溢出。请参阅C ++标准的5.7节:

ptrdiff_t is not necessarily wide enough. It's the result of subtracting two pointers, but there's nothing that says a pointer subtraction cannot overflow. See section 5.7 of the C++ standard:

当两个指针指向同一数组对象的元素相减,
  其结果是两个阵列的标之差
  元素。结果类型是实现定义的签署
  整体式;这种类型的应是被定义为相同的类型
   的std :: ptrdiff_t的 < cstddef> 头(18.2)。与任何其他
  算术溢出,如果结果不适合提供的空间,
  该行为是不确定的。

When two pointers to elements of the same array object are subtracted, the result is the difference of the subscripts of the two array elements. The type of the result is an implementation-defined signed integral type; this type shall be the same type that is defined as std::ptrdiff_t in the <cstddef> header (18.2). As with any other arithmetic overflow, if the result does not fit in the space provided, the behavior is undefined.

最大签订类型是还会将intmax_t 中所定义; stdint.h&GT; &LT; cstdint&GT; 。这是一个C99的功能,和C ++ 11是第一个C ++标准,以纳入C99标准库,让你的编译器可能不支持它(MSVC最有可能没有)。如果有一个符号类型的宽度足以键入为size_t 的所有可能的值,那么还会将intmax_t 是(虽然有可能的是一个较窄的签署类型也有资格)。

The largest signed type is intmax_t, defined in <stdint.h> or <cstdint>. That's a C99 feature, and C++11 was the first C++ standard to incorporate the C99 standard library, so your compiler might not support it (and MSVC most likely doesn't). If there's a signed type wide enough to hold all possible values of type size_t, then intmax_t is (though there might be a narrower signed type that also qualifies).

您也可以使用长长,这是一个符号类型保证至少64位(最有可能同还会将intmax_t )。即使它不够广泛持有型为size_t 的所有可能的值,它几乎肯定会保存所有的类型的相关的值为size_t - 除非你的实现实际上支持大于8艾字节(这是8192 PB级,或8388608兆兆字节)

You can also use long long, which is a signed type guaranteed to be at least 64 bits (and most likely the same as intmax_t). Even if it's not wide enough to hold all possible values of type size_t, it will almost certainly hold all relevant values of type size_t -- unless your implementation actually supports objects bigger than 8 exabytes (that's 8192 petabytes, or 8388608 terabytes).

(注意,我使用的exa-,peta-和tera-二进制的定义,这是值得怀疑的有效性。)

(Note, I'm using the binary definitions of "exa-", "peta-", and "tera-", which are of questionable validity.)

这篇关于的typedef签署的类型,它可以包含一个为size_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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