Topic: Boolean data types


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/09/24
Raw View
Robert O'Dowd wrote:
>
> andy wrote:
> >
> > use this code instead
> >
> > enum bool { false, true};
> >
> > this should work for those who have older compilers
> >
>
> Except, the following won't work....
>
> bool working = false;
>
> //   lot of code that may set working to true
>
> if (working)
> {
>     // do some more
> }

Where's the problem? working evaluates to 0 or 1, which is the
representation of "false" resp. "true" on systems not having bool.

Even s.th. like

if ((a==b)==false) // equivalent to if (a!=b)

would work.

What wouldn't work would be:

void f(char);
void f(bool);

f(a<b); // should select f(bool), but will select f(char) for enum bool
bool b=a<b; // Error: no implicit conversion int->bool

>
> For older compilers, the modifications needed depend on
> how code is using the bool type.  No one solution will
> do achieve everything that can be done with a real
> implementation of bool.

Indeed, there's no possible solution which will compile correctly
the following program:

#include "booldef.h" // some definition of bool

void f(int) { cout << "int" << endl; }
void f(bool) { cout << "bool" << endl; }

int main() { f(1<0); }

If bool is typedef'd to int, f(int) is defined twice.
In any other case, it will compile but call f(int) instead of f(bool).
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Pete Becker <petebecker@acm.org>
Date: 1998/09/24
Raw View
Sean Ellis wrote:
>
>
> Using #define for True is OK until one of your library header files
> defines a method called "True", or something like that*.

Anyone who names a function (C++ does not have methods) True should be
shot.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/09/25
Raw View
In article <36086B9B.BADAAB71@nonexistant.com>,
  "Robert O'Dowd" <nospam@nonexistant.com> wrote:
>
> andy wrote:
> >
> > use this code instead
> >     enum bool { false, true};
> > this should work for those who have older compilers

> Except, the following won't work....
>
> bool working = false;
> //   lot of code that may set working to true
> if (working)
> {
>     // do some more
> }

Sure, this will work! The enumeration was careful to use false first,
making it's value 0. (I would prefer an explicit
    enum bool { false=0, true=1 };
but that's a style issue -- the first version is equivalent.)

> For older compilers, the modifications needed depend on
> how code is using the bool type.  No one solution will
> do achieve everything that can be done with a real
> implementation of bool.

If we could easily define a class or enumeration type that was
100% equivalent to the new behavior of bool, then the new type
would be documented in the library section instead of in the
fundamental types section. But it isn't, because it isn't.

> Examples of competing requirements include:
>
>    type safety so bool can co-exist with int/char types
>      eg
>
>          void foo(bool);
>          void foo(int);
>          void foo(char);
>
>      could get in trouble if bool maps to an int or char.
>        An enumerated type will be better for resolving this.

And a new class type would probably be even better, but we're picking
nits. The original questioner wanted a way to make simple use of bool
work correctly on an older compiler, not a way to extend the compiler
capability without vendor support.

>    if (bool_variable) ....;
>
>      (as above).  enum types can not be used this way. Mapping
>        bool to an integer type is appropriate for this.

Am I missing something here? Aren't enumerated types implicitly
converted to integer types, but not vice-versa? If so, then
    if (bool_variable)
should be equivalent to
    if (int(bool_variable))
in other words: false for the enumeration with value 0, and true
for all others.

>    It is possible to implement bool as a class type as well.
>      This achieves both of the above, but gives overheads such
>      as constructor/destructor calls when passing bools by
>      by value.  That can be undesirable sometimes too.

The constructor would be short and inline. Copy constructor and
operator= would be automatically generated by the compiler.
Destructors would be useless -- unless you planned to use class
bool as a base class? (If the idea is compatibility with newer
C++ compilers, this would be an ESPECIALLY bad idea.)

> In short, the way you emulate bool with older compilers
> depends on your requirements, and on how code you receive

or write

> uses the bool type.

Correct.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "andy" <no-spam@no-spam.com>
Date: 1998/09/22
Raw View



use this code instead

enum bool { false, true};

this should work for those who have older compilers




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Pete Becker <petebecker@acm.org>
Date: 1998/09/23
Raw View
andy wrote:
>
> use this code instead
>
> enum bool { false, true};
>
> this should work for those who have older compilers

Don't do this. There are many operations that are legal on an enum that
are not legal on a bool, and there are some that mean different things.
If you've done this and you change to a compiler that supports bool and
you're very lucky you'll get error messages for the things that you have
to fix. If you're unlucky it will compile, but run incorrectly. Give
yourself a fighting chance:

#if NO_BOOL
enum Bool { False, True };
#else
typedef bool Bool;
#define False false
#define True true
#endif

Now if you get mysterious problems when you change compilers, just
switch back to the original definition of Bool.

On the other hand, if you prefer not to have to debug code that used to
work, just stick with the old version. Don't use bool when you know
you're going to have to port to a compiler that doesn't support it.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Robert O'Dowd" <nospam@nonexistant.com>
Date: 1998/09/23
Raw View
andy wrote:
>
> use this code instead
>
> enum bool { false, true};
>
> this should work for those who have older compilers
>

Except, the following won't work....

bool working = false;

