[英]What is the preferred way in C++ for converting a builtin type (int) to bool?
When programming with Visual C++, I think every developer is used to see the warning 使用Visual C ++进行编程时,我认为每个开发人员都会看到警告
warning C4800: 'BOOL' : forcing value to bool 'true' or 'false'
from time to time. 时。 The reason obviously is that BOOL is defined as int and directly assigning any of the built-in numerical types to bool
is considered a bad idea. 原因显然是BOOL被定义为int并且直接将任何内置数值类型分配给bool
被认为是一个坏主意。
So my question is now, given any built-in numerical type (int, short, ...) that is to be interpreted as a boolean value, what is the/your preferred way of actually storing that value into a variable of type bool
? 所以现在我的问题是,给定任何内置的数值类型(int,short,...)将被解释为一个布尔值,实际将该值存储到bool
类型的变量中的首选方法是什么? ?
Note: While mixing BOOL and bool is probably a bad idea, I think the problem will inevitably pop up whether on Windows or somewhere else, so I think this question is neither Visual-C++ nor Windows specific. 注意:虽然混合BOOL和bool可能是一个坏主意,但我认为无论是在Windows上还是在其他地方,问题都将不可避免地出现,所以我认为这个问题既不是Visual-C ++也不是Windows特定的。
Given int nBoolean;
给定int nBoolean;
I prefer this style: 我更喜欢这种风格:
bool b = nBoolean?true:false;
The following might be alternatives: 以下可能是替代方案:
bool b = !!nBoolean;
bool b = (nBoolean != 0);
Is there a generally preferred way? 有一般的首选方式吗? Rationale? 什么道理呢?
I should add: Since I only work with Visual-C++ I cannot really say if this is a VC++ specific question or if the same problem pops up with other compilers. 我应该补充一下:由于我只使用Visual-C ++,我无法确定这是否是VC ++特定的问题,或者是否与其他编译器弹出相同的问题。 So it would be interesting to specifically hear from g++ or users how they handle the int->bool case. 因此,专门听取g ++或用户如何处理int-> bool案例会很有趣。
Regarding Standard C++: As David Thornley notes in a comment, the C++ Standard does not require this behavior. 关于标准C ++:正如David Thornley在评论中指出的那样,C ++标准不需要这种行为。 In fact it seems to explicitly allow this, so one might consider this a VC++ weirdness. 实际上它似乎明确地允许这个,所以有人可能认为这是VC ++的怪异。 To quote the N3029 draft (which is what I have around atm.): 引用N3029草案(这是我周围的atm。):
4.12 Boolean conversions [conv.bool] 4.12布尔转换[conv.bool]
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. 算术,无范围枚举,指针或指向成员类型的指针的prvalue可以转换为bool类型的prvalue。 A zero value, null pointer value, or null member pointer value is converted to false; 零值,空指针值或空成员指针值转换为false; any other value is converted to true. 任何其他值都转换为true。 (...) (......)
In the context of using win32 SDK and MFC, I tend to write it always this way. 在使用win32 SDK和MFC的上下文中,我总是以这种方式编写它。 It's explicit. 这很明确。
bool b = (myBOOL != FALSE);
EDIT: I have edited :-) cause i'm not sure myBOOL == TRUE works for all implementation of BOOL and I can assume that FALSE may have the 0 value most of the time. 编辑:我已编辑:-)因为我不确定myBOOL == TRUE是否适用于BOOL的所有实现,我可以假设FALSE可能在大多数情况下具有0值。
我假设的正确方法是:
bool b = static_cast<bool>(val);
I cast my vote for 我投了票
BOOL nBoolean;
bool b = (nBoolean != 0);
Reason? 原因? Since BOOL resolves to an int, one should compare it to an int when converting to bool. 由于BOOL解析为int,因此在转换为bool时应将其与int进行比较。 The other two methods: !!nBoolean
and nBoolean?true:false
treat nBoolean as as logical value and therefore perform an implicit
cast
conversion. 另外两种方法: !!nBoolean
和nBoolean?true:false
治疗nBoolean作为为逻辑值,因此进行隐式
转换
的转换。
Three good ways: 三个好方法:
static_cast<bool>( whatever )
bool( whatever )
!!whatever
Personally I prefer the last, but *nix folks may react negatively (not needed for those compilers, so not familiar with that idiom). 我个人更喜欢最后一个,但是* nix的人可能会做出消极的反应(那些编译器不需要,所以不熟悉那个习语)。
One reason that the last one is good is that it shuts up Visual C++ (sillywarning suppression). 最后一个好的一个原因是它关闭了Visual C ++(愚蠢的警告抑制)。
Ungood ways include comparing with true
or false
, especially the former. Ungood的方式包括比较true
或false
,尤其是前者。
Cheers & hth., 干杯&hth。,
I tend to write it always this way. 我总是这样写它。 . 。
bool b = !!myBOOL;
It is clearer (well as an English speaker, I am used to double-negatives....) 它更清楚(作为一个说英语的人,我习惯于双重否定......)
It is also safer and avoids mistakes like: 它也更安全,避免错误,如:
bool b = (myBOOL = FALSE); //oops!
Also, I am of the opinion that booleans should never be compared using ==
or !=
rather &&
should be used. 另外,我认为不应该使用==
或!=
来比较布尔值,而应该使用&&
。 As soon as ==
or !=
is used the boolean variable is no longer treated as a boolean but as an integral value which defeats the purpose of boolean. 一旦使用了==
或!=
,布尔变量就不再被视为布尔值,而是作为一个符合布尔值目的的整数值。
There is no preferred way in C++ , since the C++ Std simply allows the integral conversion from int
to bool
. 在C ++中 没有首选方法,因为C ++ Std只允许从int
到bool
的整数转换 。 (So the preferred way wrt the Std would be bool b = i;
.) (所以std的首选方式是bool b = i;
)
That said, judging from the other answers, there does not even seem to be an accepted way to do it in Visual C++ (MS) although the MSDN page states 也就是说,从其他答案来看,尽管MSDN页面声明,但在Visual C ++(MS)中似乎没有一种可接受的方法可以做到这一点。
... If you cannot rewrite the expression to use type bool, then you can add "!=0" to the expression, which gives the expression type bool. ...如果你不能重写表达式使用类型bool,那么你可以在表达式中添加“!= 0”,它给表达式类型bool。 ... ...
So one might conclude that MS recommends to use the !=0
comparison, although I, personally, think it's the worst of all the warning-supressing alterantives presented in the question and the answers here: The "type" in the source code is BOOL
, and even though it's really just an int
at least one should compare a " BOOL
" with !=FALSE
like has been proposed in some other answers. 因此可以得出结论,MS建议使用!=0
比较,虽然我个人认为这是问题中提出的所有警告抑制变更中最糟糕的,这里的答案是:源代码中的“类型”是BOOL
,即使它真的只是一个int
,至少应该将一个“ BOOL
”与!=FALSE
进行比较,就像在其他一些答案中提出的那样。
If this bothered me, I would write a helper function like 如果这困扰我,我会写一个帮助函数,如
inline bool to_bool(BOOL b) {...}
which would hide one of the proposed implementations inside it. 这将隐藏其中一个建议的实现。 And never think about this again :) 永远不要再考虑这个:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.