标准规定 bool 值表现为整数类型,但没有指定它们在内存中的具体表示:
"bool 类型的值为 true 或 false.如下所述,bool值表现为整数类型.bool 类型的值参与整数提升" ~ C++03 3.9.1 §6
整数类型的同义词是整数类型.整数类型的表示应使用纯二进制计数系统定义值" ~ C++03 3.9.1 §7
然而它定义了从 bool 到 int 的整体提升:
"bool 类型的右值可以转换为 int 类型的右值,false 变为零和 true 合而为一.这些转换称为积分提升." ~ C++03 4.5 §4-5
"An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one. These conversions are called integral promotions." ~ C++03 4.5 §4-5
以及从其他类型到bool的转换:
零值、空指针值或空成员指针值转换为false;任何其他值转换为true." ~ C++03 4.12 §1
"A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true." ~ C++03 4.12 §1
这篇关于布尔在内存中如何表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!
本文链接:https://www.f2er.com/3181268.html