Time to standardize "true" and "false"
LTH network news server
newsuser at lth.se
Fri Sep 22 17:31:38 AEST 1989
In article <13730 at well.UUCP> nagle at well.UUCP (John Nagle) writes:
>
> I would like to suggest that the time has come to standardize the
>Boolean values in C.
I completely agree that a boolean data type is needed in C++. I
think the definition should define the following properties:
1. The data type is called "boolean".
2. The allowed values are "false" and "true".
3. int(false) = 0 and int(true) = 1
4. boolean(0) = false, other values are true
I do not know if implicit conversion boolean <==> int should be allowed.
In the time before C++ 2.0, it was quite common to define a boolean type
as an enumeration:
enum boolean {false, true};
Unfortunately (in this case), C++ no longer allows implicit conversion
int ==> enum, so the result of a comparison must be explicitly type cast:
boolean b;
b = (i == 3); // warning
b = boolean(i == 3); // ok
This is rather clumsy. We cannot define our own operator functions on
the boolean data type, because one of the arguments must be a class object.
boolean operator == (boolean x, boolean y) {...} // error
I do not understand exactly why this restriction is required, but apparently
it is. Please enlighten me.
What remains is to define a class for boolean. This is not easy to
make as efficient as the built in types, but the problem is mostly in
the lack of optimization in the code generators. I have tried; you
will find my attempt to define a boolean below. Note that I have not
defined the operators && and || -- I do not think we can achieve the
"short circuit" evaluation we're used to with a user-defined operator.
Dag Michael Bruck
--
Department of Automatic Control Internet: dag at control.lth.se
Lund Institute of Technology
P. O. Box 118 Phone: +46 46-108779
S-221 00 Lund, SWEDEN Fax: +46 46-138118
==============================================================================
// Boolean data type
enum {false, true};
class Boolean {
public:
Boolean() { val = false; }
Boolean(int i) { val = (i != false); }
Boolean(const Boolean& b) { val = b.val; }
// Any non-zero value is true; default value is false.
operator int() { return val; }
// Type cast boolean => integer.
void operator &= (const Boolean& b) { val &= b.val; }
void operator |= (const Boolean& b) { val |= b.val; }
void operator ^= (const Boolean& b) { val ^= b.val; }
// Operator assignment.
friend Boolean operator & (const Boolean&, const Boolean&);
friend Boolean operator | (const Boolean&, const Boolean&);
friend Boolean operator ^ (const Boolean&, const Boolean&);
friend Boolean operator ! (const Boolean&);
friend Boolean operator == (const Boolean&, const Boolean&);
friend Boolean operator != (const Boolean&, const Boolean&);
// These operators need access to the internal representation.
private:
int val;
Boolean(long i) { val = int(i); } // no check
};
inline Boolean operator & (const Boolean& p, const Boolean& q)
{ return long(p.val & q.val); }
inline Boolean operator | (const Boolean& p, const Boolean& q)
{ return long(p.val | q.val); }
inline Boolean operator ^ (const Boolean& p, const Boolean& q)
{ return long(p.val ^ q.val); }
inline Boolean operator ! (const Boolean& p)
{ return long(!p.val); }
inline Boolean operator == (const Boolean& p, const Boolean& q)
{ return long(p.val == q.val); }
inline Boolean operator != (const Boolean& p, const Boolean& q)
{ return long(p.val != q.val); }
// Note: there are no && and || operators.
Boolean f(Boolean b)
{
return !b;
}
main()
{
Boolean p, q, r;
r &= p;
r = p & q;
r = f(p);
}
--
Department of Automatic Control Internet: dag at control.lth.se
Lund Institute of Technology
P. O. Box 118 Phone: +46 46-108779
S-221 00 Lund, SWEDEN Fax: +46 46-138118
More information about the Comp.lang.c
mailing list