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 VARIANT_BOOL != bool Reviewed by Sourabh Soni on Thursday, November 12, 2009 Rating: 5

No comments

Author Details

Image Link [https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZYEKEHJPev0oC4dyp_vZFA3Q6PM99sbRGRgel5lr3s9PJPKQORaMDhc5f0wLqZjHSE79OnUom2STt1asn17AKrN2FPD6gH6gjz4sCmL-fCfCp5ksFbAT6sqxx02KLzi2C_Q2kSMTtQhIM/s1600/sourabhdots3.jpg] Author Name [Sourabh Soni] Author Description [Technocrat, Problem Solver, Corporate Entrepreneur, Adventure Enthusiast] Facebook Username [sourabh.soni.587] Twitter Username [sourabhs271] GPlus Username [#] Pinterest Username [#] Instagram Username [#] LinkedIn Username [sonisourabh] Youtube Username [sonisourabh] NatGeo Username [271730]