Dave's Brain

Browse - programming tips - warning C4800 int forcing value to bool true or false performance warning

Date: 2010jan24
Product: VC++

Q.  How can I fix: warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

A.  The Visual Studio compiler makes a big deal of int to bool conversions.
For example, if you do:

	bool	bPunct = ispunct(c);	// causes warning

You'll get the warning.  So how about this attempt at a fix:

	bool	bPunct = (bool) ispunct(c);	// still causes warning

No luck.  The help file says its "by design" for a cast not to hide the
warning.  Here are a number of ways to get around the warning.

Use a macro to do the conversion:

	#define MKBOOL(_a)			((_a) != 0)

	bool	bPunct = MKBOOL(ispunct(c));	// No warning

Use BOOL (which is an int in Windows):

	BOOL	bPunct = ispunct(c);	// No warning

Disable the warning (perhaps in your stdafx.h file):

	#pragma warning(disable: 4800)	// disables the warning
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.