Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bool versus BOOL in C++

Author: Bo Persson

Date: 04:59:42 09/26/03

Go up one level in this thread


On September 26, 2003 at 06:50:59, Gerd Isenberg wrote:

>Hi All,
>
>in C++ we have the boolean type "bool" with the value range true/false.
>I'm not sure about ANSI-C.

There is a _Bool type in C99. If you include bool.h you get the usual typedefs
etc for bool, true, and false.

>
>Due to some C-related "portability" problems and possible performance drawbacks
>due sizeof(bool) == 1 implies partial register handling or zero extending to
>native word lenght, you find in most MS-sources an "own" boolean type BOOL:
>
>typedef int BOOL; // e.g. WINDEF.H

The reason for this is not performance, but that Windows is older than standard
C and C++. There was no bool type back then.

You don't really have to extend the value to test for zero/non-zero. If you have
several bools in a struct, the smaller size could be good too.

>
>This own BOOL type is of course not "typesafe" as bool, as you may assign any
>"int" expressions to it. With BOOL one should interprete zero as FALSE and any
>other value as TRUE. Due to this ambiguity, comparing BOOL-expressions with TRUE
>may be erroneous, so better compare with != FALSE.

There are also potential overload problems.

>
>See also this bugreport related to this issue:
>
>http://www.codeproject.com/buglist/virtualboolbug.asp
>
>Actually i have some "disputes" with colleagues obout it.
>I found bool better for didactical reasons,
>but stay with BOOL for pragmatical reasons.
>
>A few questions:
>
>Is sizeof(bool) == 1 per definition,
>or is it compiler implementation depending?

That is up to the compiler.


>
>Is there any conditional preprocessor directive to ask whether a user defined
>type is already typedefined, similar to #ifdef?

No.

>
>What is your opinion / experience with bool versus BOOL?

I use bool as much as possible. BOOL is just for interfacing with Windows.


>
>Thanks in advance for your suggestions,
>Gerd


Bo Persson




This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.