VARIANT_BOOL != bool
Developers do a very common mistake of treating the VARIANT_BOOL as C++ bool . The VARIANT_BOOL is defined as below:
typedef short VARIANT_BOOL; #define VARIANT_TRUE ((VARIANT_BOOL)-1) #define VARIANT_FALSE ((VARIANT_BOOL)0) |
Since VARIANT_TRUE is -1, it is not equivalent to true. Hence, direct typecasting of a bool true value to VARIANT_BOOL would not result into VARIANT_TRUE value. And this is a severe bug.
The safer way is to define a macro or inline function that would explicitly compare the VARIANT_BOOL variable value with VARIANT_FALSE and returns true/false.
#define CAST_TO_VARIANT_BOOL(x) (!(x) ? VARIANT_FALSE : VARIANT_TRUE) #define CAST_FROM_VARIANT_BOOL(x) ((x) == VARIANT_FALSE ? false : true) |
VARIANT_BOOL != bool
Reviewed by Sourabh Soni
on
Thursday, November 12, 2009
Rating:
No comments