我知道 std::numeric_limits<bool>::is_signed
永远是错误的,但对于 std::is_signed<bool>::value
也是如此吗? ?谢谢
最佳答案
std::is_signed
定义如下(表 49 - 类型属性谓词,n3485):
is_arithmetic<T>::value && T(-1) < T(0)
bool
是整数类型 [basic.fundamental]/7,因此是算术类型 [basic.fundamental]/8。
bool(x)
其中 x
是一个 int
, 使用 boolean 转换 [conv.bool]/1
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type
bool
. A zero value, null pointer value, or null member pointer value is converted tofalse
; any other value is converted totrue
. [...]
所以我们有bool(-1) < bool(0)
评估为 true < false
,它受制于(参见 [expr.rel]/2)通常的算术转换 [expr]/10 => 积分提升 [conv.prom]/6
A prvalue of type
bool
can be converted to a prvalue of typeint
, withfalse
becoming zero andtrue
becoming one.
然后比较结果为 1 < 0
,即 false
.支票保证评估为 false
.
在 n3797 中,修复后 LWG 2197 ,检查定义如下:
If
is_arithmetic<T>::value
istrue
, the same result asintegral_constant<bool, T(-1) < T(0)>::value
; otherwise,false
在 T == bool
的情况下具有相同的结果.
关于c++ - std::is_signed<bool>::value 是否保证返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22620517/