views:

409

answers:

6

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 everyone 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.

+1  A: 

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.

Adam Wright
Why the downvotes? This is the code the compiler must generate to handle the cast correctly, and the branch dependency is what it's warning you about.
Billy ONeal
+14  A: 

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.

Kerido
I dont want to make the code look weird like that. check my edits if theres anything you know about?
Newbie
@Newbie: So you seriously mean that the code in your question isn't weird? Well, good luck! Otherwise, I just don't get whose code is weird.
Kerido
A: 

another way:

switch (val) {
  case -1: something_else = !something_else; break;
  case  0: something_else = false; break;
  default: something_else = true; break;
}
Ben Voigt
sorry im not gonna convert my code overcomplicated because of this...
Newbie
You must have no idea what overcomplicated code looks like. And because you came here and asked a question with absolutely no interest in the answer, I'm starting to think you're a troll.
Ben Voigt
thats not the answer im looking for, i look a way to hide the warning of that specific case. im not gonna make my functions a mess like that, sorry. im asking "How do i get rid off the warnings?" not "how to program?"
Newbie
Then change your question to say you want to keep using the same code but hide the warning. You've got answers that tell you that too. But most people who say they want to "get rid of the warning" mean they want to FIX the code so it doesn't produce a warning, not just tell the compiler to shut up. And your "should I do it with !!" suggests that you were looking for the most correct way, not just inhibiting the warning. Well, "!= 0" is the best way to rewrite the single line, and my switch statement is the clearest way to rewrite the entire function.
Ben Voigt
fair enough, now when i think about it, i think this is the best way
Newbie
+1  A: 

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;
    }
}
UncleBens
So there is no way to work around the specific warning? Only disable bool warnings completely? I dont want that since its sometimes useful warning. Also i dont want to make my code any more complicated because of that one warning...
Newbie
The workaround is using: `something_else = val != 0;`
UncleBens
yeah and thats not gonna happen, i dont want to add complexity for such thing.
Newbie
I don't think this adds any "complexity", even though I'm no big fan of those warnings myself (e.g the tribool example produces 87 assorted warnings about added padding and removed inline functions with /Wall).
UncleBens
+1  A: 

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.

Billy ONeal
Yeah != makes no sense to me in that kinda of code, not gonna use it. I just wish there was way to fix that specific warning when i convert int to bool by telling "convert int to bool", i understand that if you dont tell it to covert it to bool its good to show warning
Newbie
You think != 0 makes less sense than !!? That seems odd to me, but whatever. The reason for the warning is that in order to convert to bool, the compiler must insert a branch instruction into the compiled code. Branch instructions are performance sensitive, and the compiler wants you to know that you aren't getting that conversion for "free" like you do when you cast between most other types. By writing != the branch dependence is clear from a simple examination of the program.
Billy ONeal
A: 

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).

kenny