That would be problematic as it means if statements and the like (in which the condition is defined to be a value converted to bool) would test the lowest bit of the condition, not the non-zeroness.
I meant in terms of bitwise operations it's defined as always being treated as a 1-bit integer. But bool is always hard in most C-like languages, where it's ½ enumeration, ¼ byte/machine integer, ¼ a single bit.
Fantastic example of why integer promotion is a kinda horrible idea, as is treating bool types as integers in general.
In any language, not just C++.
I enthusiastically used Pascal for many years- decades, in fact- but got increasingly annoyed by this sort of behaviour particularly since Wirth had tightened up type compatibility in later languages.
Most of the weirdness in the examples is not even really C++, it's behaviour that C++ inherited from C and hasn't been able to fix without breaking backwards compatibility. Integer promotion rules? C. Boolean values are ints? C. The entire 'Putting it together' section? C!.
Most of the worst bits of C++ are C, the nice bits of languages like Rust are due to not trying to be a C superset.
And I completely agree with the rest of this thread. Boolean should not be an integer type. C also has some fun things. Did you know that x++ on a boolean sets it to true? Guess what x-- does (hint: it does not set it to false). Trying to pretend that boolean values are integers causes all sorts of weirdness.
The integer promotion rules have other fun corner cases arising from sign extension in unexpected places.
tomsmeding | 22 hours ago
I should really stop writing C++. I shook my head so many times through this post. Brilliant.
icefox | a day ago
Fantastic example of why integer promotion is a kinda horrible idea, as is treating
booltypes as integers in general.nytpu | 23 hours ago
Or at the very least, properly treating bool as a 1-bit integer even if it means sometimes generating a few more instructions to mask it down.
hailey | 11 hours ago
That would be problematic as it means if statements and the like (in which the condition is defined to be a value converted to
bool) would test the lowest bit of the condition, not the non-zeroness.boolis specialnytpu | 7 hours ago
I meant in terms of bitwise operations it's defined as always being treated as a 1-bit integer. But bool is always hard in most C-like languages, where it's ½ enumeration, ¼ byte/machine integer, ¼ a single bit.
MarkMLl | 15 hours ago
In any language, not just C++.
I enthusiastically used Pascal for many years- decades, in fact- but got increasingly annoyed by this sort of behaviour particularly since Wirth had tightened up type compatibility in later languages.
david_chisnall | 8 hours ago
Most of the weirdness in the examples is not even really C++, it's behaviour that C++ inherited from C and hasn't been able to fix without breaking backwards compatibility. Integer promotion rules? C. Boolean values are
ints? C. The entire 'Putting it together' section? C!.Most of the worst bits of C++ are C, the nice bits of languages like Rust are due to not trying to be a C superset.
And I completely agree with the rest of this thread. Boolean should not be an integer type. C also has some fun things. Did you know that
x++on a boolean sets it to true? Guess whatx--does (hint: it does not set it to false). Trying to pretend that boolean values are integers causes all sorts of weirdness.The integer promotion rules have other fun corner cases arising from sign extension in unexpected places.