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://3.bp.blogspot.com/-zo21XIdyPqc/VuTrFfUyPhI/AAAAAAAAAO8/EEWTN73XHUA7aTIjuxuBSN-WGaGkNUymA/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]