I know that std::numeric_limits<bool>::is_signed
will always be false but is that also true for std::is_signed<bool>::value
? Thanks
std::is_signed
is defined as follows (Table 49 - Type property predicates, n3485):
is_arithmetic<T>::value && T(-1) < T(0)
bool
is an integral type [basic.fundamental]/7, therefore an arithmetic type [basic.fundamental]/8.
bool(x)
where x
is an int
, uses the boolean conversion [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
. [...]
So we have bool(-1) < bool(0)
evaluating to true < false
, which is subject (see [expr.rel]/2) to the usual arithmetic conversions [expr]/10 => integral promotion [conv.prom]/6
A prvalue of type
bool
can be converted to a prvalue of typeint
, withfalse
becoming zero andtrue
becoming one.
The comparison then reads 1 < 0
, which is false
. The the check is guaranteed to evaluate to false
.
In n3797, after fixing LWG 2197, the check is defined as follows:
If
is_arithmetic<T>::value
istrue
, the same result asintegral_constant<bool, T(-1) < T(0)>::value
; otherwise,false
Which has the same result in case of T == bool
.
From 20.9.4.3, Table 49:
is_arithmetic::value && T(-1) < T(0)
So what translates -1 to when converted into a bool? Naturally, 1:
A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
(4.12)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With