为什么 C++ 没有将 sizeof(bool) 定义为 1标准?
C++ 标准本身将 char
、signed char
和 unsigned char
的大小定义为 1 个字节。我想知道为什么它也没有定义 sizeof(bool)
?
C++03 标准 $5.3.3/1 说,
sizeof(char)、sizeof(signed char) 和 sizeof(unsigned char) 为 1;这 sizeof 的结果应用于任何其他 基本类型(3.9.1)是 实现定义的。 [注:在 特别是,sizeof(bool) 和 sizeof(wchar_t) 是 实现定义.69)
我理解 sizeof(bool) 不能小于一个字节。但是有什么理由让它大于 1 字节吗?我并不是说实现将其定义为大于 1,而是标准让它由实现定义就好像它可能大于 1。
如果没有原因 sizeof (bool)
大于 1,那么我不明白为什么标准没有将其定义为 1 byte
,因为它定义了 sizeof(char)
,而且都是变体。
Size of char
, signed char
and unsigned char
is defined to be 1 byte, by the C++ Standard itself. I'm wondering why it didn't define the sizeof(bool)
also?
C++03 Standard $5.3.3/1 says,
sizeof(char), sizeof(signed char) and
sizeof(unsigned char) are 1; the
result of sizeof applied to any other
fundamental type (3.9.1) is
implementation-defined. [Note: in
particular,sizeof(bool) and
sizeof(wchar_t) are
implementation-defined.69)
I understand the rationale that sizeof(bool) cannot be less than one byte. But is there any rationale why it should be greater than 1 byte either? I'm not saying that implementations define it to be greater than 1, but the Standard left it to be defined by implementation as if it may be greater than 1.
If there is no reason sizeof(bool)
to be greater than 1, then I don't understand why the Standard didn't define it as just 1 byte
, as it has defined sizeof(char)
, and it's all variants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它的另一个可能的大小是 int 的大小,它是平台的“高效”整数类型。
在实现选择 1 或
sizeof(int)
之间存在差异的架构上,可能需要在大小之间进行权衡(但如果您愿意为每个bool< 浪费 7 位) /code>,为什么你不应该乐意浪费 31(当大小很重要时使用位域)与性能(但什么时候存储和加载 bool 值会成为真正的性能问题?使用
int
当速度很重要时明确)。因此实现灵活性获胜 - 如果由于某种原因1
在性能或代码大小方面会很糟糕,它可以避免它。The other likely size for it is that of
int
, being the "efficient" integer type for the platform.On architectures where it makes any difference whether the implementation chooses 1 or
sizeof(int)
there could be a trade-off between size (but if you're happy to waste 7 bits perbool
, why shouldn't you be happy to waste 31? Use bitfields when size matters) vs. performance (but when is storing and loading bool values going to be a genuine performance issue? Useint
explicitly when speed matters). So implementation flexibility wins - if for some reason1
would be atrocious in terms of performance or code size, it can avoid it.正如@MSalters 指出的,一些平台可以更有效地处理更大的数据项。
许多“RISC”CPU(例如,MIPS、PowerPC、Alpha 的早期版本)在处理小于一个字的数据时会遇到相当大的困难,因此它们也会这样做。 IIRC,至少在Alpha上的一些编译器中,bool实际上占用了64位。
PowerPC Mac 的 gcc 默认使用 4 个字节表示布尔值,但如果您愿意,可以通过开关将其更改为 1 个字节。
即使对于 x86,使用 32 位数据项也有一些优势。 x86 的 gcc 在其配置文件之一中有(或者至少曾经有——我最近根本没有看过)
BOOL_TYPE_SIZE
的定义(来自内存,所以我可以这样定义)名字有点错误),你可以设置为1或4,然后重新编译编译器以获得该大小的bool。至于背后的原因,我想说这是 C 和 C++ 基本哲学的简单反映:为实现留出尽可能多的空间来优化/定制其合理的行为。仅当/如果有明显的、切实的好处,并且不太可能产生任何重大责任时,才需要特定的行为,特别是如果更改将使在某些特定平台上支持 C++ 变得更加困难(当然,如果该平台足够强大)晦涩难懂,可能会被忽略)。
As @MSalters pointed out, some platforms work more efficiently with larger data items.
Many "RISC" CPUs (e.g., MIPS, PowerPC, early versions of the Alpha) have/had a considerably more difficult time working with data smaller than one word, so they do the same. IIRC, with at least some compilers on the Alpha a bool actually occupied 64 bits.
gcc for PowerPC Macs defaulted to using 4 bytes for a bool, but had a switch to change that to one byte if you wanted to.
Even for the x86, there's some advantage to using a 32-bit data item. gcc for the x86 has (or at least used to have -- I haven't looked recently at all) a define in one of its configuration files for
BOOL_TYPE_SIZE
(going from memory, so I could have that name a little wrong) that you could set to 1 or 4, and then re-compile the compiler to get a bool of that size.As for the reason behind this, I'd say it's a simple reflection of a basic philosophy of C and C++: leave as much room for the implementation to optimize/customize its behavior as reasonable. Require specific behavior only when/if there's an obvious, tangible benefit, and unlikely to be any major liability, especially if the change would make it substantially more difficult to support C++ on some particular platform (though, of course, if the platform is sufficiently obscure, it might get ignored).
许多平台无法有效加载小于 32 位的值。他们必须加载 32 位,并使用移位和掩码操作来提取 8 位。您不会希望对单个 bool 进行此操作,但对于字符串来说这是可以的。
Many platforms cannot effectively load values smaller than 32 bits. They have to load 32 bits, and use a shift-and-mask operation to extract 8 bits. You wouldn't want this for single
bool
s, but it's OK for strings.运算结果“sizeof”是 MADU(最小可寻址单元),而不是字节。所以系列处理器 C54*。 C55 * 德州仪器,表达式 1 MADU = 2 字节。
对于此平台,sizeof (bool) = sizeof (char) = 1 MADU = 2 字节。
这并不违反C++标准,而是澄清了情况。
The operation resulted in 'sizeof' is MADUs (minimal addresible unit), not bytes. So family processors C54 *. C55 * Texas Instuments, the expression 1 MADU = 2 bytes.
For this platform sizeof (bool) = sizeof (char) = 1 MADUs = 2 bytes.
This does not violate the C + + standard, but clarifies the situation.