- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我知道 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/
两者都是 std::is_signed和 std::numeric_limits::is_signed应该给出关于 T 的签名的答案. 为什么现在有两个符号指示符(即自 C++11 起)? 最佳答案
我想检查变量是否有符号。我找到了一个名为 is_signed 的类模板但我不能使用它,因为我是 C++ 的新手。如何检查变量是否已签名? #include #include int main()
谁能解释一下,为什么 #include #include using namespace std; enum E : signed int { a=-1, b = 1,}; int main()
我知道 std::numeric_limits::is_signed 永远是错误的,但对于 std::is_signed::value 也是如此吗? ?谢谢 最佳答案 std::is_signed定义
我有一个函数,但我想将它分成两个函数,一个用于返回有符号整数,一个用于返回无符号整数。看起来像 std::is_signed不是严格意义上的整数,所以我想我是否可以对类似 std::is_integr
我是一名优秀的程序员,十分优秀!