Re: forcing value to bool 'true' or 'false' (performance warning)
Quote:
Originally Posted by
ranjithan
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
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 :(
Re: forcing value to bool 'true' or 'false' (performance warning)
Quote:
Originally Posted by
monarch_dodra
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
Re: forcing value to bool 'true' or 'false' (performance warning)
Quote:
Originally Posted by
Paul McKenzie
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.