开发者

Forcing value to boolean: (bool) makes warning, !! doesnt

I like (bool) way more, but it generates warnings. How do i get rid off the warnings?

I have code like:

bool something_else = 0;

void switcher(int val = -1){
    if(val != -1){
        something_else = (bool)val;
    }else{
        something_else ^= 1;
    }
}

Should i just do it like ever开发者_Python百科yone else and use '!!' or make it somehow hide the warning messages when using (bool) ? Or is '!!' actually faster than (bool) ?

I would like to use (bool) and so i have to hide the warning, but how?

Edit: Visual Studio 2008 i am using, sorry i forgot to tell.

Edit 2: The warning message is warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) And it comes on the line something_else = (bool)val; And on the line something_else = val; But not on the line something_else = !!val;

The problem is, i want it to respect that i want to convert it to boolean. I dont want to hide all boolean warnings, because sometimes they saved my ass.


You should use operators and constructs specific to the bool type:

bool something_else = false;

void switcher(int val = -1)
{
    if(val == -1)    // val is -1 by default so THIS part is likely to execute
        something_else = !something_else;
    else
        something_else = (val != 0);
}

If your nickname is self-explanatory, it may make sense to write code that others will also understand. In case of this particular question, using the bool type appropriately will surely improve your further co-operation with other developers.


You can make the implied conversion explicit with (val != 0). The behaviour will be the same as a cast to bool, or !!, but makes your intent explicit with respect to the type of val.


I get a warning with VC++:

main.cpp:5: warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

You can work around it if you don't "force" the value into bool, but assign a value that already is boolean:

something_else = val != 0;

(Don't ask me why this deserves to be a warning - among warnings that inform you of serious problems (/W3). IMO, it would be a lot better to have a special flag to turn on warnings of such questionable value.)

But what is the bigger picture: toggle or use val (where -1 represents "don't use")?

How about:

bool something_else = 0;

void switcher(bool val, bool use_val = false){
    if(use_val){
        something_else = val;
    }else{
        something_else = !something_else;
    }
}

Or with tribool (never used it before): :)

#include <boost/logic/tribool.hpp>

bool something_else = false;

void switcher(boost::tribool val = boost::indeterminate){
    if(!indeterminate(val)){
        something_else = val;
    }else{
        something_else = !something_else;
    }
}


I would like to use (bool) and so i have to hide the warning, but how?

Why don't you consult the compiler's documentation on the warning? http://msdn.microsoft.com/en-us/library/b6801kcy(VS.71).aspx

This warning is generated when a value that is not bool 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.


I don't suggest it in this case, but you can always #pragra warning disable.

#pragra warning disable # [, #]

It also may be based on compiler warning level (http://msdn.microsoft.com/en-us/library/b6801kcy(VS.80).aspx).


another way:

switch (val) {
  case -1: something_else = !something_else; break;
  case  0: something_else = false; break;
  default: something_else = true; break;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