4 ms·
but, from what i can tell, _Bool actually prevents mistakes. it works consistently and logically like a boolean type. the mistakes come from pre-C99 code that
by andrewcooke 13y ago
but, from what i can tell, _Bool actually prevents mistakes. it works consistently and logically like a boolean type.
the mistakes come from pre-C99 code that calls things boolean, but has unexpected dependencies (that _Bool removes) on bit-level structure.
the more i learn from this thread, the more i want to use _Bool. which worries me, because Linus isn't dumb. but i think(?) the point he is making is related more to legacy code that is "broken but works". or maybe to programmers that rely on/use/exploit bit-structure/implementation details in "bool" types because they don't have experience with more strictly typed languages. [edit: or as mbell said in a reply to me elsewhere, because at os level you often do need to care about bits]
- to3m 13y agoI've certainly found C++ bool helpful, in terms of readability and self-documentation, and the general positive clarifying effects of making sure you've got your types straight (to the extent that C++ gives you any help with that). C++ bool does the right thing, as far as turning values into true or false goes (the cast effectively does a "!=0", making the result exactly 1 or 0). I believe that C99 _Bool behaves in the same way in this respect - so that would be doing the right thing too. (One issue with "native" bools, that others have pointed out, is that it's possible to introduce bugs due to implicit conversions. But I've personally found this behaviour usually to be what you want, and I don't remember having to fix any bugs caused by it, suggesting that they can't have been all that difficult to sort out.) His point about casting to fake bool types is well made, and I wonder how many instances of this I will now spot? (gcc appears to issue perfectly fine warnings for this case, though.) Another argument for using the built-in types, in my view. Or for switching to "typedef uintptr_t bool".
- marcosdumay 13y agoNothing is the C family simply prevents mistakes. It's always more complex than that. The bool type prevents a lot of kinds of mistakes, but opens your code to a lot of new types. The problem is that if you have a lot of experience in not using it, you already learned how to avoid those first mistakes, but all that experience is useless for dealing with the newly introduced ones. My opinion is that the bool is a good construct. It's a good idea to use it on new code, but first you must understand it, otherwise you won't get a fighting chance.