c++ - std::is_signed<bool>::value 是否保证返回 false?

标签 c++ boolean signed

我知道 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 to false; any other value is converted to true. [...]

所以我们有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 type int, with false becoming zero and true becoming one.

然后比较结果为 1 < 0 ,即 false .支票保证评估为 false .


在 n3797 中,修复后 LWG 2197 ,检查定义如下:

If is_arithmetic<T>::value is true, the same result as integral_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/

相关文章:

c++ - 什么是树形小部件的视口(viewport)?

C++:使用 boolalpha

C++——返回 "true"的函数,但 "if"语句未将其视为 true

c - 代码资格 - libc 的 qsort 中有符号和无符号值之间的比较

java - 更新部署后的 Java 代码签名证书

c++ - 检查 #ifdef 中的宏或真实条件

c++ - 如何在不将其用作行尾的情况下将 "\"输出到文件

math - 有符号整数中的前导 1 表示负数从何而来?

c++ - std::map - 递减迭代器给出奇怪的结果?

c++ - std::ostringstream 如何转换为 bool?