char 是唯一具有强制大小的类型吗?

Is char the only type with a mandated size?

在 C++ 标准中:

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.

  1. C++ 中是否有任何其他类型具有固定的 sizeof

  2. 空结构符合这个定义吗?

关于问题1,你也有stdint.h中的类型。

  1. 没有。如您所说,sizeof(signed char)sizeof(unsigned char)sizeof(char) 由标准定义为 1。请注意,char 必须是 signedunsigned 但它始终被认为是一种独特的类型。 sizeof 其他任何事情都取决于实施受某些限制(例如 sizeof(long) 不能小于 sizeof(int)。)

  2. C++ 标准要求 sizeof 一个空的 class 是一个大于零的整数值(否则指针算法会严重崩溃)。

1) Are there any other types in C++ which have a fixed sizeof?

有指定大小的数组:

sizeof(T[N]) == N * sizeof(T)

所以 sizeof(char[42])42

2) Does the empty structure correspond to this definition?

Empty struct 既不是 charsigned char 也不是 unsigned char,所以它不符合这个定义。 (顺便说一句,它的 sizeof 不能是 0)。