Condition question with VC++ 2008

This is a discussion on Condition question with VC++ 2008 within the C++ Programming forums, part of the General Programming Boards category; okay i have the following code the first condition produces a warning, and the second doesnt. I am curious as ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Condition question with VC++ 2008

    okay i have the following code the first condition produces a warning, and the second doesnt. I am curious as to why it produces a warning in the first place.
    Code:
    static const int A = 1;
    static const int B = 2;
    
    int main()
    {
      if ( SomeFunc() == ( A || B ) ) // produces warning saying '==' : unsafe mix of type 'int' and type 'bool' in operation
         //do something
      if ( SomeFunc() == (int)( A || B ) ) //no warning due to cast, is there a problem with this?
        //do the same thing
    
    }
    Thank you for any insight.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    The warning is not required by the Standard, but is an attempt to be helpful. In this case I think it makes sense.

    The result of (A || B) is a bool. But what does SomeFunc() return? If it returns a bool, well and good. But it seems to return an int, and perhaps you did not intend to compare an int with a bool. This is what the warning is getting at.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Yea, but how do i check if the function returned 2 integers?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Yea, but how do i check if the function returned 2 integers?
    The function cannot return two integers, it can only return one integer. Did you write it yourself?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I said that the wrong way.
    1. Yes i do have class i made that allows a functions to return multiple values in a sense.
    2. I meant to ask how do i see which of the two integers the given function returned.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you want is probably something like this:
    Code:
    int x = SomeFunc();
    if (x == 2 || x == 1) ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Oh... so i can't do it by the return value inside the condition statement. Thank you.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So, SomeFunc returns an integer and you want to check if the return value is either A or B.

    See, how the compiler got that warning right. This code indeed did something else than you intended. A || B produces true, which you cast to int in your attempt to suppress the warning, hence it becomes 1 and you end up checking if SomeFunc returns 1.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Raigne View Post
    Oh... so i can't do it by the return value inside the condition statement. Thank you.
    Well, if you want to be "complicated", you could do this:
    Code:
    int x;
    if ((x = SomeFunc()) == 1 || x == 2) ...
    But note the extra paranthesis that you need, if you get that wrong, it will still compile nicely, but the test will assign x with the value of (SomeFunc() == 1), which is either 0 or 1 depending on the return value from SomeFunc().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. While Statements Question
    By thekautz in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 01:48 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21