c++之size_t 是否与 ptrdiff_t 具有相同的大小和对齐方式

pengyingh 阅读:30 2025-02-15 21:57:57 评论:0

在我的平台上(以及我认为的大多数平台上)std::size_tstd::ptrdiff_t具有相同的大小和相同的对齐方式。有没有哪个平台不是真的?简而言之:标准是否要求?

请您参考如下方法:

In short: is it required by the standard?



不。唯一的要求来自 [support.types.layout]/2它是:

The type ptrdiff_­t is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object, as described in [expr.add].



paragraph 4

[ Note: It is recommended that implementations choose types for ptrdiff_­t and size_­t whose integer conversion ranks are no greater than that of signed long int unless a larger size is necessary to contain all the possible values. — end note ]



但注释是非规范性的,它只是一个建议,而不是一个要求。
std::size_t被定义为

The type size_­t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object ([expr.sizeof]).



paragraph 3并且它也没有要求它们是相同的。


标签:C++
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号