Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::is_unsigned<bool>::value well defined?

I am wondering whether

std::is_unsigned<bool>::value

is well defined according to the standard or not?

I ask the question because typename std::make_unsigned<bool>::type is not well defined.

like image 527
Vincent Avatar asked Jan 05 '16 12:01

Vincent


People also ask

Is boolean signed or unsigned?

The Boolean type is unsigned and has the lowest ranking in its category of standard unsigned integer types; it may not be further qualified by the specifiers signed , unsigned , short , or long .

Is unsigned Type C++?

The unsigned keyword is a data type specifier, that makes a variable only represent non-negative integer numbers (positive numbers and zero). It can be applied only to the char , short , int and long types.

Is Wchar_t signed?

wchar_t is unsigned. Corresponding assembly code says movzwl _BOM, %eax .

What is unsigned long long C++?

unsigned long long is a 'simple-type-specifier' for the type unsigned long long int (so they're synonyms). The long long set of types is also in C99 and was a common extension to C++ compilers even before being standardized.


1 Answers

There is no concept of signedness for bool. From [basic.fundamental]/6:

Values of type bool are either true of false. [Note: There are no signed, unsigned, short, or long bool types or values. — end note] Values of type bool participate in integral promotions (4.5).

By contrast, signedness is explicitly called out for the signed integer types (paragraph 2) and unsigned integer types (paragraph 3).

Now for the is_signed and is_unsigned traits. First off, the traits are always well-defined, but only interesting for arithmetic types. bool is an arithmetic type, and is_signed<T>::value is defined (see Table 49) as T(-1) < T(0). By using the rules of boolean conversion and standard arithmetic conversions, we see that this is is false for T = bool (because bool(-1) is true, which converts to 1). Similarly, is_unsigned<T>::value is defined as T(0) < T(-1), which is true for T = bool.

like image 136
Kerrek SB Avatar answered Oct 07 '22 17:10

Kerrek SB