//   lot of code that may set working to true

if (working)
{
    // do some more
}

For older compilers, the modifications needed depend on
how code is using the bool type.  No one solution will
do achieve everything that can be done with a real
implementation of bool.

Examples of competing requirements include:

   type safety so bool can co-exist with int/char types
     eg

         void foo(bool);
         void foo(int);
         void foo(char);

     could get in trouble if bool maps to an int or char.
       An enumerated type will be better for resolving this.

   if (bool_variable) ....;

     (as above).  enum types can not be used this way. Mapping
       bool to an integer type is appropriate for this.

   It is possible to implement bool as a class type as well.
     This achieves both of the above, but gives overheads such
     as constructor/destructor calls when passing bools by
     by value.  That can be undesirable sometimes too.

In short, the way you emulate bool with older compilers
depends on your requirements, and on how code you receive
uses the bool type.

-<Automagically included trailer>
Robert O'Dowd                       Ph    +61 (8) 9553 3618
DSTO Bldg A51                     Fax    +61 (8) 9553 3577
HMAS Stirling                       Email:
robert.odowd@dsto.defence.gov.au
Rockingham, Western Australia, 6958

Disclaimer: Opinions above are mine and may be worth what you paid for
them


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sellis@geocities.com (Sean Ellis)
Date: 1998/09/23
Raw View
Pete Becker <petebecker@acm.org> wrote:

>andy wrote:
>
>#if NO_BOOL
>enum Bool { False, True };
>#else
>typedef bool Bool;
>#define False false
>#define True true
>#endif

One problem with this is that the #define is a real sledgehammer.

I'd recommend using...

 const Bool False=false;
 const Bool True =true;

...instead.

A minor point, but I've been bitten by this kind of thing before,
notably Visual C++ "wizards" inserting something like...

 #define new  DEBUG_NEW
 #define delete DEBUG_DELETE

...into generated code, then me wondering why my derived class's
per-class operator new won't compile properly.

Using #define for True is OK until one of your library header files
defines a method called "True", or something like that*.

As Stroustrup says in Chapter 18 of _Design and Evolution of C++_: "I
didn't like Cpp at all, and I still don't like it."

-----

* Not as silly as it sounds - "to true" means to straighten, so in CAD
programs it's certainly a candidate for a method name on a graphical
object.

-----


Sean

|||||||| 012345678 Proportiometer(TM)
######## ^  [Non spammers: Delete "un-" after @ in reply-to field]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/09/18
Raw View
In article <35fd096f.0@news.atrax.net.au>,
  "iKaruS" <gonzo@atrax.net.au> wrote:
>
>     I have a C++ compiler (borland C++ Ver 4.5)that does not handle the data
> type "bool".  Can anyone tell me of a header file that will allow me to use
> the data type "bool".

    typedef unsigned char bool;

This isn't 100% completely equivalent -- for instance, function
overloading could break down. But you probably won't accidentally
write code that would work with "real" bool and yet breaks with
this version.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Pete Becker <petebecker@acm.org>
Date: 1998/09/19
Raw View
AllanW@my-dejanews.com wrote:

> In article <35fd096f.0@news.atrax.net.au>,
>   "iKaruS" <gonzo@atrax.net.au> wrote:
> >
> >     I have a C++ compiler (borland C++ Ver 4.5)that does not handle the data
> > type "bool".  Can anyone tell me of a header file that will allow me to use
> > the data type "bool".

>     typedef unsigned char bool;

> This isn't 100% completely equivalent -- for instance, function
> overloading could break down. But you probably won't accidentally
> write code that would work with "real" bool and yet breaks with
> this version.

Why take a chance when you can guarantee that it won't break with "real"
bool? Give it a different name. Then when you go to a compiler that
supports bool try a typedef in place of your definition. If it works,
fine. If not, go back to the original definition.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/09/21
Raw View
Edward Diener <eddielee@abraxis.com> writes:

> iKaruS wrote:

>> I have a C++ compiler (borland C++ Ver 4.5)that does not handle the data
>> type "bool".  Can anyone tell me of a header file that will allow me to use
>> the data type "bool".

> typedef int bool;
> #define true 1
> #define false 0

This is not equivalent.  Just think of overload resolution, implicit
conversions, result type of comparisons, etc...  That's why C++
explicitly supports type bool.

A closer alternative is to define bool as an enumeration, but it fails
to model the correct behavior for results of comparisons :-(

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/09/17
Raw View
You could do

typedef int bool;
#define true 1
#define false 0

BTW BC++ 5.0+ and C++ Builder both implement the "bool" data type.

iKaruS wrote:

>     I have a C++ compiler (borland C++ Ver 4.5)that does not handle the data
> type "bool".  Can anyone tell me of a header file that will allow me to use
> the data type "bool".

[ moderator's note: excessive quoting removed. Please don't quote more
  than what is needed. -sdc ]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "iKaruS" <gonzo@atrax.net.au>
Date: 1998/09/16
Raw View
    I have a C++ compiler (borland C++ Ver 4.5)that does not handle the data
type "bool".  Can anyone tell me of a header file that will allow me to use
the data type "bool".

Thanx.

iKaruS
gonzo@atrax.net.au



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]