如何最好地将 VARIANT_BOOL 转换为 C++ bool?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/886729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:47:46  来源:igfitidea点击:

How to best convert VARIANT_BOOL to C++ bool?

c++windowscominteropboolean

提问by sharptooth

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 trueand false:

使用 COM 时,布尔值将作为 VARIANT_BOOL 传递,在 wtypes.h 中声明为shorttruefalse也有预定义的值:

#define VARIANT_TRUE ((VARIANT_BOOL)-1)
#define VARIANT_FALSE ((VARIANT_BOOL)0)

Which is the best way to convert from VARIANT_BOOL to C++ booltype? Obvious variants are:

从 VARIANT_BOOL 转换为 C++bool类型的最佳方法是什么?明显的变体是:

  1. compare with VARIANT_FALSE

  2. simply cast to bool

  1. 与 VARIANT_FALSE 比较

  2. 简单地强制转换为 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?

哪个是最好的方法 - 最易读、最符合标准、最不容易出现意外的错误种植以及最不容易出现移植到 64 位平台的问题?

回答by 1800 INFORMATION

Compare to VARIANT_FALSE. There is a lot of buggy code out there that mistakenly passes in the C++ bool truevalue (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_FALSE。有很多错误代码错误地将 C++ booltrue值(转换为整数值 1)传递给期望VARIANT_BOOL. 如果与 进行比较VARIANT_FALSE,您仍然会得到正确的预期值。

回答by Euro Micelli

I don't like to have to worry about compatibility between different boolean values, so I will normally write:

我不喜欢担心不同布尔值之间的兼容性,所以我通常会写:

VARIANT_BOOL vb_bool = VARIANT_FALSE;

// ... vb_bool set to something by some other code

bool myBool = (vb_bool == VARIANT_TRUE);

Are there tinier (as in "will compile to simpler x86 code"), valid ways to do it? Of course. Not worth it. This is guaranteed to work, so I can worry about my business logic.

是否有更小的(如“将编译为更简单的 x86 代码”),有效的方法?当然。不值得。这保证有效,所以我可以担心我的业务逻辑。

回答by Matthew Flaschen

Casting to bool is obviously wrong. Some people say (e.g. comments at BOOL vs. VARIANT_BOOL vs. BOOLEAN vs. bool) to compare to VARIANT_FALSE, but I would compare to both. That way you catch invalid values (anything but VARIANT_FALSE or VARIANT_TRUE) early.

转换为 bool 显然是错误的。有人说(例如BOOL vs. VARIANT_BOOL vs. BOOLEAN vs. bool 的评论)与 VARIANT_FALSE进行比较,但我会比较两者。这样您就可以尽早捕获无效值(除了 VARIANT_FALSE 或 VARIANT_TRUE 之外的任何值)。

e.g.

例如

bool VariantBoolToBool(VARIANT_BOOL varFlag)
{
  bool boolFlag;

  switch( varFlag ) 
  {
    case VARIANT_TRUE:
        boolFlag = true;
        break;
    case VARIANT_FALSE:
        boolFlag = false;
        break;
    default:
        throw Exception("Not a valid value");
  }

  return boolFlag;
}

回答by Jesus Fernandez

Declare this macro in one of your global headers.

在您的全局标头之一中声明此宏。

#define b(X) ((X)!=VARIANT_FALSE)


EDIT: Much safer version:


编辑:更安全的版本:

inline bool b(VARIANT_BOOL v){return v!=VARIANT_FALSE;}

回答by Pod

Why have an explicit cast?

为什么要有明确的演员表?

if (my_bool)
{
    blargh();
}
else
{
   blarglerr();
}

This way, true is true and false is false, as per the C standard. If you need to SET a C++ style boolthen do something like:

这样,按照 C 标准,真为真,假为假。如果您需要设置 C++ 样式,bool请执行以下操作:

VARIANT_BOOL vb_bool = VARIANT_FALSE
bool cpp_bool = !!vb_bool

回答by Ruben Bartelink

Standard C++ conversion rules rely on zero meaning false [and as 1800 INFORMATION points out, the TRUE variant is where the most confusion happens] and nothing more. Hence a static_cast would be best.

标准 C++ 转换规则依赖于 0 表示 false [并且正如 1800 INFORMATION 指出的那样,TRUE 变体是最容易混淆的地方],仅此而已。因此 static_cast 将是最好的。

But in many cases, the code would be more readable as a comparison. In that case, VARIANT_BOOL is the thing to compare with.

但在许多情况下,作为比较,代码会更具可读性。在这种情况下, VARIANT_BOOL 是要比较的东西。