Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bool versus BOOL in C++

Author: Russell Reagan

Date: 04:51:49 09/26/03

Go up one level in this thread


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)

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

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



This page took 0.02 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.