When using COM boolean values are to be passed as VARIANT_BOOL which is declared in wtypes.h as short. There are also predefined values for true and false:
#define VARIANT_TRUE ((VARIANT_BOOL)-1)
#define VARIANT_FALSE ((VARIANT_BOOL)0)
Which is the best way to convert from VARIANT_BOOL to C++ bool
type? Obvious variants are:
compare with VARIANT_FALSE
simply cast to bool
Other ways can be easily invented.
Which is the best way to do this - most readable, most standart-compliant, least prone to accidential bugs planting and least prone to issues with porting to 64-bit platforms?
Compare to VARIANT_FALSE
. There is a lot of buggy code out there that mistakenly passes in the C++ bool true
value (cast to the integer value 1) to a function expecting VARIANT_BOOL
. If you compare to VARIANT_FALSE
, you will still get the correct expected value.