Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::is_signed<bool>::value guaranteed to return false?

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

like image 667
loop Avatar asked Mar 24 '14 21:03

loop


2 Answers

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

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 type int, with false becoming zero and true 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 is true, the same result as integral_constant<bool, T(-1) < T(0)>::value; otherwise, false

Which has the same result in case of T == bool.

like image 159
dyp Avatar answered Oct 02 '22 14:10

dyp


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)

like image 31
Peter - Reinstate Monica Avatar answered Oct 02 '22 14:10

Peter - Reinstate Monica