在 C/C++ 中将 int 转换为 bool
Casting int to bool in C/C++
我知道在 C 和 C++ 中,当将 bool 转换为 int 时,(int)true == 1
和 (int)false == 0
。我想知道反向投射...
在下面的代码中,以下所有断言在使用 Visual Studio 2013 和 Keil µVision 5 编译的 .c 文件中对我来说都是正确的。注意 (bool)2 == true
。
C 和 C++ 标准对将非零、非一整数转换为布尔值有何规定?是否指定了此行为?请附上引文。
#include <stdbool.h>
#include <assert.h>
void TestBoolCast(void)
{
int i0 = 0, i1 = 1, i2 = 2;
assert((bool)i0 == false);
assert((bool)i1 == true);
assert((bool)i2 == true);
assert(!!i0 == false);
assert(!!i1 == true);
assert(!!i2 == true);
}
不是 Can I assume (bool)true == (int)1 for any C++ compiler?的副本:
- 反向转换 (int --> bool)。
- 那里没有讨论非零、非一值。
0 个基本类型的值 (1)(2)映射到 false
。
其他值映射到 true
。
这个约定是在原始 C 中通过其流控制语句建立的; C当时没有boolean类型。
这是一个常见的错误,认为作为函数 return 值,false
表示失败。但特别是从 main
开始,false
表示成功。我已经看到这个错误很多次了,包括在 D 语言的 Windows 起始代码中(当你有像 Walter Bright 和 Andrei Alexandrescu 这样的人把它弄错时,那么它就很 easy 会出错),因此这个单挑要当心。
对于内置类型,无需强制转换为 bool
,因为该转换是隐式的。但是,Visual C++(Microsoft 的 C++ 编译器)倾向于为此发出性能警告 (!),纯粹是愚蠢的警告。转换不足以关闭它,但通过双重否定进行转换,即 return !!x
,效果很好。可以将 !!
读作“转换为 bool
”运算符,就像 -->
可以读作“转到”一样。对于那些深入了解运算符表示法可读性的人。 ;-)
1) C++14 §4.12/1 “零值、空指针值或空成员指针值转换为 false
;任何其他值都将转换为 true
。对于直接初始化 (8.5),std::nullptr_t
类型的纯右值可以转换为 bool
类型的纯右值;结果值为 false
。”
2) C99 和 C11 §6.3.1.2/1 “当任何标量值转换为 _Bool
时,如果该值比较等于 0,则结果为 0;否则,结果为 1。”
以下引用C11标准(终稿)
6.3.1.2: When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.
bool
(由 stdbool.h
映射到 C 的内部名称 _Bool
)本身是一个 unsigned integer type:
... The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types.
根据6.2.5p2:
An object declared as type _Bool is large enough to store the values 0 and 1.
据我所知,这些定义在语义上与 C++ 相同 - 内置(!)名称略有不同。 bool
用于 C++,_Bool
用于 C。
请注意,C 不像 C++ 那样使用术语 rvalues。但是,在 C 中,指针是 标量 ,因此将指针分配给 _Bool
的行为与在 C++ 中一样。
有一些老派 'Marxismic' 方法来转换 int -> bool 没有微软 cl 编译器的 C4800 警告 - 是使用否定的否定。
int i = 0;
bool bi = !!i;
int j = 1;
bool bj = !!j;
我知道在 C 和 C++ 中,当将 bool 转换为 int 时,(int)true == 1
和 (int)false == 0
。我想知道反向投射...
在下面的代码中,以下所有断言在使用 Visual Studio 2013 和 Keil µVision 5 编译的 .c 文件中对我来说都是正确的。注意 (bool)2 == true
。
C 和 C++ 标准对将非零、非一整数转换为布尔值有何规定?是否指定了此行为?请附上引文。
#include <stdbool.h>
#include <assert.h>
void TestBoolCast(void)
{
int i0 = 0, i1 = 1, i2 = 2;
assert((bool)i0 == false);
assert((bool)i1 == true);
assert((bool)i2 == true);
assert(!!i0 == false);
assert(!!i1 == true);
assert(!!i2 == true);
}
不是 Can I assume (bool)true == (int)1 for any C++ compiler?的副本:
- 反向转换 (int --> bool)。
- 那里没有讨论非零、非一值。
0 个基本类型的值 (1)(2)映射到 false
。
其他值映射到 true
。
这个约定是在原始 C 中通过其流控制语句建立的; C当时没有boolean类型。
这是一个常见的错误,认为作为函数 return 值,false
表示失败。但特别是从 main
开始,false
表示成功。我已经看到这个错误很多次了,包括在 D 语言的 Windows 起始代码中(当你有像 Walter Bright 和 Andrei Alexandrescu 这样的人把它弄错时,那么它就很 easy 会出错),因此这个单挑要当心。
对于内置类型,无需强制转换为 bool
,因为该转换是隐式的。但是,Visual C++(Microsoft 的 C++ 编译器)倾向于为此发出性能警告 (!),纯粹是愚蠢的警告。转换不足以关闭它,但通过双重否定进行转换,即 return !!x
,效果很好。可以将 !!
读作“转换为 bool
”运算符,就像 -->
可以读作“转到”一样。对于那些深入了解运算符表示法可读性的人。 ;-)
1) C++14 §4.12/1 “零值、空指针值或空成员指针值转换为 false
;任何其他值都将转换为 true
。对于直接初始化 (8.5),std::nullptr_t
类型的纯右值可以转换为 bool
类型的纯右值;结果值为 false
。”
2) C99 和 C11 §6.3.1.2/1 “当任何标量值转换为 _Bool
时,如果该值比较等于 0,则结果为 0;否则,结果为 1。”
以下引用C11标准(终稿)
6.3.1.2: When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.
bool
(由 stdbool.h
映射到 C 的内部名称 _Bool
)本身是一个 unsigned integer type:
... The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types.
根据6.2.5p2:
An object declared as type _Bool is large enough to store the values 0 and 1.
据我所知,这些定义在语义上与 C++ 相同 - 内置(!)名称略有不同。 bool
用于 C++,_Bool
用于 C。
请注意,C 不像 C++ 那样使用术语 rvalues。但是,在 C 中,指针是 标量 ,因此将指针分配给 _Bool
的行为与在 C++ 中一样。
有一些老派 'Marxismic' 方法来转换 int -> bool 没有微软 cl 编译器的 C4800 警告 - 是使用否定的否定。
int i = 0;
bool bi = !!i;
int j = 1;
bool bj = !!j;