Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bool versus BOOL in C++

Author: Omid David Tabibi

Date: 04:47:21 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.
>
>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
>
>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.

I have experienced this problem in the past. It is one of the hardest problems
to debug since that's the last thing you suspect. Now I solve the problem as
follows:

If something is BOOL, I never check the value with == TRUE or == FALSE, etc, but
use it as:

    if (flag)

    if (!flag)

etc.

On the other hand when something is not boolean, I always test it by comparing
to a number. For example, I never do things like

    if (!depth)

instead, I always use:

    if (depth == 0)


or instead of

    if (ply)

use:

    if (ply != 0)

etc.


>
>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?

I think it is compiler dependant. For example, in MSVC++ 4.2 sizeof(bool)==4,
while in later versions sizeof(bool)==1.


>
>Is there any conditional preprocessor directive to ask whether a user defined
>type is already typedefined, similar to #ifdef?
>
>What is your opinion / experience with bool versus BOOL?

It seems to me that BOOL is preferable in time critical programs (e.g., chess),
since it is defined as the size of word, so it is faster. I.e., because of the
same reason that you prefer int to char (when cache size is not an issue).

Additionally, you feel freer with BOOL, but of course that additional freedom
comes at a cost of possible confusion.



>
>Thanks in advance for your suggestions,
>Gerd



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.