rulururu

post BOOL and bool

January 18th, 2008

Filed under: C++, MFC — Kai @ 2:49 pm

When using MFC I often wondered about the difference between ‘BOOL’ and ‘bool’.
Especially warnings like that confused me:

warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

Some time ago I detected that the size of ‘BOOL’ is 4, that’s why I found out the following:

‘bool’ is a built-in C++ type while ‘BOOL’ is a Microsoft specific type that is defined as an ‘int’. You can find it in ‘windef.h’:

typedef int                 BOOL;
#ifndef FALSE
#define FALSE               0
#endif
#ifndef TRUE
#define TRUE                1
#endif

The only possible values for a ‘bool’ are ‘true’ and ‘false’, whereas for ‘BOOL’ you can use any ‘int’ value, though ‘TRUE’ and ‘FALSE’ macros are defined in ‘windef.h’ header.

If you use the ’sizeof’ operator, it will yield 1 for ‘bool’, though according to the standard the size of’ bool’ is implementation defined, and 4 for ‘BOOL’, on 32-bits platform, where ’sizeof(int)’ is 4 bytes. If the size of ‘int’ changes to 8 bytes on 64-bits platforms, ’sizeof(BOOL)’ will yield 8 instead.

‘BOOL’ was used by Microsoft long before ‘bool’ was actually added to the C++ language, but it has nothing to do with MFC. Many Windows API returns a ‘BOOL’ to indicate success or failure.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)