Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bool versus BOOL in C++

Author: Gerd Isenberg

Date: 07:58:06 09/26/03

Go up one level in this thread


On September 26, 2003 at 07:51:49, Russell Reagan wrote:

>On September 26, 2003 at 06:50:59, Gerd Isenberg wrote:
>
>>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.
>
>Hi Gerd,
>
>I've seen people do it like this:
>
>typedef int BOOL;
>
>#define FALSE (0)
>#define TRUE  (!FALSE)

Hi Russell,

Yes, see ms WINDEF.H

typedef int                 BOOL;

#ifndef FALSE
#define FALSE               0
#endif

#ifndef TRUE
#define TRUE                1
#endif

>
>I have also heard that if you use C++ 'bool', that sometimes the compiler can
>make some optimizations since it knows it will always be a zero or one. With a
>'BOOL', I'm not sure if it will know that. I guess you could try this:
>
>enum BOOL { FALSE, TRUE };
>
>Then the compiler might assume that a 'BOOL' is always zero or one, and you
>couldn't assign other values to a 'BOOL'.
>

Yes, that seems safe, but has some other drawbacks:

BOOL cond = (a > b); // is an error and has to use an explicite cast
BOOL cond = (BOO)(a > b):
BOOL cond = (a > b) ? TRUE : FALSE; // or conditional assignment

Even if it produces the same branchless code, i found that annoying.

>I don't think that the sizeof(bool) is always 1. I think it is 4 on some
>compilers. When BOOL is an enum, the sizeof(BOOL) is 4 in MSVC.
>
>
>#include <iostream>
>
>enum BOOL { FALSE, TRUE };
>
>int main ()
>{
>	std::cout << "sizeof(bool) is " << sizeof(bool) << std::endl;
>	std::cout << "sizeof(BOOL) is " << sizeof(BOOL) << std::endl;
>	return 0;
>}
>
>sizeof(bool) is 1
>sizeof(BOOL) is 4

I see, thanks.
The problem arises with ms .net and language independent data types.

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.