Re: RFC: booleans and the kernel

H. Peter Anvin
2002-01-24T22:22:02Z

Followup to: <Pine.LNX.4.44.0201241545120.2839-100000@waste.org>
By author: Oliver Xymoron <oxymoron@waste.org>
In newsgroup: linux.dev.kernel
>
> > It doesn't fix "if ( x = true)". If would
> > just make it more legit to use "if (x)".
>
> It's been legit and idiomatic since day 1, if not sooner.
>

The main reasons for bool is:

a) The ability to save space. No need to waste a 32- or 64-bit word
to hold a single bit. If you're on an architecture that has flags
or predicates you may be able to carry a boolean in such a value
instead of in a full register.

b) Compatibility with other languages, including but not limited to
C++ (there is a standard under development for inter-language linking,
incidentally.) C++, of course, needs bool for overloading reasons.

c) The ability to cast to bool and get an unambiguous true or false:

b = (bool)a;

This replaces the idiomatic but occationally confusing

b = !!a;

d) Similarly, you can avoid doing booleanization multiple times:

/* Highly artificial example */
int foo(bool a)
{
return a ? 55 : 47;
}

... could be implemented by the compiler as 47 + (a << 3), or
depending on your ABI convention, perhaps a caller calling
foo(x < 4) could be implemented as foo(x-4) without needing to
convert it into an integer of exactly 1 and 0.

Given the way C currently does it, you pretty much have do
booleanize both in the caller and the callee to be on the safe
side.

-hpa

--
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt <amsp@zytor.com>