C++ 警告 C4800:“int”:强制值布尔值“true”或“false”(性能警告)

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

Warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

c++visual-c++-2010visual-c++-2008

提问by user3157184

I have this problem in my code:

我的代码中有这个问题:

bool CBase::isNumber()
{
return (id & MID_NUMBER);
}

bool CBase::isVar()
{
return (id & MID_VARIABLE);
}

bool CBase::isSymbol()
{
return (id & MID_SYMBOL);
}

回答by Marco A.

FYI: Casts won't hide the warning by design.

仅供参考:演员不会按设计隐藏警告。

Something like

就像是

return (id & MID_NUMBER) != 0;

should clearly state "I want to check whether this value is zero or not" and let the compiler be happy

应该清楚地说明“我想检查这个值是否为零”并让编译器高兴

回答by cup

Use the !! idiom eg

使用 !!习语例如

bool CBase::isNumber()
{
    return !!(id & MID_NUMBER);
}

回答by Vman

Where's the declaration of id and MID_NUMBER? Are you sure they are not windef-style BOOLs rather than (lowercase) bool's? BOOL's have been in windef for decades typedef'd as an int; they pre-date proper C++ bool's and a lot of developers still use them.

id 和 MID_NUMBER 的声明在哪里?您确定它们不是 Windef 风格的 BOOL 而不是(小写)bool 吗?几十年来,BOOL 一直在windef 中被定义为一个int;它们早于适当的 C++ bool 并且许多开发人员仍在使用它们。