布尔值的最小大小是多少? [英] What is the minimum size of a boolean?
问题描述
我正在研究Stanley B. Lippman在C ++ Prime中的算术类型.当作者谈论整数类型以及每个整数的大小(例如char是8位)时,我注意到尚不清楚布尔的最小大小.考虑类型转换以及以下代码:
I was studying about arithmetic types in C++ Prime by Stanley B. Lippman. When the author talks about integral types and the size of each one of them, like for example char is 8 bits, I noticed that it is not so clear the minimum size of a boolean. Considering type conversions, and the following code:
bool b = 42; // b is true
int i = b; // i had value 1
我可以说布尔值具有与整数相同的最小大小(通常为16位),因为如果为假,它将为0(即一个int),如果为true,则为1(如果为true)则为另一个int?如果我说错了,布尔值的最小大小是多少?
Can I say that booleans have the same minimum size of an integer (usually 16 bits), since if it is false it would be 0, that is an int, and 1 if it is true, another int? And if what I said is wrong, what is the minimum size of a boolean?
推荐答案
布尔值的最小大小是多少?
What is the minimum size of a boolean?
在标准情况下,您可以阅读5.3.3:
in standard you can read 5.3.3:
将sizeof应用于任何其他基本类型(3.9.1)的结果是 实现定义的. [注意:特别是sizeof(bool),sizeof(char16_t),sizeof(char32_t)和 sizeof(wchar_t)是实现定义的.75—尾注]
The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.75 —end note ]
并注意:
sizeof(bool)不必为1
sizeof(bool) is not required to be 1
因此它可能是1个字节,但也可能是4个字节. Standard还允许int的大小为 1字节 16位:
so it might be 1 byte but it might be also 4 bytes. Standard also allows for ints to be of size 1 byte 16bits:
1 == sizeof(char)< = sizeof(short)< = sizeof(int)< = sizeof(long)< = sizeof(long long)
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
因此bool的最小大小为1个字节,与int相同.另一个问题是您是否会找到类型为1字节 int
的平台.
so minimum size for bool is 1 byte, the same as for int. The other question is whether you will ever find platform with 1 byte int
type.
minimum size (guaranteed minimum) for int
is 16bits, this size guarantee for integral/arithmetic types in C and C++ explains why.
这篇关于布尔值的最小大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!