Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bool versus BOOL in C++

Author: Gerd Isenberg

Date: 08:41:39 09/26/03

Go up one level in this thread


On September 26, 2003 at 07:59:42, Bo Persson wrote:

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

I see, but performance is an (negligible?) issue here with partial registers.
Pushing actual bool parameters on the stack requires zero extensions - or you
have other missaligned 32-bit values.

IIRC it was a bit more than the "random" noise improvement in IsiChess, when i
switched from bool to BOOL. I use a lot of boolean inlines like isKing()...


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

Very seldom, instead of more (than eight) bools in a structure or an array of
bools i prefere array of bits, but hide the implementation with boolean
inliners.


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

Yes, int and BOOL have the same signature, like
#define BOOL int

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

The most correct way - but unfortunately not the fastest.
Finally i will use a conditional compiled types:

#ifdef QUICK_AND_DIRTY
#define MyBool bool
#else
#define MyBool BOOL
#endif

Gerd

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