Article View: comp.std.c++
Article #1589Re: Incrementing a bool
From: Pablo Halpern
Date: Fri, 04 Aug 1995 16:39
Date: Fri, 04 Aug 1995 16:39
64 lines
3194 bytes
3194 bytes
kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) wrote:
>Actually, if I remember your proposal correctly, the main
>justification given for the bool type was existing practice: every
>major library has one. And most of the examples were in fact C, not
>C++. (A quick grep over the X11 include files, for example reveals
>typedefs/#defines for Boolean, boolean, Bool and BOOL.)
Yes. Other places I have also seen Flag, flag, and FLAG and Logical.
>Obviously, some programmers *have* felt the need for such a type.
I was one of the people who wanted a bool type in the C standard before
it was finalized simply so that I didn't have to define it myself and
worry about coliding with some other #include file (e.g. one library has
"#define bool int;" and I try to do "enum bool { FALSE, TRUE };".
I have never found a need to overload on a boolean expression, so I
don't mind bool being a synonym for int. I just want to use bool (and
TRUE and FALSE) as a way of making code more self-documenting.
Therefore, I would be happy if bool, TRUE, and FALSE were just made part
of the standard library (in stdlib.h, perhaps).
BTW, there is one programming error that can be attributed to the
absense of a real boolean type. The following code is bad style, but I
do see it (and have to debug it) sometimes:
typedef int bool;
#define FALSE 0
#define TRUE 1
int f();
bool x = f();
if (x == TRUE)
// do something
The problem being, of course, that x could have a non-false value of
non-one. A built in bool type could improve this situation. A C++ class
could also solve this problem (but would not be the automatic result of
comparsion operators like operator <(int, int) ). In the following
(untested) class the assignment and comparison operators prevent a bool
objects from ever assuming values other than TRUE or FALSE. Comparisons
to TRUE will always (I think) be interpreted as checks for non-zero:
class bool
{
// Class invarient: 0 <= val <= 1
int val;
public:
bool(int i = 0) : val(i != 0) { } // ensure class invarient
bool(const bool& b) : val(b.val) { }
operator int() const { return val; }
bool& operator=(int i) { val = (i != 0); return *this; }
friend int operator==(bool b1, bool b2)
{ return b1.val == b2.val; }
friend int operator==(bool b, int i)
{ return b.val == (i != 0); }
friend int operator==(int i, bool b)
{ return b.val == (i != 0); }
};
const bool FALSE(0), TRUE(1);
-------------------------------------------------------------
Pablo Halpern phalpern@truffle.ultranet.com
Technical Details
Message-ID:
<3vtigo$shc@caesar.ultra.net>Path:
arc1-ca.pugleaf.net!news.ultranet.com!usenetReferences:
<1995Jul22.022031.10937@sq.sq.com> <3uuprl$frm@newsbf02.news.aol.com> <1995Jul26.030356.21528@sq.sq.com> <DCBsLu.n1A@research.att.com> <KANZE.95Aug3175431@slsvhdt.lts.sel.alcatel.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.ultranet.com!usenet From: phalpern@truffle.ultranet.com (Pablo Halpern) Newsgroups: comp.std.c++ Subject: Re: Incrementing a bool Date: Fri, 04 Aug 1995 16:39:19 GMT Organization: UltraNet Communications, Inc. Message-ID: <3vtigo$shc@caesar.ultra.net> References: <1995Jul22.022031.10937@sq.sq.com> <3uuprl$frm@newsbf02.news.aol.com> <1995Jul26.030356.21528@sq.sq.com> <DCBsLu.n1A@research.att.com> <KANZE.95Aug3175431@slsvhdt.lts.sel.alcatel.de> NNTP-Posting-Host: truffle.ultranet.com X-Newsreader: Forte Free Agent v0.46