How to best convert VARIANT_BOOL to C++ bool?(如何最好地将 VARIANT_BOOL 转换为 C++ bool?)
问题描述
使用 COM 时,布尔值将作为 VARIANT_BOOL 传递,在 wtypes.h 中声明为 short.true 和 false 也有预定义的值:
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)
从 VARIANT_BOOL 转换为 C++ bool
类型的最佳方法是什么?明显的变体是:
Which is the best way to convert from VARIANT_BOOL to C++ bool
type? Obvious variants are:
与 VARIANT_FALSE 比较
compare with VARIANT_FALSE
只需转换为 bool
可以很容易地发明其他方法.
Other ways can be easily invented.
最好的方法是什么 - 最易读、最符合标准、最不容易出现意外错误植入以及最不容易移植到 64 位平台时出现问题?
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?
推荐答案
比较VARIANT_FALSE
.有很多错误代码错误地将 C++ bool true
值(强制转换为整数值 1)传递给期望 VARIANT_BOOL
的函数.如果您与 VARIANT_FALSE
进行比较,您仍然会得到正确的预期值.
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.
这篇关于如何最好地将 VARIANT_BOOL 转换为 C++ bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何最好地将 VARIANT_BOOL 转换为 C++ bool?


- 近似搜索的工作原理 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01