windows 什么时候应该在 C++ 中使用 BOOL 和 bool?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/830067/
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-09-15 12:24:44  来源:igfitidea点击:

When should BOOL and bool be used in C++?

c++windows

提问by zhaorufei

When should BOOL and bool be used in C++ and why?

什么时候应该在 C++ 中使用 BOOL 和 bool,为什么?

I think using bool is cleaner and more portable because it's a built-in type. But BOOL is unavoidable when you interactive with legacy code/C code, or doing inter-op from .NET with C code/Windows API.

我认为使用 bool 更干净、更便携,因为它是一种内置类型。但是当您与遗留代码/C 代码交互时,或者从 .NET 与 C 代码/Windows API 进行互操作时,BOOL 是不可避免的。

So my policy is: Use bool inside C++. Use BOOL when talk to outer world, e.g., export function in windows DLL.

所以我的策略是:在 C++ 中使用 bool。与外界对话时使用 BOOL,例如,windows DLL 中的导出函数。

Is there a definitive explanation of when to use one over the other?

是否有明确的解释何时使用一个而不是另一个?

采纳答案by Josh Kelley

Matthew Wilson discusses BOOL, bool, and similar in section 13.4.2 of Imperfect C++. Mixing the two can be problematic, since they generally have different sizes (and so pointers and references aren't interchangeable), and since boolisn't guaranteed to have any particular size. Trying to use typedefs or conditional compilating to smooth over the differences between BOOLand boolor trying to allow for a single Boolean type to work in both C and C++ is even worse:

Matthew Wilson在Imperfect C++ 的第 13.4.2 节中讨论了BOOLbool和 类似内容。将两者混合可能会出现问题,因为它们通常具有不同的大小(因此指针和引用不可互换),并且不能保证具有任何特定的大小。尝试使用 typedef 或条件编译来消除和或尝试允许单个布尔类型在 C 和 C++ 中工作的差异甚至更糟:boolBOOLbool

#if defined(__cplusplus) || \
    defined(bool) /* for C compilation with C99 bool (macro) */
 typedef bool   bool_t;
#else
 typedef BOOL   bool_t;
#endif /* __cplusplus */

This approach means that a function's return type can differ depending on which language calls it; Wilson explains that he's seen more than one bug in his own code and others' that results from this. He concludes:

这种方法意味着函数的返回类型可以根据调用它的语言而有所不同;威尔逊解释说,他在自己的代码和其他人的代码中看到了不止一个由此导致的错误。他总结道:

The solution to this imperfection is, as it so often is, abstinence. I never use boolfor anything that can possibly be accessed across multiple link units—dynamic/static libraries, supplied object files—which basically means not in functions or classes that appear outside of header files. The practical answer, such as it is, is to use a pseudo-Boolean type, which is the size of int.

In short, he would agree with your approach.

简而言之,他会同意你的方法。

回答by David Thornley

If BOOL is some sort of integral type, and it always is, and BOOL is defined so that it works right, the standard conversions will automatically get it right. You can't quite use them interchangeably, but you can get close.

如果 BOOL 是某种整数类型,并且始终是,并且 BOOL 被定义为使其正常工作,则标准转换将自动使其正确。你不能完全互换使用它们,但你可以接近。

Use BOOL at the interface, where you have to talk to the Win32 API or whatever. Use bool everywhere else.

在界面上使用 BOOL,您必须在该界面上与 Win32 API 或其他任何东西交谈。在其他地方使用 bool。

回答by bk1e

Another situation where you should use BOOL: when implementing a callback function that takes or returns a BOOL.

您应该使用的另一种情况BOOL:在实现接受或返回BOOL.

For example, EnumWindows()takes a pointer to a callback function with the following signature:

例如,EnumWindows()获取一个指向具有以下签名的回调函数的指针:

BOOL CALLBACK EnumWindowsProc(      
    HWND hwnd,
    LPARAM lParam
);

If you use boolfor this, you will have to typecast your function pointer.

如果您bool为此使用,则必须对函数指针进行类型转换。

回答by Spook

If you wish to use a function written in C++ (for example embedded in DLL library) in managed program (for example in C#), you haveto use BOOL. If you return bool, the result will always be true - this is known bug for a long time and apparently not yet resolved (VS 2010, .NET Framework 4).

如果您希望在托管程序(例如在 C# 中)中使用用 C++ 编写的函数(例如嵌入在 DLL 库中),必须使用 BOOL。如果您返回 bool,结果将始终为 true - 这是一个长期已知的错误,显然尚未解决(VS 2010,.NET Framework 4)。

Best regards -- Spook.

最好的问候 - 幽灵。

回答by Steel

I think of "true"/"TRUE" and "false"/"FALSE" as syntactic sugar, a solution to a problem that never existed. I've always thought it easier to both use and read "1" and "0".

我认为“真”/“真”和“假”/“假”是句法糖,是对一个从未存在过的问题的解决方案。我一直认为使用和读取“1”和“0”更容易。

When you think about flags in registers being on or off, do you think in 1s and 0s or trues and falses? What happens if you want to store several flags in a single variable? 1s and 0s are universal.

当您考虑寄存器中的标志打开或关闭时,您会考虑 1 和 0 还是真假?如果您想在单个变量中存储多个标志会发生什么?1 和 0 是通用的。

I think the word "false" is too long for its own good. When I see a "0", it stands out in my mind like a red stop sign. Stop signs are red because the color red gets people's attention. Reading the word "false" is like seeing a green stop sign.

我认为“虚假”这个词太长了。当我看到“0”时,它就像一个红色的停车标志一样在我的脑海中脱颖而出。停车标志是红色的,因为红色能引起人们的注意。读“假”这个词就像看到一个绿色的停车标志。

So, to hell with bool and BOOL. I default to int.

所以,让 bool 和 BOOL 见鬼去吧。我默认为int。

...but, really, getting boolean flags right is the least worry in a language with as many ways to make a mistake as C++.

...但是,实际上,在与 C++ 一样容易出错的语言中,正确获取布尔标志是最不担心的。