fleabay said:
I fear this is going to spawn a large thread of conversation about the nuances of booleans and the bool data type in various languages.
No need to fear, that's basically guaranteed with the question!
Booleans are different from a bool data type. As long as you remember that, there's not much to debate.
Boolean logic -- the stuff from George Boole who received a ton of awards in the 1840s and 50s -- concerns itself only with values of true and false. That logic can be represented by a single bit for determined states, and two bits if you need indeterminate states of true, false, and maybe/unknown.
A bool data type needs to deal with the discrete computing machines we use. This includes recognition of the smallest addressable unit of memory on the hardware which is never a single bit.
Everything that follows from that stems from the difference between Boolean logic and the bool data type.
Calin said:
Isn`t bool redundant?
Yes for storage, no for logic.
It wasn't added to C until C99, nor was it a distinct type in many of the early C++ compilers. It wasn't until the early 1990s that it spread.
Before that, an integral type was always used, it was not mentioned in the C89 standard or earlier.
Unfortunately it was forced because of the logic around inconsistent definitions. False was nearly always 0, but true was variations of !false, 1, -1 (all bits set), 0xff, 0xffff, any non-zero value with no particular bit set, and more. With standardization the compiler writers could enforce rules around it. One example would be the “any non-zero value” definition, a function could return the value 2, which is technically true, but would compare false when compared with a different bool value. That is, result == TRUE is false, because result is the value 2. Similarly, one library's TRUE (0xff) and a different library's TRUE (1) fail the a==b test.
You can do work-arounds, !!a == !!b, and you can even find that in a lot of code such as the Linux kernel that must accept outside data.
Calin said:
There doesn`t seem to exist any specialized functionality for bool like there is for char for instance
There is a bunch of specialized functionality!
The two most critical are conversion rank and the conversion between types.
Bool has conversion rank less than any other standard integer type, including char. This has side effects that it can only expand, it affects how it is passed as a parameter, promotions, and conversions. These are mostly effects that happen behind your back, so you may not have given them much thought.
Non-arithmetic types can be converted to bool, such as pointers, pointers to members, and null pointers (which internally aren't guaranteed to be the integral value zero). These often require additional processing by the compiler. For example, a pointer to a member is usually a pair of two pointers, if either one is non-zero or non-null the result is converted to true.
When a conversion is made away from bool, regardless of the internal storage method (which is implementation defined) the value false must be converted to the integer zero, and the value true must be converted to the integer one. This solves the issue above, like a==b, if they are done as a bool test then they are guaranteed to be the same internal type and therefore consistent. If they're promoted to an integral comparison they MUST be either 0 or 1, they cannot have the other values.
Logical negation ! has a result type of bool, which engages both conversion rank and type rules. Increment and decrement (++ and --) are expressly forbidden to be bool. There are notes around equality and inequality. There are notes around template parameters. And on and on.