Advertisement

Code Cosmetics question in (V)C++

Started by July 13, 2001 08:22 AM
2 comments, last by JonnyQuest 23 years, 7 months ago
Hi Might be a really simple question with an even simpler answer, however I can't get rid if the following warnings in Visual C++ 6.0 (SP5):
quote:
--------------------Konfiguration: ILG - Win32 Release-------------------- Kompilierung läuft... loadsettings.cpp I:\ILG\loadsettings.cpp(37) : warning C4800: 'int' : Variable wird auf booleschen Wert ('True' oder 'False') gesetzt (Auswirkungen auf Leistungsverhalten moeglich) I:\ILG\loadsettings.cpp(38) : warning C4800: 'int' : Variable wird auf booleschen Wert ('True' oder 'False') gesetzt (Auswirkungen auf Leistungsverhalten moeglich) I:\ILG\loadsettings.cpp(39) : warning C4800: 'int' : Variable wird auf booleschen Wert ('True' oder 'False') gesetzt (Auswirkungen auf Leistungsverhalten moeglich) I:\ILG\loadsettings.cpp(40) : warning C4800: 'int' : Variable wird auf booleschen Wert ('True' oder 'False') gesetzt (Auswirkungen auf Leistungsverhalten moeglich) Linker-Vorgang läuft... ILG.exe - 0 Fehler, 4 Warnung(en)
which translates to something like that:
quote:
--------------------Configuration: ILG - Win32 Release-------------------- Compilation running... loadsettings.cpp I:\ILG\loadsettings.cpp(37) : warning C4800: 'int' : variable is being set to boolean value ('True' or 'False') (may effect performance) I:\ILG\loadsettings.cpp(38) : warning C4800: 'int' : variable is being set to boolean value ('True' or 'False') (may effect performance) I:\ILG\loadsettings.cpp(39) : warning C4800: 'int' : variable is being set to boolean value ('True' or 'False') (may effect performance) I:\ILG\loadsettings.cpp(40) : warning C4800: 'int' : variable is being set to boolean value ('True' or 'False') (may effect performance) Linker-procedure running... ILG.exe - 0 Errors, 4 Warning(s)
This is the troublesome code:
      
	Settings->General.nDevice = pnData[0];
	Settings->General.bFullscreen = static_cast<bool>(pnData[1] & GF_FullScreen);
	Settings->General.bHALTransform = static_cast<bool>(pnData[1] & GF_HALTrans);
	Settings->General.bVSync = static_cast<bool>(pnData[1] & GF_VSync);
	Settings->General.bNoVSync = static_cast<bool>(pnData[1] & GF_NoVSync);
	Settings->General.bMultitexture = static_cast<bool>(pnData[1] & GF_Multitex);
	Settings->General.nWidth = pnData[2];
	Settings->General.nHeight = pnData[3];
	Settings->General.nBitDepth = pnData[4];
  
It's the bottom four of the five lines with the static_cast. It doesn't matter if I use the cast or not, it always gives me the warning. pnData is a pointer to an int array, the GF_ constants are part of this enum:
  
enum Const_GeneralFlags
{
    GF_FullScreen = 1,
    GF_VSync = 2,
    GF_NoVSync = 4,
    GF_DefaultVSync = 0,
    GF_Multitex = 8,
    GF_HALTrans = 16
};       
Obviously, it likes the first of the five lines because it only can return 0 or 1. Can I somehow solve this without bitshifting or so? (e.g. (pnData[1] & GF_HALTrans) << 4) Using that solution would generally reduce readability. Thanks in advance. - JQ Infiltration: Losing Ground "Simpson, eh?" -Mr. Burns "Excellent!" -the above Edited by - JonnyQuest on July 13, 2001 9:27:14 AM
~phil
This method seems slightly better:
  	Settings->General.bHALTransform = !(!(pnData[1] & GF_HALTrans));  

i.e. if it''s not not true, but IMO, that''s just plain crazy.

I''m looking for the correct way.

- JQ
Infiltration: Losing Ground
"Simpson, eh?" -Mr. Burns
"Excellent!" -the above
~phil
Advertisement
From MSDN:

Compiler Warning (level 3) C4800
''type'' : forcing value to bool ''true'' or ''false'' (performance warning)

This warning is generated when some non-bool value is assigned or coerced into type bool. Typically, this message is caused by assigning int variables to bool variables where the int variable contains only values true and false, and could be redeclared as type bool. If you cannot rewrite the expression to use type bool, then you can add "!=0" to the expression, which gives the expression type bool. Casting the expression to type bool will not disable the warning, which is by design.
----

So the MS way is to do something like this:

  Settings->General.bHALTransform = ((pnData[1] & GF_HALTrans) != 0);  




Mark Fassett
Laughing Dragon Entertainment
http://www.laughing-dragon.com

Mark Fassett

Laughing Dragon Games

http://www.laughing-dragon.com

.runscreamingintowall

Thanks. I do have the MSDN, I just forgot to look up the warning there. Duh. Thanks a lot anyway.


- JQ
Infiltration: Losing Ground
"Simpson, eh?" -Mr. Burns
"Excellent!" -the above
~phil

This topic is closed to new replies.

Advertisement