Re: [PATCH] Use ^=1 to toggle between 0 and 1
- From
René Scharfe <l.s.r@web.de>
- Date
- Dec 14, 2023, 13:08 UTC
- Message-ID
- <4d0b2a5f-305b-4350-b164-44923cb250d8@web.de>
- In-Reply-To
- <20231213080143.GA1684525@coredump.intra.peff.net>
Am 13.12.23 um 09:01 schrieb Jeff King:
Show 6 quoted lines
> On Tue, Dec 12, 2023 at 11:30:03PM +0100, René Scharfe wrote: > >> I wonder if >> it's time to use the C99 type _Bool in our code. It would allow >> documenting that only two possible values exist in cases like the one >> above. That would be even more useful for function returns, I assume.
Show 10 quoted lines
> I don't even know that we'd need much of a weather-balloon patch. I > think it would be valid to do: > > #ifndef bool > #define bool int > > to handle pre-C99 compilers (if there even are any these days). Of > course we probably need some conditional magic to try to "#include > <stdbool.h>" for the actual C99. I guess we could assume C99 by default > and then add NO_STDBOOL as an escape hatch if anybody complains.
The semantics are slightly different in edge cases, so that fallback would not be fully watertight. E.g. consider:
bool b(bool cond) {return cond == true;}
bool b2(void) {return b(2);}b() returns false if you give it false and true for anything else. b2() returns true.
With int as the fallback this becomes:
int b(int cond) {return cond == 1;}
int b2(void) {return b(2);}Now only 1 is recognized as true, b2() returns 0 (false).
A coding rule to not compare bools could mitigate that. Or a rule to only use the values true and false in bool context and to only use logical operators on them.
René