Search code examples
c++booleansigned

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


Solution

  • 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.