BOOL to bool – Performance Warning

Once I was trying to pass a BOOL variable to a function which expected a bool value instead and I ended up with a performance warning.

warning C4800: ‘int’ : forcing value to bool ‘true’ or ‘false’ (performance warning)

It is very easy to avoid this. Put double negation in front of the BOOL variable.

#include <wtypes.h>
void MyFunc1(bool Mybool)
   Mybool ? cout << "True\n" : cout << "False\n";
int main()
   MyFunc1(!! MyBOOL);
   return 0;

Some Additional Information

  • BOOL is microsoft specific and type def as int . TRUE and FALSE are two macros evaluated to 1 and 0 respectively.
  • The size of BOOL is same as that of int (4 in 32 bit platforms) where as the size of bool is 1.
  • The return value of a function returning BOOL can be other than TRUE or FALSE. For example, the following function is perfectly legal.
      BOOL MyFunc(int Number)
         return Number;
      So it is better to avoid checking if (TRUE == MyFunc(10))
      as the return value of MyFunc(10)is never TRUE.
      Instead you can check like
      if (MyFunc(10))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: