a warning for using bool relation "<"

الإجابة a warning for using bool relation "<"

  • lunedì 12 marzo 2012 22:45
     
      Contiene codice

    hi,all, i got a C4804 warning:

    warning C4804: '<' : unsafe use of type 'bool' in operation

    is it safe to ignore this warning, or i need to do something to fix the code.

    cheers

Tutte le risposte

  • lunedì 12 marzo 2012 23:00
     
     Con risposta

    You didn't show any code, so it depends.  Typically a bool is either 0 or not 0 so doing a test with < could make sense or might not.  If it were me I would just check to see if it's true or false or use a variable type that is more descriptive or what you're trying to do.

    Tom

    • Contrassegnato come risposta daiyueweng martedì 13 marzo 2012 16:28
    •  
  • lunedì 12 marzo 2012 23:07
     
     
    "daiyueweng" <=?utf-8?B?ZGFpeXVld2VuZw==?=> wrote in message news:3a683fc7-377f-48c0-b7f5-95757a550491...

    hi,all, i got a C4804 warning:

    warning C4804: '<' : unsafe use of type 'bool' in operation

    is it safe to ignore this warning, or i need to do something to fix the code.

    cheers


    You must shoot the parrot.
  • lunedì 12 marzo 2012 23:16
     
     Con risposta

    Warnings are usually given for code which could be a sign of bugs. For example, asignment in if statement, unreachable code and things like that.

    If this warning has appeared then no, don't ignore it, the compiler emitted it for a reason so check the line of code that it refers to and try to fix it, you will likely find that it fixes a problem later on.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

    • Contrassegnato come risposta daiyueweng martedì 13 marzo 2012 16:29
    •  
  • martedì 13 marzo 2012 16:17
     
      Contiene codice

    You didn't show any code, so it depends.  Typically a bool is either 0 or not 0 so doing a test with < could make sense or might not.  If it were me I would just check to see if it's true or false or use a variable type that is more descriptive or what you're trying to do.

    Tom

    if(top1 > top2 && top1 + height1 <= top2 + height2 && 0 <= abs((left2 + width2) - (left1 + width1)) < 10){}

    top1 and 2; height1 and 2;left1 and2;width1and 2 are all ints;
  • martedì 13 marzo 2012 16:30
     
     Con risposta

    Complex if statements are always a problem so try not to make them so complicated. Also use parenthesis when you can too.

    But as a bit of a hint, 0 <= x < 10 statements don't usually work well with C++. This gets parsed into something different than what you expect.

    This ends up parsed as (0 <= x) < 10 and as a result it ends up as (false/true) < 10. Since true is 1 then this will always succeed regardless of the result of 0 <= x.

    You need to seperate this out into two comparisons

    0 <= x && x < 10

    to get rid of the warning and the bug waiting to happen.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

    • Contrassegnato come risposta daiyueweng martedì 13 marzo 2012 16:31
    •  
  • mercoledì 14 marzo 2012 05:18
     
     
    Under what conditions do you expect 0 <= abs(...) to ever evaluate to false?