char 是唯一具有强制大小的类型吗?
Is char the only type with a mandated size?
在 C++ 标准中:
sizeof(char)
, sizeof(signed char)
and sizeof(unsigned char)
are 1
.
C++ 中是否有任何其他类型具有固定的 sizeof
?
空结构符合这个定义吗?
关于问题1,你也有stdint.h
中的类型。
没有。如您所说,sizeof(signed char)
、sizeof(unsigned char)
和 sizeof(char)
由标准定义为 1。请注意,char
必须是 signed
或 unsigned
但它始终被认为是一种独特的类型。 sizeof
其他任何事情都取决于实施受某些限制(例如 sizeof(long)
不能小于 sizeof(int)
。)
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 既不是 char
、signed char
也不是 unsigned char
,所以它不符合这个定义。 (顺便说一句,它的 sizeof
不能是 0
)。
在 C++ 标准中:
sizeof(char)
,sizeof(signed char)
andsizeof(unsigned char)
are1
.
C++ 中是否有任何其他类型具有固定的
sizeof
?空结构符合这个定义吗?
关于问题1,你也有stdint.h
中的类型。
没有。如您所说,
sizeof(signed char)
、sizeof(unsigned char)
和sizeof(char)
由标准定义为 1。请注意,char
必须是signed
或unsigned
但它始终被认为是一种独特的类型。sizeof
其他任何事情都取决于实施受某些限制(例如sizeof(long)
不能小于sizeof(int)
。)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 既不是 char
、signed char
也不是 unsigned char
,所以它不符合这个定义。 (顺便说一句,它的 sizeof
不能是 0
)。