CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: forcing value to bool 'true' or 'false' (performance warning)

    Quote Originally Posted by ranjithan View Post
    you can make use of the operator ! which will return bool. See the code snippet below.
    Here is the point I'm making:

    There is no such thing as "BOOL" in C++ -- that is a Windows invention, and it isn't portable (note this is a non-Visual C++ forum). A "bool" is C++ and can be used anywhere a compliant C++ compiler exists, whether it's Windows, UNIX, Mac, or any other operating system.

    That's why if you're writing C++ code, and not using the Windows API, you use "bool". Just because a popular OS API uses "BOOL" doesn't make it good to use in any C++ program (and you definitely can't use it if you're on Linux).

    Regards,

    Paul McKenzie

  2. #17
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: forcing value to bool 'true' or 'false' (performance warning)

    It's funny, because JUST TODAY, I fell on this in one of my pieces of code:

    Code:
    bInstallCanceled = ITevent.IsITRaised()?true:false;
    And I was like: ***?
    Turns out IsITRaised returns a BOOL (read int), and indeed, the ternary operator suppressed a warning (bInstallCanceled is a normal bool).

    I really hate the windows API

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: forcing value to bool 'true' or 'false' (performance warning)

    Quote Originally Posted by monarch_dodra View Post
    It's funny, because JUST TODAY, I fell on this in one of my pieces of code:

    Code:
    bInstallCanceled = ITevent.IsITRaised()?true:false;
    And I was like: ***?
    Turns out IsITRaised returns a BOOL (read int), and indeed, the ternary operator suppressed a warning (bInstallCanceled is a normal bool).

    I really hate the windows API
    The problem is that the Windows API is 'C' based, so there really is no such type as "bool" for 'C' programmers. So BOOL was invented for 'C' programmers to denote TRUE or FALSE. So the only solution (and really, correct solution) is to convert the int to the proper bool value.

    You're lucky that the original API didn't make TRUE equal to -1, which is what a lot of Windows-based languages seem to do. That code may not have worked correctly without the ternary if that were the case (although I need to see the ANSI spec on that one).

    Regards,

    Paul McKenzie

  4. #19
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: forcing value to bool 'true' or 'false' (performance warning)

    Quote Originally Posted by Paul McKenzie View Post
    You're lucky that the original API didn't make TRUE equal to -1, which is what a lot of Windows-based languages seem to do. That code may not have worked correctly without the ternary if that were the case (although I need to see the ANSI spec on that one).
    I'm pretty sure that in C++, the only standard is 0 is false, everything else is true. So even if -1 means true, the code should work, even without the ternary operator.

    What is problematic are those functions that return 0 when everything is fine, and non zero for error code.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured